JAVA Socket Programming Learning Note (i)

Source: Internet
Author: User

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)

    1. Instantiates a Seversocket object with the specified port. The server can use this port to listen for connection requests sent from the client.
    2. Call the Accept () method of serversocket to cause blocking while waiting for the connection to listen for connection requests from the port.
    3. Operation of Read/write IO using the client socket object returned by the Accept method
    4. 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

    1. Instantiate the socket object with the server's IP address and port number.
    2. Call the Connect method to connect to the server.
    3. Obtains the stream on the socket, encapsulates the stream into an instance of Bufferedreader/printwriter for reading and writing
    4. Use the getInputStream and Getoutputstream methods provided by the socket to send the data stream to the server via an IO stream object
    5. 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)

    1. Server-side Create ServerSocket, loop call accept () Wait for client connection
    2. Client creates a socket and requests and server-side connections
    3. Server-side Accept client requests, create sockets to establish a leased line connection with the customer
    4. Connect two sockets dialog on a separate thread
    5. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.