// Server code
Import java. io .*;
Import java.net .*;
Public class ServerTest3 {
Public static void main (String [] args ){
Try {
// Set the sever Link
ServerSocket s = new ServerSocket (8888 );
Socket s1 = s. accept (); // sever waiting for Link
// Instantiate the output stream
OutputStream OS = s1.getOutputStream ();
DataOutputStream dos = new DataOutputStream (OS );
// Instantiate the input stream
InputStream is = s1.getInputStream ();
DataInputStream dis = new DataInputStream (is );
// Instantiate the objects of two threads
Thread msr = new MyServerReader (dis );
Thread msw = new MyServerWriter (dos );
// Start the thread
Msr. start ();
Msw. start ();
}
// Capture exceptions
Catch (SocketException e ){
System. out. println (e );
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process for receiving and reading data
Class MyServerReader extends Thread {
Private DataInputStream dis;
Public MyServerReader (DataInputStream dis ){
This. dis = dis;
}
Public void run (){
String msg;
Try {
While (true ){
Msg = dis. readUTF ();
System. out. println ("the other party said:" + msg );
If (msg. equals ("bye ")){
System. out. println ("the other party goes offline, the program exits ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process to write and send data
Class MyServerWriter extends Thread {
Private DataOutputStream dos;
Public MyServerWriter (DataOutputStream dos ){
This. dos = dos;
}
Public void run (){
InputStreamReader isr = new InputStreamReader (System. in );
BufferedReader br = new BufferedReader (isr );
String msg;
Try {
While (true ){
Msg = br. readLine ();
Dos. writeUTF (msg );
If (msg. equals ("bye ")){
System. out. println ("deprecate yourself, exit the program ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Client code
Import java. io .*;
Import java.net .*;
Public class ClientTest3 {
Public static void main (String [] args ){
Try {
// Link the server
Socket s1 = new Socket ("127.0.0.1", 8888 );
// Instantiate the input stream
InputStream is = s1.getInputStream ();
DataInputStream dis = new DataInputStream (is );
// Instantiate the output stream
OutputStream OS = s1.getOutputStream ();
DataOutputStream dos = new DataOutputStream (OS );
// Instantiate two processes
Thread mcr = new MyClientReader (dis );
Thread mcw = new MyClientWriter (dos );
// Start two processes
Mcr. start ();
Mcw. start ();
// Capture exceptions
} Catch (SocketException e ){
System. out. println (e );
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process for receiving and reading data
Class MyClientReader extends Thread {
Private DataInputStream dis;
Public MyClientReader (DataInputStream dis ){
This. dis = dis;
}
@ Override
Public void run (){
String msg;
Try {
While (true ){
Msg = dis. readUTF ();
System. out. println ("the other party said:" + msg );
If (msg. equals ("bye ")){
System. out. println ("the other party goes offline, the program exits ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
// Create a process to write and send data
Class MyClientWriter extends Thread {
Private DataOutputStream dos;
Public MyClientWriter (DataOutputStream dos ){
This. dos = dos;
}
@ Override
Public void run (){
InputStreamReader isr = new InputStreamReader (System. in );
BufferedReader br = new BufferedReader (isr );
String msg;
Try {
While (true ){
Msg = br. readLine ();
Dos. writeUTF (msg );
If (msg. equals ("bye ")){
System. out. println ("deprecate yourself, exit the program ");
System. exit (0 );
}
}
} Catch (IOException e ){
System. out. println (e );
}
}
}
Open two consoles, first run the server code, then run the client code, and then the two ends can make free calls