Learning Notes-Network programming 3 (Network programming based on TCP protocol)

Source: Internet
Author: User

TCP Protocol BasicsIP protocol is a key protocol used on the Internet, its full name is Internet Protocol, ie, Internet Protocol, usually referred to as IP protocol. Create a TCP server using ServerSocketBefore the virtual link is established for two communication entities, a communication entity must first make a "proactive gesture" to proactively receive connection requests from other communication entities. The class in Java that can receive connection requests for other communication entities is the Serversocket,serversocket object used to listen for the socket from the client and, if there is no connection, it will remain in the waiting state. The ServerSocket contains a method that listens for requests from client connections. Socket Accpet (): If a connection request to a client socket is received, the method will return a socket corresponding to the client socket, otherwise the method will remain in the waiting state and the thread will be blocked. ServerSocket (int port): Creates a serversocket with the port specified. The port should have a valid port integer value, which is 0~65535. ServerSocket (int port, int backlog): Adds a parameter backlog to change the length of the connection queue. ServerSocket (int port, int backlog, intetaddress localaddr): In the case of multiple IP addresses on the machine, Allows the LOCALADDR parameter to be specified to bind the ServerSocket to the specified IP address. When ServerSocket is finished, use the close () method of ServerSocket to close the ServerSocket. In general, the server should not receive only one client request, but should always receive all requests from the client, so Java programs often call ServerSocket's accept () method through loops. Shown in the following code snippet.
Create a serversocket that listens for connection requests from the client socket ServerSocket SS = new ServerSocket (30000);//uses loops to continuously receive requests from the client while (true) {// Whenever a client socket request is received, the server also generates a socketsocket s = ss.accept ();//The socket can be used to communicate ...}
Tip: When you create a serversocket in the program above without specifying an IP address, the ServerSocket will be bound to the native default IP address. Using 30000 as the port number for the ServerSocket in your program, it is generally recommended to use more than 1024 ports, primarily to avoid conflicts with common ports in other applications. using the socket for communicationThe client can usually use the socket's constructor to connect to the specified server, which typically uses the following two constructors: socket (inetaddress/string remoteaddress, int port): Create a connection to the specified remote host, The socket for the remote port, which does not specify a local address, a local port, defaults to the default IP address of the local host, and uses the system's dynamically specified IP address by default. Socket (inetaddress/string remoteaddress, int port, inetaddress localaddr, int localport):: Creates a socket connected to the specified remote host, remote port, and specify a local IP address and a local port number for situations where the local host has multiple IP addresses. The remote host specified in the above two constructors can be specified using either inetaddress or directly using a string object, but the program typically uses a string object (such as 192.168.1.1) to specify the remote IP address. Using the first method is simpler when the local host has only one IP address. As shown in the following code.
Create a socketsocket s = new socket ("127.0.0.1", 30000) connected to the native, 30000 port, or the socket can be used to communicate ...

Learning Notes-Network programming 3 (Network programming based on TCP protocol)

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.