Server side:
1. Create ServerSocket object and bind listening port;
2. Listen to the client request through the accept() method;
3. Read the request information sent by the client through the input stream after the connection is established;
4. Send response information to client through output;
package com.wxd.socket; import java.io.*; import java.net.ServerSocket; import java.net.Socket; /** * Socket communication based on TCP protocol realizes user login and server */ public class Server { public static void main(String[] args) { try { //Server side //1,Create a server Socket,I.e ServerSocket,Specify the bound port and listen for it ServerSocket serverSocket =new ServerSocket(10086);//1024-65535 A port of //2,call accept()Method to start listening and wait for the client to connect Socket socket = serverSocket.accept(); //3,Get the input stream and read the client information InputStream is = socket.getInputStream(); InputStreamReader isr =new InputStreamReader(is); BufferedReader br =new BufferedReader(isr); String info =null; while((info=br.readLine())!=null){ System.out.println("I am the server, and the client says:"+info); } socket.shutdownInput();//Close input stream //4,Get the output stream in response to the client's request OutputStream os = socket.getOutputStream(); PrintWriter pw = new PrintWriter(os); pw.write("Welcome!"); pw.flush(); //5,close resource pw.close(); os.close(); br.close(); isr.close(); is.close(); socket.close(); serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } }
Console output:
I am the server, and the client says: user name: admin; password: 123
client:
1. Create a socket object, indicating the server address and port number to be connected;
2. After establishing the connection, send the request information to the server through the output;
3. Obtain the response information of the server through the input stream;
4. Close related resources;
package com.wxd.socket; import java.io.*; import java.net.Socket; /** * Socket communication based on TCP protocol and Realization of client */ public class Client { public static void main(String[] args) { try { //client //1,Create client Socket,Specify server address and port Socket socket =new Socket("localhost",10086); //2,Get the output stream and send information to the server OutputStream os = socket.getOutputStream();//Byte output stream PrintWriter pw =new PrintWriter(os);//Wrap the output stream as a print stream pw.write("user name: admin;Password: 123"); pw.flush(); socket.shutdownOutput(); //3,Get the input stream and read the response information on the server side InputStream is = socket.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String info = null; while((info=br.readLine())!=null){ System.out.println("I am the client and the server says:"+info); } //4,close resource br.close(); is.close(); pw.close(); os.close(); socket.close(); }catch(IOException e){ e.printStackTrace(); } } }
Console output:
I am the client, the server said: Welcome!
Process finished with exit code 0