Server-side
Package org.tcp;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.PrintStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Echoserver {
public static void Main (string[] args) throws exception{
ServerSocket server = null;
Socket client = null;
PrintStream out = null;
BufferedReader buf = null;
Server = new ServerSocket (8888);
Boolean F = true;
while (f) {
SYSTEM.OUT.PRINTLN ("The server is running, wait for the client link.") ");
Client = Server.accept ();
BUF = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
out = new PrintStream (Client.getoutputstream ());
Boolean flag = true;
while (flag) {
String str = buf.readline ();
if (str = = null| | "". Equals (str)) {
Flag = false;
}else{
if ("Bye". Equals (str)) {
Flag = false;
}else{
Out.println ("ECHO:" +str);
}
}
}
Out.close ();
Client.close ();
}
Server.close ();
}
}
Program Run Result: server running, waiting for client link.
Client
Package org.tcp;
Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.PrintStream;
Import Java.net.Socket;
public class Echoclient {
public static void Main (string[] args) throws exception{
Socket client = null;
Client = new Socket ("localhost", 8888);
BufferedReader buf = null;
PrintStream out = null;
BufferedReader input = null;
input = new BufferedReader (new InputStreamReader (system.in));
out = new PrintStream (Client.getoutputstream ());
BUF = new BufferedReader (New InputStreamReader (Client.getinputstream ()));
Boolean flag = true;
while (flag) {
System.out.print ("Input information:");
String str = input.readline ();
Out.println (str);
if ("Bye". Equals (str)) {
Flag = false;
}else{
String echo = Buf.readline ();
System.out.println (Echo);
}
}
Client.close ();
Buf.close ();
}
}
Program Run Result: input information: Chen Fei Peng
ECHO: Chen Fei Peng
Input information: Chenfeipeng
Echo:chenfeipeng
Input information: Hello
Echo:hello
Input information: bye