// SERVER SIDE
import java.io.*;
import java.net.*;
public class myserver
{
public static void main(String args[])
{
try
{
// Creating a server side socket.
// TCP Port 8765
// Data being sent throught this port.
ServerSocket ss = new ServerSocket(8765);
Socket incoming = ss.accept();
BufferedReader br = new BufferedReader (new InputStreamReader(incoming.getInputStream()));
BufferedReader lin = new BufferedReader (new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(incoming.getOutputStream(),true);
System.out.println("Server : Enter BYE to exit");
out.println("Enter BYE to exit");
boolean more = true;
while(more)
{
String line = br.readLine();
if(line == null)
more = false;
else
{
if(line.trim().equals("BYE"))
more = false;
line = "Client:" + line ;
System.out.print(line + "\nServer:");
line = lin.readLine();
out.println("Server:"+line);
}
}
incoming.close();
}
catch(Exception e)
{
}
}
}
// CLIENT SIDE
import java.io.*;
import java.net.*;
public class myclient
{
public static void main(String args[])
{
try
{
int character;
// Creating Client Socket, to transmit/receive Data from client side
Socket socket = new Socket("127.0.0.1",8765);
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader lin = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
boolean more = true;
String line = br.readLine();
System.out.println(line);
while(more)
{
System.out.print("\nClient:");
line = lin.readLine();
if(line.trim().equals("BYE"))
more = false;
else
{
out.println(line);
line = br.readLine();
System.out.println(line);
}
}
}
catch(Exception e)
{}
}
}