Java socket Programming (top)

Source: Internet
Author: User
Tags range socket port number

Java Development Network software is very convenient and powerful, the power of Java is derived from his unique set of powerful APIs for the network, these APIs are a series of classes and interfaces, both in the package java.net and javax.net. In this article we will introduce the socket (socket) concept, as well as an example of how to use the network API to manipulate sockets, after completing this article, you can write the network low-end communication software.

What is a socket (socket)?

The network API is typically used to communicate with other programs based on TCP/IP network Java programs, and the network API relies on sockets for communication. The socket can be viewed as an endpoint in a communication connection between two programs, and a program writes a piece of information to the socket that sends this information to another socket so that it can be transferred to another program. As shown in Figure 1

Let's analyze the figure 1,host A to write a piece of information into the socket, the contents of the socket by host a network management software access, and this information through host a network interface card sent to host B,host B network interface card received this information, The network management software that is passed to Host B is stored in the socket of Host B, and then program B can read the information in the socket.

Assuming that the third host C is added to the network in Figure 1, how does host a know that the information is transferred correctly to Host B instead of being routed to host C? Each host on a TCP/IP network is given a unique IP address, which is a 32-bit unsigned integer that is usually separated by a decimal point because it is not converted into binary. For example: 198.163.227.6, as seen in the IP address consists of four parts, each part of the range is 0-255, to represent 8-bit address.

It is noteworthy that the IP address is 32-bit address, this is the IP Protocol version 4 (IPV4), the current due to IPV4 address is nearly depleted, so IPV6 address is gradually replacing Ipv4 address, Ipv6 address is 128-bit unsigned integer.

Assuming that the second program is added to the Host B of the network in Figure 1, how can the information transmitted by host A be correctly passed to program B instead of to the newly added program? This is because each TCP/IP network communication program is given a unique port and port number, and the port is an information buffer that retains the input/output information in the socket, and the port number is a 16-bit unsigned integer range of 0-65535. To distinguish each program on the host (the port number is like the room number in the house), short slogans below 256 are reserved for standard applications, such as POP3 's port number is 110, each socket is combined into the IP address, port, and port number, so that the formation of the whole can be distinguished by each socket T, Let's talk about two kinds of sockets: stream sockets and self addressed data sockets.

Stream socket (stream socket)

Whenever you send and receive information between two network applications, you need to establish a reliable connection. The stream socket relies on the TCP protocol to ensure that the information arrives at the destination correctly, in fact, the IP packet may be lost in the network or in the transmission process error, any situation occurs, as the recipient of the TCP will contact the sender TCP to resend the IP packet. This is known as establishing a reliable connection between two stream sockets.

Stream sockets play a necessary role in the C/S program, and the client program (the network application that needs access to certain services) creates a stream socket object that acts as the IP address of the host of the server program and the port number of the server program (the network application that provides the service for the client application).

The initialization code of the client stream socket passes the IP address and port number to the network management software of the client host. The management software passes the IP address and port number through the NIC to the server-side host, the server-side host reads the data that is passed through the NIC, and then looks at the server program for monitoring status. This monitoring is still done through sockets and ports; If the server program is in the listening state, then the server-side network management software sends a positive response signal to the client network management software, after receiving the response signal, the client stream socket initialization code creates a port number for the client program, And the port number is passed to the server program's socket (the server program will use this port number to identify whether the message is part of the client program) and complete the initialization of the stream socket.

If the server program is not in the listening state, then the server-side network management software will send a negative signal to the client, after receiving this negative signal, the client program's stream socket initialization code will throw an exception object and do not establish a communication connection, nor create a stream socket object. This situation is like making a phone call, when someone's communication is established, otherwise the phone will be suspended.

This part of the work includes three related classes: InetAddress, sockets, and ServerSocket. The InetAddress object depicts a 32-bit or 128-bit IP address, the socket object represents the client stream socket, and ServerSocket represents the service flow socket, all three classes in the package java.net.

Related Article

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.