Complete Guide to basic programming of socket communication in Java

What is a socket? Two programs on the network exchange data through a two-way communication connection. One end of this 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.

During socket communication, 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: (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 (in practical applications, the displayed close is not used. Although many articles recommend it, in my program, it may not have any impact because the program itself is relatively simple and does not have high requirements.)

Create socket 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 indicates the maximum number of connections that the server can support. For example, learning video network http://www.xxspw.com

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.

code

server

client

Server binding IP

When writing socket in C, struct SOCKADDR_ The in structure can specify sin_ addr. s_ Addr, that is, you can specify an IP address. Why is there such a requirement? For example, my network link is as follows:

I may only want to bind the IP address of the network card eth0, because my lo and wlan0 may use one port as the virtual host of nginx. Therefore, when opening the ServerSocket on the server side, there is a need to specify the IP

A constructor of the scheme ServerSocket is as follows:

public ServerSocket(int port,InetAddress bindAddr) throws IOException

Parameters:

Port - local TCP port backlog - listen for backlog bindaddr - InetAddress to bind the server to

Because InetAddress has no constructor, I've been struggling here for a long time. Check the stackoverflow. You can use the getbyname method of InetAddress

Sample code

The concurrent access server side processes the requests of multiple clients at the same time by adding multiple threads. In fact, the implementation is still very water. After all, Java is good enough to encapsulate multiple threads. I implemented the runnable interface with an internal class on the server side, processed the requests of the client in the run method, and printed the data

Server code

The client code is basically unchanged, and an exit operation is added

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