1. Introduction and model of Socket communication
The Java Socket enables bidirectional real-time communication between the client-server. The two class sockets and ServerSocketdefined in the java.net package are used to implement both the client and server side of the two-way connection.
2. Socket Communication Implementation Method
2.1 Server-side (non-threaded)
- Instantiates a Seversocket object with the specified port. The server can use this port to listen for connection requests sent from the client.
- Call the Accept () method of serversocket to cause blocking while waiting for the connection to listen for connection requests from the port.
- Operation of Read/write IO using the client socket object returned by the Accept method
- To close open streams and socket objects
/** * Socket communication based on TCP protocol, implement user login, service side *///1, create a server-side socket, that is, ServerSocket, specify the bound port, and listen to this port serversocket ServerSocket = Newserversocket (10086);//1024-65535 a port//2, call the Accept () method to start listening, waiting for the client connection socket socket = serversocket.accept (); 3. Get the input stream and read the client information InputStream is = Socket.getinputstream (); InputStreamReader ISR =newinputstreamreader (IS); BufferedReader BR =newbufferedreader (ISR); String info =null; while ((Info=br.readline ())!=null) {System.out.println ("Hello, I am the server, client says:" +info);} socket.shutdowninput ();//Close input stream 4. Get output stream, respond to client request OutputStream OS = Socket.getoutputstream (); PrintWriter pw = new PrintWriter (OS); Pw.write ("Hello world! "); Pw.flush ();
2.2 Client
- Instantiate the socket object with the server's IP address and port number.
- Call the Connect method to connect to the server.
- Obtains the stream on the socket, encapsulates the stream into an instance of Bufferedreader/printwriter for reading and writing
- Use the getInputStream and Getoutputstream methods provided by the socket to send the data stream to the server via an IO stream object
- Closes the open stream and socket.
Client//1, create client socket, specify server address and port socket socket =newsocket ("127.0.0.1", 10086); 2. Get output stream, send information to server side outputstream OS = Socket.getoutputstream ();//byte output stream printwriter PW =newprintwriter (OS);// Wraps the output stream into a print stream Pw.write ("username: admin; Password: admin"); Pw.flush (); Socket.shutdownoutput (); 3. Get the input stream and read the server-side response information InputStream is = Socket.getinputstream (); BufferedReader br = new BufferedReader (new InputStreamReader (IS)); String info = null; while ((Info=br.readline ())!null) { System.out.println ("Hello, I am the client, server says:" +info);}
2.2 Server-side (multi-threaded)
- Server-side Create ServerSocket, loop call accept () Wait for client connection
- Client creates a socket and requests and server-side connections
- Server-side Accept client requests, create sockets to establish a leased line connection with the customer
- Connect two sockets dialog on a separate thread
- Server side continues to wait for new connections
The server thread handles the socket socket socket =null associated with this thread; Public serverthread (socket socket) {this.socket = socket;} ServerSocket serversocket =newserversocket (10086); Socket socket =null; int Count =0;//records the number of clients while (true) {socket = serverscoket.accept (); Serverthread serverthread =newserverthread (socket); Serverthread.start ();
References:
Http://developer.51cto.com/art/201509/490775.htm
http://blog.csdn.net/benweizhu/article/details/6615542/
Http://www.blogjava.net/Reg/archive/2010/07/17/326392.html
Java tcp/ip+ Socket Programming
JAVA Socket Programming Learning Note (i)