Introduction to basic knowledge of socket network programming in Java

1、 Introduction to TCP / IP

TCP / IP protocol family is a protocol used in the Internet, and can also be used in independent private networks. TCP / IP protocol family includes IP protocol, TCP protocol and UDP protocol.

IP protocol uses IP address to distribute messages, but it is a best effort service. Messages may be lost, out of order or sent repeatedly. TCP and UDP protocols add port numbers on the basis of IP protocol, so as to establish a transparent connection between the applications of the two hosts.

The difference is that the TCP protocol will repair the errors in the IP layer. It establishes a connection between hosts through handshaking messages, and then restores the errors in the messages by adding a serial number to the messages. UDP simply extends the IP protocol so that it can work between applications, not between hosts.

For IP addresses, a host can have multiple network interfaces, and an interface can have multiple addresses. Some IP addresses have special purposes:

A. Loopback address: 127.0 0.1 is always assigned to a loopback interface, mainly for testing.

B. Private address: 10, 192.168, 172 (16-31) for private networks. When the NAT device forwards the message, it maps the private address port pair of the message in one interface to the public address port pair in another interface. This enables a small group of hosts to share an IP address pair.

C. Multicast address: the first number is between 224 and 239.

2、 Socket Foundation

1. Access to address

2. TCP instance program

It should be noted that although only one write () method is used on the client side to send the string, the server side may also accept the information from multiple blocks. Even if the feedback string is stored in a block when the server returns, it may be divided into multiple parts by the TCP protocol.

TCPEchoClientTest. java

TCPEchoServerTest. java

Note that the new socket specifies the port number monitored by the remote server instead of the local port, so the default address and available port number will be used. On my machine, the client port is 4593 and connected to port 7 of the server.

3. UDP instance program

Why use UDP protocol? If the application only exchanges a small amount of data, the establishment phase of TCP connection needs to transmit at least twice the amount of information (and twice the round-trip time).

UDPEchoClientTest. java

UDPEchoServerTest. java

Comparing this example with the previous TCP examples, the differences are as follows:

A. The datagram socket does not need to specify a destination address when it is created, because UDP does not need to establish a connection, and each data message can be sent or received at a different destination address.

B. If you block and wait on read() like TCP, it may be blocked there forever, because UDP protocol simply extends IP protocol, and UDP packets may be lost. So be sure to set the timeout of blocking waiting.

C. UDP protocol retains the boundary information of the message. Each receive () call can only receive the data sent by the send () method call at most once.

D. The maximum data that can be transmitted by a UDP message datagram packet is 65507 bytes. The excess bytes will be discarded automatically, and there is no prompt for the receiving program. Therefore, the cache array can be set to about 65000 bytes, which is safe.

E. If the receive () method is called repeatedly using the same datagram packet instance, the internal length of the message must be explicitly reset to the actual length of the cache before each call.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>