[BigData] Java programming foundation _socket use multithreading

The use of multi-threading in socket communication, mainly in order to solve when there are communication problems when a socket, resulting in blockage of communication, the entire socket can not function properly, in a wait state, with the multi-threaded, so you can run independent of each socket, without disturbing each other.

The following is a specific implementation case

Client: ClientDemo.java

Package cn.test.logan.day13.ThreadSocket; 

Import a java.io.InputStream;
 Import a java.io.OutputStream;
 Import the java.net.Socket; 

public  class ClientDemo {
     public  static  void main (String [] args) throws Exception {
         the while ( to true ) {
             // issued request to establish a connection to the server of 
            the Socket SC = new new the Socket ( "localhost", 9998 );
             // get the data sent from a tool connected to 
            the OutputStream OUT = sc.getOutputStream (); 
            
            // send The first question
            out.write ( "Who are you?" .getBytes ()); 
            
            // receiving an answer to the question of 
            InputStream in = sc.getInputStream ();
             byte [] b = new new  byte [1024 ];
             int NUM = in. the Read (b); 
            System.out.println ( "first received an answer is:" + new new String (b, 0 , NUM)); 
            
            // send second question 
            out.write ( "How old are you? " .getBytes ());
             // receiving the second answer to the question 
            NUM = in.read (B); 
            System.out.println ( " second received answer is: "+ new new String (B, 0 , NUM ));
            in.close();
            out.close();
            sc.close();
        }
    }
}

Method main server code: ServerDemo.java

package cn.test.logan.day13.ThreadSocket;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 在socket中使用多线程
 * @author QIN
 *
 */
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(9998);
        int i=1;
        while(true) {
            Socket sc =ss.accept (); 
            System.out.println ( "receive section" + i + "client connection" ); 
            SocketDemo socketDemo = new new SocketDemo (SC); 
            the Thread Thread = new new the Thread (socketDemo); 
            Thread.start () ; 
            I ++ ; 
        } 
        
    } 
}

 

 Server code implementation: SocketDemo.java

package cn.test.logan.day13.ThreadSocket;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SocketDemo implements Runnable{
    
    private Socket sc;
    // 定义构造方法
    public SocketDemo(Socket sc) {
        this.sc = sc;
    }
    @Override
    public void run() {
        try {
            // 获得输入输出流
            InputStream in = sc.getInputStream();
            OutputStream out =sc.getOutputStream (); 
            
            byte [] B = new new  byte [1024 ]; 
            
            // receiving a first problem 
            int NUM = in.read (B); 
            System.out.println ( "question received from the client 1:" + new new String (b, 0 , NUM)); 
            
            // send the first question to answer client 
            out.write ( "I'm Logan" .getBytes ()); 
            
            // receiving the second question 
            NUM = in. Read (B); 
            System.out.println ( "question received from the client 2:" + new new String (B, 0 , NUM)); 
            
            // send a second answer client questions to 
            out.write ( " I am 22 years old " .getBytes ()); 
            
            // close connection with the flow
            in.close();
            out.close();
            sc.close();
            
        }catch (Exception e){
            System.out.println("出现异常了......");
        }
        
    }

}

 

 The output Screenshot:

 

Guess you like

Origin www.cnblogs.com/OliverQin/p/12150289.html