Java Program for protocol 2 with two way communication between sender and receiver until receiver response some signal.

//sender to send message for protocol 1

import java.io.*;
import java.net.*;
public class protocol2_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="",signal="";
do
{
System.out.println("Sender: ");
str=br.readLine();
System.out.println("String: "+str);
dos.writeUTF(str);

//dos.writeUTF(ch);
//dos.flush();

signal=dis.readUTF();
System.out.println("Server says: " +signal);

}while(signal.equals("signal"));
//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 protocol2_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.flush();
dos.writeUTF("signal");

//ch=dis.readUTF();
}while(!msg.equals(""));
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