Simple Java TCP network programming example
Start the Server first and enter an endless loop so that you can monitor whether a port has connection requests. Then run the Client, and the Client sends a connection request. After listening to this request, the server sends a message to the Client to receive the message, establishes the connection, and starts a thread to process the request, then listen to other requests in an endless loop. After the client inputs a string, press enter to send data to the server. The server reads the data and replies to the client. After the request is processed, the starting thread disappears. If the client receives data outside of "OK", it will send a connection request again and send data. The server will start a thread for the connection again to respond... The client exits when the returned data received by the client is "OK.
Server code
Import java. io. bufferedReader; import java. io. dataInputStream; import java. io. dataOutputStream; import java. io. inputStreamReader; import java.net. serverSocket; import java.net. socket; public class TCPServer {public static final int PORT = 12345; // The listening PORT number public static void main (String [] args) {System. out. println ("server startup... \ n "); TCPServer server = new TCPServer (); server. init ();} public void init () {tr Y {ServerSocket serverSocket = new ServerSocket (PORT); while (true) {// once there is a blockage, it indicates that the server and the client have obtained the connection Socket client = serverSocket. accept (); // process the connection new HandlerThread (client) ;}} catch (Exception e) {System. out. println ("server exception:" + e. getMessage () ;}} private class HandlerThread implements Runnable {private Socket socket; public HandlerThread (Socket client) {socket = client; new Thread (this ). star T ();} public void run () {try {// read client data DataInputStream input = new DataInputStream (socket. getInputStream (); String clientInputStr = input. readUTF (); // The write method corresponding to the output stream of the client. Otherwise, the System of the client data will be thrown. out. println ("received by the server:" + clientInputStr); // return the message DataOutputStream out = new DataOutputStream (socket. getOutputStream (); System. out. println ("Server Input:"); // send a line of String s = new Bu entered by the keyboard FferedReader (new InputStreamReader (System. in )). readLine (); out. writeUTF (s); out. close (); input. close ();} catch (Exception e) {System. out. println ("Server run exception:" + e. getMessage ();} finally {if (socket! = Null) {try {socket. close ();} catch (Exception e) {socket = null; System. out. println ("server finally exception:" + e. getMessage ());}}}}}}
Client code
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 TCPClient {public static final String IP_ADDR = "localhost"; // The server address is public static final int PORT = 12345; // server port number public static void main (String [] args) {System. out. println ("client startup... "); System. out. println ("When the received server character is \" OK \ ", the client terminates \ n"); while (true) {Socket socket = null; try {// create a stream socket and connect it to the specified PORT number Socket = new socket (IP_ADDR, PORT) on the specified host ); // read the server data DataInputStream input = new DataInputStream (socket. getInputStream (); // send data to the server DataOutputStream out = new DataOutputStream (socket. getOutputStream (); System. out. println ("client input:"); String str = new BufferedReader (new InputStreamReader (System. In )). readLine (); out. writeUTF (str); String ret = input. readUTF (); System. out. println ("received by the Client:" + ret); // Disconnect if ("OK ". equals (ret) {System. out. println ("the client will close the connection"); Thread. sleep (500); break;} out. close (); input. close ();} catch (Exception e) {System. out. println ("client exception:" + e. getMessage ();} finally {if (socket! = Null) {try {socket. close ();} catch (IOException e) {socket = null; System. out. println ("client finally exception:" + e. getMessage ());}}}}}}
Test Results