Java learning network programming (1)

This blog post will continue to be updated...

Data transmission between client and server

Network applications use the client and server modes to communicate. The data transmission between the client and the server is divided into three steps: connection, transmission, and reception . The server first starts the service and waits, the client application actively initiates communication and applies for a connection, while the server application passively waits for communication. After the connection is successful, the client can send or request data to the server, and the server makes a request for the client. Make the corresponding response.

For the connection and data transmission and reception between the client and the server, a special API provides a series of methods to realize the process, namely the socket API. The socket API is the application program interface (application and operating system The interface between communication protocols is called an application program interface (API) in a set of processes for network communication.

Some of the methods specifically included in the socket API are as follows:

socket():创建socket

bind():绑定socket到本地地址和端口,通常由服务端调用

listen():TCP专用,开启监听模式

accept():TCP专用,服务器等待客户端连接,一般是阻塞态

connect():TCP专用,客户端主动连接服务器

send():TCP专用,发送数据

recv():TCP专用,接收数据

sendto():UDP专用,发送数据到指定的IP地址和端口

recvfrom():UDP专用,接收数据,返回数据远端的IP地址和端口

closesocket():关闭socket

For a detailed description of the API, please refer to ( https://www.cnblogs.com/yuqiao/p/5786427.html )

Here is an explanation of the process: the
client first calls the process gethorstbyname to convert the computer into an IP address, and then calls getprotobyname to convert the protocol name to the internal binary form used in the socket process; then creates a socket when calling socket , Call connect to establish a connection with the server (calling connect will block until TCP can establish a connection); once the connection is successful, the client repeatedly calls recv to receive information from the server until all the data is read, this This repeats until the client gets an end-of-file mark (ie a set of 0) and stops; finally, when the message is received, close is called to close the socket.

The server first calls the procedure getprotobyname to generate the internal binary flag of the protocol; then calls socket to create a socket, then calls bind to specify a port number of this protocol to the socket, and calls listen to put the socket in passive mode; this When the server enters an infinite loop, and then calls accept to receive the next request (most of the time the server is suspended at access until the request arrives), and sends information to the client by calling send; finally, calling close to terminate the connection. After closing the connection, the server then calls accept to read the next connection, knowing that the information has been sent.

The most important related classes of Java sockets

class description
Socket The communication endpoint, which represents a socket of TCP, through which the client program uses the TCP protocol to connect to the server
ServerSocket Communication endpoint through which the service program receives client connection requests
DatagramSocket Represents a socket of UDP, used to send or receive UDP packets
DatagramPacket Represents a UDP datagram
InetAddress Represents an Internet address

Data transmission method

TCP data transmission

TCP is a method of stream-based network communication. TCP sockets are divided into two types: client (Socket) and server (ServiceSocket).

The client socket is bound to any free port on the remote computer and connected to a specific server port and host (which can be the local machine). When the socket is created, a connection is established with the host and port. TCP sockets only communicate between no more than two computers. When there are more than two computers communicating, multiple socket connections should be established to establish a connection for each computer. When the socket establishes a connection with another computer, it needs to bind the IP and port number of the remote computer. The socket of the local computer will be bound to a local IP address and port number.

The server socket is bound to a specific port on the local computer, so that location services can be implemented. The server socket can respond to multiple client requests. Once the server's socket is created, it will bind the local port and then wait to receive incoming connections. When a client tries to connect, it is inserted into the queue, and when the queue is full, other clients' connections are rejected. Use accept() method to receive connection request

UDP data transmission

UDP is a transmission protocol based on user datagrams. The source and the terminal do not establish a connection before transmitting the data. When it wants to transmit, it simply grabs the data from the application and throws it on the network as quickly as possible. On the sending end, the speed of UDP transmission of data is only limited by the speed at which the application generates data, the capacity of the computer, and the transmission bandwidth; on the receiving end, UDP puts each message segment in the queue, and the application removes it from the queue every time Read a message segment.

For more details about the difference between TCP and UDP, please refer to: ( https://blog.csdn.net/laoniu_c/article/details/39237415 )

Guess you like

Origin blog.csdn.net/dypnlw/article/details/82706869