The first chapter website basics in Chapter 4 Java Socket usage

Chapter 4 Java usage in Socket

4.1 Socket common usage

  Java network communication is achieved through Socket, Socket into ServetSocket and Socket two categories, ServetSocket for the server , the listener can accept a request by a process of, after listening to the request returns Socket, Socket for a particular data transmission is completed, Socket client directly initiation request and transmit data.

  ServerSocket use can be divided into three steps:

  1) Create a ServerSocket. ServerSocket constructor a total of five, with the most convenient is ServerSocket (int port), requires only one port (port number) on it.
  2) call creates out of ServerSocket method accept listening. accept method is a blocking method, the method that is invoked accept the program will stop and wait for a connection request, the program will not go down until the request is received, after receiving the request accept method returns a Socket.
  3) Socket accept method returns to communicate with the client.

  The service uses examples:

public  class Server {
     / ** 
     * After the ServerSocket for the server, the listener can accept the request by the method, the monitored request returns Socket, 
     * Socket for a particular data transfer is complete, the client directly using the Socket initiation request and transmit data. 
     * @Param args
      * / 
    public  static  void main (String [] args) {
         the try {
             // create a listening port 8080 ServerSocket 
            ServerSocket Server = new new ServerSocket (8080 );
             // wait request 
            the Socket Socket = server.accept ();
             / / using socket communication after receiving the request, for reading data creating BufferedReader 
            BufferedReader IS = new new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String line = is.readLine();
            System.out.println("received from client:"+line);
            //创建PrintWriter,用于发送数据
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            pw.println("received data:"+line);
            pw.flush();
            pw.close();
            is.close();
            socket.close();
            server.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
View Code

  Clients use examples:

public  class Client {
     / ** 
     * msg after starting automatically sent to the server, and then receiving the data returned from the server and print to the console, and finally closes the connection release resources 
     * @param args
      * / 
    public  static  void main (String [] args) {
         the try { 
            String MSG = "Client Data" ;
             // Create a socket, port 8080 is connected with the machine 
            Socket Socket = new new Socket ( "127.0.0.1", 8080 );
             // use Socket and create PrintWriter BufferedReader data read and write 
            the PrintWriter PW = new new the PrintWriter (Socket.getOutputStream ()); 
            BufferedReader IS= New new the BufferedReader ( new new the InputStreamReader (Socket.getInputStream ()));
             // send data 
            pw.println (MSG); 
            pw.flush (); 
            // receive data 
            String = Line is.readLine (); 
            System.out.println ( "reveived from Server:" + Line);
             // Close the resource 
            pw.close (); 
            is.close (); 
            the Socket.close (); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
        
    } 

}
View Code

4.2 NioSocket usage

  nio (new IO) and provides ServerSocketChannel SocketChannel, which correspond to the original ServerSocket and Socket.

Buffer、Channel和Selector

  Buffer: I want to send the goods;

  Channel: courier (with or truck bound for an area);

  Selector: Sorter transit station;  

  NioSocket use first create ServerSocketChannel, then register Selector, then you can receive a request with Selector and processed.

  ServerSocketChannel can use your own static factory method to create open. Each ServerSocketChannel corresponds to a ServerSocket, you can call its socket method to get, but if used directly to obtain the ServerSocket to listen for the request, and that was the original processing mode, usually to bind the port to use to obtain the ServerSocket. ServerSocketChannel can configureBlocking way to set whether to adopt the blocking mode, if you want to adopt non-blocking mode can be used configureBlocking (false) can be set after the call, set up a non-blocking mode register method to use a registered Selector ( blocking mode can not be used Selector ).

 

Guess you like

Origin www.cnblogs.com/nachdenken/p/11285555.html