Java Core API--(Socket)

Source: Internet
Author: User

1. Socket Related Concepts

1) network communication model: C/s:client/server, client/server side, B/s:browser/server, browser-side/server-side, the advantages of C/s structure: The application of strong pertinence, beautiful picture, the application of complex functions. Cons: Difficult to maintain. Advantages of B/s structure: easy to maintain. Disadvantage: Poor effect, not strong interactivity.

2) Socket: Encapsulates the local address, service port and other information. ServerSocket: The socket on the server side.

Server: Use ServerSocket to listen to the specified port, the port can be arbitrarily specified (because the port below 1024 is usually reserved port, in some operating systems is not free to use, it is recommended to use a port greater than 1024), waiting for the client connection request, after the client connection, the session generated After completing the session, close the connection.

Client: Use the socket to make a connection request to one of the servers on the network, and once the connection is successful, the session is opened and the socket is closed after the session is complete. The client does not need to specify an open port, typically a temporary, dynamic allocation of more than 1024 ports.

3) will always be the socket to actively connect serversocket. A serversocket can receive a number of connections to a socket. The premise of network communication: Be sure to catch exceptions.

4) The socket connection is based on the TCP/IP protocol and is a long connection (long-time attached).

5) Read server information will be blocked, write operation will not.

6) Establish the connection and send the information to the server step: ① connect to the server via the server's address and port, and the above two data is required to create the socket. After the ② connection is successful, the input stream and output stream can be obtained through the socket, and the input stream is used to receive the information sent by the server. ③ Close the connection.

7) Connect the server: Once the socket is instantiated, it begins to attempt to connect to the server with the given address and port number (automatic). The address "localhost" here is the address of the server, and Port 8088 is the server's external port. Our own ports are system-assigned, and we don't need to know.

8) communicate with the server (read and write data): Use getInputStream () in the socket to get the input stream and use Getoutputstream () to get the output stream.

9) The ServerSocket construction method requires us to pass in an open port number, and the ServerSocket object is created to request that the port be opened to the operating system.

10) by calling ServerSocket's Accept method, the server side begins to wait for a connection from the receiving client. This method is a blocking method that listens on the specified port for a client connection. The method does not end until a client connects to it and receives a client socket.


2. Basic socket Network Programming--Server and client simple implementation

Case 37:

650) this.width=650; "style=" width:672px;height:548px; "title=" clipboard 1.png "src=" http://s3.51cto.com/wyfs02/ M02/72/88/wkiom1xljkpde1dvaancxym7fuc379.jpg "width=" 689 "height=" 551 "alt=" wkiom1xljkpde1dvaancxym7fuc379.jpg "/ >

650) this.width=650; "style=" WIDTH:670PX;HEIGHT:509PX; "title=" clipboard 2.png "src=" http://s3.51cto.com/wyfs02/ M02/72/88/wkiom1xljlvgbjsiaal8x2fw32o792.jpg "width=" 699 "height=" 527 "alt=" wkiom1xljlvgbjsiaal8x2fw32o792.jpg "/ >

This article is from "Forever Young" blog, please be sure to keep this source http://kingkongzhao.blog.51cto.com/6319491/1690571

Java Core API--(Socket)

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.