Java multithreading programming socket communication example code

There are many programming examples about Java multithreading communication spread on the network. This one is fairly good, and the code is available. Let's look at the details.

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.

@H_ 404_ 9 @ comparison:

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

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

@H_ 404_ 9 @ application:

1. TCP has strong vitality in network communication. For example, both remote connection (telnet) and file transfer (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. Therefore, 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.

@H_ 404_ 9@ II Java network programming based on socket

@H_ 404_ 9@1. What is 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.

@H_ 404_ 9@2.socket Communication process

The server side listens to whether a port has a connection request, and the client side sends a connect request to the server side, 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. For a fully functional socket, it should include the following basic structure, and its working process includes the following four basic steps:

@H_ 404_ 9@ (1) create a socket; (2) open the input / output stream connected to the socket; (3) read / write the socket according to a certain protocol; (4) close the socket

@H_ 404_ 9@3. 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:

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 represents the maximum number of connections that the server can support.

  Socket client = new Socket("127.0.01.", 80);

  ServerSocket server = new ServerSocket(80);

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, exceptions must be caught or thrown when creating a socket or ServerSocket.

@H_ 404_ 9@ III Server and client code

@H_ 404_ 9@1. First, implement the service class (there are detailed comments in the code, which will not be repeated one by one)

@H_ 404_ 9@2. The thread class of the server responding to the request

@H_ 404_ 9@3. Client request socket class

@H_ 404_ 9@ IV test

@H_ 404_ 9@1 First, start the multisocketserver on the server and wait for the request

@H_ 404_ 9@2. Start the client multiclientsocket, send a request to the server, and get the information returned by the server

@H_ 404_ 9@3. Let's look at the response results of the server

@H_ 404_ 9@ succeeded!

@H_ 404_ 9 @ summary

The above is the sample code of socket communication in Java multithreading programming. I hope it will be helpful to you. Interested friends can continue to refer to this site: one-way communication in the basic chapter of Java network programming, examples of Java using agents for network connection, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support.

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