How to realize multithreading of socket network programming

  • 2020-06-15 10:02:03
  • OfStack

This article mainly introduces how to realize the multithreading of socket network programming, to share with you

Basic introduction of TCP, UDP and IP addresses:

TCP

It's a reliable connection. Reliable means that there must be a clear connection object, just like a phone call, the number dialed must be connected to someone in the service to establish the network session.

UDP:

An unreliable connection. Unreliable means that you are not sure if the conversation will actually be delivered to the object you want to connect to. It is like sending express mail.

IP address:

The id card of the computer. The first six digits of the ID number are the address code (you can know which province, city and county it is), followed by the eight-digit number is the birth date code, as is the IP address, which is composed of the network address (determine which network) and the host address (which host in the network).

Local address: localhost is equivalent to 127.0.0.1

The following example is based on the TCP protocol.


public class Client {
 public static void main(String[] args) throws Exception{

  // Open the 1 Socket, ready to send the request 
  Socket socket = new Socket("localhost", 9996);// You have to establish a connection, which is called TCP Is to establish a reliable connection 

  System.out.println(" dear VIP Customer, the request data team is ready, please enter your request: ");

  // Sends data to the server with a client request that is read from the keyboard and sent to the server PrintWriter Form packing 
  PrintWriter out =new PrintWriter(socket.getOutputStream(),true);

  // The input stream getInputStream() Alone, open 1 A thread in the Receive Class to complete receiving data from the server 
  Thread t = new Thread(new Receive(socket));
  t.start();

  Scanner scanner =new Scanner(System.in);// Keyboard input 
  while(scanner.hasNextLine()){
   out.println(scanner.nextLine());// To send data from the keyboard 
  }

 }
}

public class Receive implements Runnable {
 private Socket socket;
 public Receive(Socket socket) {
  this.socket=socket;
 }

 @Override
 public void run() {
  try {
   Scanner scanner =new Scanner(socket.getInputStream());// Receive data 
   String str =null;
   while(true){
    str=scanner.nextLine();
    System.out.println(" The server says: "+str);// Print the received data 
   }
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

}

public class Server {
 public static void main(String[] args) throws Exception{

  ServerSocket server = new ServerSocket(9996);// In the port 9996 open 1 The service listens to the request sent by the client and prepares to receive the request data sent by the client 

  System.out.println(" Server ready, ready to receive requests! ");

  Socket socket =server.accept();// It returns only when a client requests and connects 1 a Socket Object, which is the one with the client request Socket object 


  // create 1 a PrintWriter Instance object of out To accomplish the task of sending data from the server to the client, 
  PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

  // Separate open 1 Three threads receive requests from the client, in Receive1 Class to complete the receiving of data 
  Thread t = new Thread(new Receive1(socket));
  t.start();

  /*1 Need to understand Socket is 1 Three classes for communication between machines */

  // Send data to the client 
  Scanner scanner = new Scanner(System.in);// Keyboard input server to send data to the client 
  while(scanner.hasNextLine()){
   out.println(scanner.nextLine());// Encapsulate the data that the keyboard input server wants to send to the client PrintWriter Class object 
  }
 }
}

public class Receive1 implements Runnable {
 private Socket socket;
 public Receive1(Socket socket) {
  this.socket=socket;// Calls are required to receive data from the client on the server side Receive1() So this method, you're going to get 1 a Socket Object that carries the client request socket
 }

 @Override
 public void run() {
  try {
   Scanner scanner=new Scanner(socket.getInputStream());// Gets the input stream that the client receives from the keyboard 
   String str = null;
   while(true){
    str=scanner.nextLine();
    System.out.println(" Data sent by the client: "+str);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }// Receive data 
 }

}

Related articles: