Java Program for protocol 1 with one side communication from sender to receiver assuming that receiver always receives the message.

//sender to send message for protocol 1

import java.io.*;
import java.net.*;
public class protocol1_Sender
{
public static void main(String args[])
{
try
{

Socket s=new Socket("localhost",6666);
DataOutputStream dos=new DataOutputStream(s.getOutputStream());


DataInputStream dis=new DataInputStream(s.getInputStream());


BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

String str="",ch="";
do
{
System.out.println("Sender: ");
str=br.readLine();

System.out.println("String: "+str);
dos.writeUTF(str);

System.out.println("press 1 to continue.. ");
ch=br.readLine();

dos.writeUTF(ch);
//str1=dis.readUTF();
//System.out.println("Server says: " +str1);

}while(ch.equals("1"));

dos.flush();

dos.close();
//s.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
}
}
//receiver to receive message for protocol 1

import java.io.*;
import java.net.*;
public class protocol1_Rec
{
public static void main(String args[])

try
{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();

DataInputStream dis=new DataInputStream(s.getInputStream());

DataOutputStream dos=new DataOutputStream(s.getOutputStream());

//BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

                String msg="", ch="";

int i;

do
{
msg=dis.readUTF();
System.out.println("Given string is: "+msg);
//dos.writeUTF(result);
//dos.flush();
ch=dis.readUTF();
}while(ch.equals("1"));

dis.close();
s.close();
ss.close();
}
catch(IOException e)
{
System.out.println(e.getMessage());

}
}
}

Comments

Popular posts from this blog

Java Program to implement binary checksum

Java Program for block parity

Welcome Guide to Swift 5 Escaping String