Analysis of java socket based programming related knowledge

1、 Two main problems in network programming

One is how to accurately locate one or more hosts on the network, and the other is how to reliably and efficiently transmit data after finding the host.

In TCP / IP protocol, IP layer is mainly responsible for the positioning of network hosts and the routing of data transmission. A host on the Internet can be uniquely determined by IP address.

The TCP layer provides application-oriented reliable (TCP) or unreliable (UDP) data transmission mechanism, which is the main object of network programming. Generally, it does not need to care about how the IP layer processes data.

At present, the more popular network programming model is client / server (C / s) structure. That is, one of the communication parties acts as a server to wait for and respond to the customer's request. The customer applies to the server when the service is needed. The server generally runs as a daemon and listens to the network port. Once there is a customer's request, it will start a service process to respond to the customer. At the same time, it will continue to listen to the service port to make subsequent customers Households can also get services in time.

2、 Two types of transport protocols: TCP \ UDP

TCP is the abbreviation of tranfer control protocol. It is a connection oriented protocol to ensure reliable transmission. Through TCP protocol transmission, we get a sequential error free data stream. A connection must be established between the two paired sockets of the sender and the receiver in order to communicate on the basis of TCP protocol, When a socket (usually a server socket) is waiting to establish a connection, the other socket can request a connection. Once the two sockets are connected, they can carry out two-way data transmission, and both sides can send or receive.

UDP is the abbreviation of user datagram protocol. It is a connectionless protocol. Each datagram is an independent information, including a complete source address or destination address. It is transmitted to the destination by any possible path on the network. Therefore, whether it can reach the destination, the time to reach the destination and the correctness of the content cannot be guaranteed.

Comparison:

UDP: 1. The complete address information is given in each datagram, so there is no need to establish a connection between the sender and the receiver. 2. DP has a size limit when transmitting data, and each transmitted datagram must be limited to 64KB. 3. DP is an unreliable protocol. The datagrams sent by the sender do not necessarily arrive at the receiver in the same order

TCP: 1. For the protocol to connect, a connection must be established before data transmission between sockets, so the connection time is required in TCP. 2. The size of CP transmission data is limited. Once the connection is established, the sockets of both parties can transmit large data in a unified format. 3. CP is a reliable protocol, which ensures that the receiver completely and correctly obtains all data sent by the sender. Application:

1. TCP has a strong vitality in network communication. For example, telnet and FTP require data of variable length to be reliably transmitted. However, reliable transmission has to pay a price. The verification of the correctness of data content will inevitably occupy the processing time of the computer and the bandwidth of the network. Therefore, the efficiency of TCP transmission is not as high as UDP.

2. UDP is easy to operate and requires less monitoring, so it is usually used for client / server applications in decentralized systems with high reliability of LAN. For example, the video conference system does not require the audio and video data to be absolutely correct, as long as the consistency is guaranteed. In this case, it is obviously more reasonable to use UDP.

3、 Java network programming based on socket

1. Is it a socket

Two programs on the network exchange data through a two-way communication connection. One end of the two-way link is called a socket. Socket is usually used to connect the client and the server. Socket is a very popular programming interface of TCP / IP protocol. A socket is uniquely determined by an IP address and a port number.

However, TCP / IP is not the only protocol supported by socket, so there is no inevitable relationship between them. In the Java environment, socket programming mainly refers to network programming based on TCP / IP protocol.

2. Process of socket communication

The server side listens to whether a port has a connection request. The client side sends a connect request to the server side, and the server side sends an accept message back to the client side. A connection is established. Both the server side and the client side can communicate with each other through send, write and other methods.

A fully functional socket must contain the following basic structure, and its working process includes the following four basic steps:

(1) create a socket;

(2) open the input / output connected to the socket;

(3) read / write the socket according to a certain protocol;

(4) close the socket

3. Create a socket

Create socket

Java in package Java Net provides two classes socket and ServerSocket, which are used to represent the client and server of two-way connection respectively. These are two well encapsulated classes, which are very convenient to use. The construction method is as follows:

  Socket(InetAddress address,int port);

  Socket(InetAddress address,int port,boolean stream);

  Socket(String host,int prot);

  Socket(String host,int prot,boolean stream);

  Socket(SocketImpl impl)

  Socket(String host,InetAddress localAddr,int localPort)

  Socket(InetAddress address,int localPort)

  ServerSocket(int port);

  ServerSocket(int port,int backlog);

  ServerSocket(int port,int backlog,InetAddress bindAddr)

Where address, host and port are the IP address, host name and port number of the other party in the two-way connection respectively, stream indicates whether the socket is a stream socket or a datagram socket, and localport indicates the port number of the local host, Localaddr and bindaddr are the addresses of the local machine (the host address of ServerSocket). Impl is the parent class of socket, which can be used to create both ServerSocket and socket. Count indicates the maximum number of connections that the server can support. For example:

  Socket client = new Socket(“127.0.0.1”,8888);

  ServerSocket server = new ServerSocket(8888);

Note that you must be careful when selecting ports. Each port provides a specific service. Only when the correct port is given can the corresponding service be obtained. The port numbers from 0 to 1023 are reserved by the system. For example, the port number of HTTP service is 80, the port number of Telnet service is 21, and the port number of FTP service is 23. Therefore, when selecting the port number, it is best to select a number greater than 1023 to prevent conflicts.

If an error occurs when creating a socket, an IOException will be generated, which must be handled in the program. Therefore, you must catch or throw an exception when creating a socket or ServerSocket.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>