One: basic knowledge of network programming
basic knowledge of network programming:
IP Address and port number
If computers in a network need to communicate with each other, you must specify an indicator number for each computer, and an identification number to specify the computer that accepts the data and identifies the sending data. The "Mark" in the TCP/IP protocol is the IP address.
A computer class runs multiple network programs at the same time, using an IP address to send data to a computer without being able to guarantee which network program to submit the data to. Therefore, the head of each sent network packet is provided with a "port" section, which is an integer that indicates which application to hand the data frame to. You must also specify a port number for your network program so that different applications accept data on different ports. Multiple programs that use the same port can not exist on the same computer.
The range of ports is between 0~65535, where the port number between the 0~1023 is used for well-known network services and applications, and more than 1024 of the port number is used for ordinary network applications, which avoids the use of the port number on behalf of the network program string. If a network program specifies a port number of 3900, the packet sent by another network program must indicate that the recipient's port number is 3900, and the computer will receive the port number in the packet after the data is reached. TCP and UDP
TCP is the Transmission Control protocol and UDP is the User Datagram protocol.
TCP is a connection-oriented copper protocol that provides reliable data transfer between two computers. When an application uses TCP communication, a virtual connection is established between the data source and the target, and a connection is made, and the data can be interacted as a two-way byte stream between the two computers. The TCP mode of operation is similar to making a phone call, only after confirming that the other person is connected, only then starts the call, and both sides can hear each other's speech content.
UDP is a connectionless communication protocol that does not guarantee the transmission of reliable data, but is capable of delivering data to several destinations. For example, when a host sends data to a B host, the data is immediately emitted, regardless of whether Host B is ready to receive it. Host B's data reception mechanism is similar to that of host a, no matter whether the data is received or not, it is no longer confirmed to host a. UPD operation mode is similar to the pager to send information to the user, the pager itself to the user can receive the message is not controllable, users will not be feedback to the pager to receive the message. Socket Introduction
A socket (socket) is a network-driven layer that provides an interface and mechanism for applications that can be viewed as ports that are created for applications. The task of the application is to place the container (the data sent over the network) on the port of loading, leaving the remaining work to the shipping company (the driver) to handle it. When receiving data, the application also needs to create port terminals, when the cargo (data) arrives, the application takes the goods from the Dock (Socket).
One end of a two-way link is called a socket two: TCP network programming TCP network Overview
Using the TCP protocol to write a network program, you need to provide server-side programs and client programs. Server-side and client program operation process is similar to the relationship between 114 ltddirectory and ordinary phone, the runtime must first have 114 ltddirectory (server-side program), Ordinary phone (client program) to dial, search number. In the 114 ltddirectory (server-side program) there is a switchboard dedicated to the incoming call, but the switchboard does not directly engage in dialogue, but the phone is allocated to the idle extension for processing. When there is no idle time, the customer is prompted to wait for a busy, waiting line, when waiting for the phone to reach a certain number of calls, the switchboard will completely reject the incoming call. The ServerSocket in Java is similar to the 114 Ltddirectory switchboard.
The specific steps of the TCP protocol are as follows:
(1) The server program creates the ServerSocket object and calls the Accept () method to wait for the client to connect.
(2) The client program creates the socket object and requests a connection to the server.
(3) The server receives the client's connection request and creates a new socket object to establish a connection line with the client.
(4) Implement (2), (3) The two sockets in the step to establish the connection in the same thread dialog.
(5) The server waits for the new connection request again. ServerSocket class
TCP network Server program writing, you need to use the ServerSocket class to create a server. The main methods of the ServerSocket class are:
Main methods of Servlersocket class
| Public serversocket (int prot) |
Construction method |
Create a ServerSocket instance |
| Public Socket Accept () |
Method |
Waiting for client connections |
| Public inetaddress getinetaddress () |
Method |
Returns the IP address of the server |
| public boolean isclosed () |
Method |
Returns the shutdown status of the ServerSocket |
| public void Close () |
Method |
Close ServerSocket |
The server side needs to invoke the accept () method to wait for a client connection each time it executes, and the service end will go into a blocking state, knowing that the client is connected again. The return type of the Accept () method is a socket. Socket Class
The client must create a socket object to establish a connection to the server, and each socket object represents a client.
Common methods for socket classes
| Method |
Type |
Describe |
| Public Socket (String host,int Prot) |
Construction method |
Constructs the socket object, specifying the host name and port number to connect to the server |
| Public InputStream getInputStream () |
Method |
Returns the input stream for a socket |
| Public OutputStream Getoutputstream () |
Method |
Returns the output stream of a socket |
| public boolean isclosed () |
Method |
To determine if a socket is closed |
| public void Close () |
Method |
Close this socket |
The relationship between ServerSocket and socket is as shown
information in TCP network programming is passed in the form of an input/output stream.