Java Socket programming, small cases (annotated)

A, Socket Programming

  In network programming, use the most is the Socket. Like the familiar QQ, MSN uses the Socket technology related.

Second, the case

 1. server

  • ① Create a ServerSocket object and bind listening port
  • ② listen for client requests by the accept () method
  • ③ After the connection is established, the client sends a read request information through the input stream
  • ④ transmission output corresponding information by the client flow
  • ⑤ Close Related Resources
     1 package cn.kgc.sockettest.demo1;
     2 
     3 import java.io.*;
     4 import java.net.ServerSocket;
     5 import java.net.Socket;
     6 
     7 /**
     8  * 服务端
     9  *
    10  * @author
    11  * @create 2019-07-23 14:16
    12  **/
    13 public class server {
    14     public static void main(String[] args) {
    15         try {
    16             ServerSocket serverSocket = new new ServerSocket (8080 );
     17              System.out.println ( "server startup is completed ... start listening!" );
     18              // open listening, waiting for access to the client 
    19              the Socket socket = serverSocket.accept ();
     20              // get the input stream, because the client sends data to the server 
    21 is              the inputStream inputStream = Socket.getInputStream ();
     22 is              // Create a stream buffer 
    23 is              the BufferedReader br = new new the BufferedReader ( new new the InputStreamReader (inputStream));
     24              String info = null ;
     25              the while ((info = br.readLine ())! =null ) {
     26 is                  System.out.println ( "Client server here is:" + info);
     27              }
     28              // respond to the client 
    29              the OutputStream the outputStream = Socket.getOutputStream ();
     30              info = "here a server, we receive your request information processed processing ... "! ;
     31 is              OutputStream.write (info.getBytes ());
     32              outputStream.close ();
     33 is          } the catch (IOException E) {
     34 is              e.printStackTrace ();
     35          }
     36      }
     37 [ }

    2. Client

  • ① create a Socket object indicating the need to connect to the server's address and port number
  • ② After the connection is established, the server sends the output stream want to request information
  • ③ server response information acquired by the input stream
  • ④ closing response resources
    package cn.kgc.sockettest.demo1;
    
    import java.io.*;
    import java.net.Socket;
    
    /**
     * 客户端
     *
     * @author
     * @create 2019-07-23 14:17
     **/
    public class Client {
        public static void main(String[] args) {
            try {
                Socket socket = new Socket("localhost",8080);
                OutputStream outputStream = socket.getOutputStream();
                String info = "你好啊!";
                //Output! 
                OutputStream.write (info.getBytes ()); 
                socket.shutdownOutput (); 
                // response server receiving 
                the InputStream inputStream = Socket.getInputStream (); 
                the BufferedReader br = new new the BufferedReader ( new new the InputStreamReader (inputStream));
                 the while ((info = ! br.readLine ()) = null ) { 
                    System.out.println ( "the received server response!" + info); 
                } 
                // flush the buffer 
                outputStream.flush (); 
                outputStream.close (); 
                inputStream.close (); 
                the Socket.close (); 
            } the catch(IOException e) { 
                e.printStackTrace (); 
            } 
        } 
    }

    Then, the first implementation of the server, and then execute the client code on it. This is just a small case, what deficiencies, please advise!

Guess you like

Origin www.cnblogs.com/Cavalry-/p/11233862.html