New two projects, one client, one server, first start the server and then start the client
The read-write operation thread classes for both projects are essentially identical
Service side:
Import Java.io.BufferedReader;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.ServerSocket;
Import Java.net.Socket; public class Server {public static final int port = 8000;//Listener port number public static void main (string[] args) {Ser
Ver Server = new server ();
Server.init ();
public void init () {ServerSocket serversocket = null;
try {serversocket = new ServerSocket (PORT);
while (true) {Socket client = serversocket.accept ();
A client connection accounts for two threads to handle read and write new thread (new Readhandlerthread (client)). Start ();
New Thread (new Writehandlerthread (client)). Start ();
} catch (Exception e) {e.printstacktrace ();
} finally{try {if (ServerSocket!= null) {serversocket.close ();
} catch (IOException e) {e.printstacktrace (); }}}/* * thread that handles read operations/class Readhandlerthread implements runnable{private Socket client;
Public Readhandlerthread (Socket client) {this.client = client;
@Override public void Run () {datainputstream dis = null;
try{while (true) {//read client data dis = new DataInputStream (Client.getinputstream ());
String reciver = Dis.readutf ();
SYSTEM.OUT.PRINTLN ("Client sent over the content:" + reciver);
}}catch (Exception e) {e.printstacktrace ();
}finally{try {if (dis!= null) {dis.close ();
} if (client!= null) {client = null;
} catch (IOException e) {e.printstacktrace ();
}}/* * The thread that handles the write operation/class Writehandlerthread implements runnable{private Socket client;
Public Writehandlerthread (Socket client) {this.client = client;
@Override public void Run () {DataOutputStream dos = null;
BufferedReader br = null;
try{while (true) {//Reply message to client dos = new DataOutputStream (Client.getoutputstream ());
System.out.print ("Please enter: \ t"); Keyboard input BR = new BufferedReader (newInputStreamReader (system.in));
String send = Br.readline ();
Sending data Dos.writeutf (send);
}}catch (Exception e) {e.printstacktrace ();
}finally{try {if (Dos!= null) {dos.close ();
} if (br!= null) {br.close ();
} if (client!= null) {client = null;
} catch (IOException e) {e.printstacktrace (); }
}
}
}
Client:
Import Java.io.BufferedReader;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.net.Socket;
public class Client {public static final String IP = "localhost";/server address public static final int port = 8000;//Server port number
public static void Main (string[] args) {handler ();
private static void Handler () {try {//Instantiate a socket and specify server address and port Socket client = new Socket (IP, port);
Open two threads, one responsible for reading and one responsible for writing new thread (new Readhandlerthread (client)). Start ();
New Thread (new Writehandlerthread (client)). Start ();
catch (Exception e) {e.printstacktrace ();
}}/* * The thread that handles the read operation */class Readhandlerthread implements runnable{private Socket client;
Public Readhandlerthread (Socket client) {this.client = client;
@Override public void Run () {datainputstream dis = null;
try {while (true) {//Read server-side data dis = new DataInputStream (Client.getinputstream ()); STring receive = Dis.readutf ();
SYSTEM.OUT.PRINTLN ("The server side returned is:" + receive);
} catch (IOException e) {e.printstacktrace ();
} finally{try {if (dis!= null) {dis.close ();
} if (client!= null) {client = null;
} catch (IOException e) {e.printstacktrace ();
}}/* * The thread that handles the write operation/class Writehandlerthread implements runnable{private Socket client;
Public Writehandlerthread (Socket client) {this.client = client;
@Override public void Run () {DataOutputStream dos = null;
BufferedReader br = null;
try {while (true) {//get output stream dos = new DataOutputStream (Client.getoutputstream ());
System.out.print ("Please enter: \ t");
Keyboard input BR = new BufferedReader (new InputStreamReader (system.in));
String send = Br.readline ();
Sending data Dos.writeutf (send);
} catch (IOException e) {e.printstacktrace ();
} finally{try{if (dos!= null) {dos.close (); } if (br!= null) {
Br.close ();
} if (client!= null) {client = null;
}}catch (Exception e) {e.printstacktrace ();
}
}
}
}