cool hit counter Beginner's Section 72 Network Programming - ServerSocket (I)_Intefrankly

Beginner's Section 72 Network Programming - ServerSocket (I)


Highlights

introduction

The last section explains the basic use of sockets, this section will talk about one of the important options of sockets, haha, more or less in the future development to use. It still works better.

public void setsolingen( boolean on, int seconds)

Set this option: public void setsolingen( boolean on, int seconds) throws Socketexception

After each socket call to the close method, the underlying socket is not actually closed immediately by default, but will wait for the remaining data to be sent before actually closing the underlying socket and breaking the link to the server.

So now what if I need to close the underlying socket immediately after calling the socket,close method?

This is the time to use this method to socket. setsolinger(true, 0) is set and the socket's close method is executed to close the underlying socket immediately, but all remaining data not sent is discarded.

It is worth noting that in the above case, when the closed method returns, the underlying socket will be closed and disconnected. In addition, the seconds parameter in the setsolinger( boolean on, int seconds) method is in seconds, not milliseconds.

Code demonstration

Server-side Code

Client code

running result

This will force the underlying socket to close and disconnect the server. If you need to wait for the data to be released before disconnecting, you can also set it to: setsolinger(true,2000), the second parameter is in seconds, which means it will block 2000 seconds before closing the underlying socket and disconnecting the server. You can also remove this method is also will default to wait for the data to be sent before closing the link. You can test it out for yourself.

ServerSocket

In the client/server communication mode, the server needs to create a Serversocket that listens to a specific port, and the Serversocket is responsible for receiving client connection requests. java provides a ServerSocket class to represent a server socket. The server socket runs on the server and listens for inbound ftp connections. Each server socket listens to a specific port on the server. When a client on the remote annotation tries this port, the server is woken up, negotiates to establish a connection between the client and the server side, and returns a regular Socket object representing a socket between the 2 hosts. It also means that when the server-side socket receives a connection sent from the client-side socket, the server-side generates a regular socket object for sending data to the client, and the data is always transferred through the regular socket.

ServerSocket Life Cycle

The basic lifecycle of a ServerSocket server consists of the following:

1.Use a ServerSocket() constructor to create a new ServerSocket object on a specific port.

2.ServerSocket uses his accept() method to listen for inbound connections on this port. accept will block until a client attempts to establish a connection with the server, at which point accept will return a connected client and server Socket object.

3.Depending on the type of server, the getInputStream or getOutputStream method, or both, of the Socket object is called to get the input and output streams for client communication.

4.The server and client interact according to the protocol that has been negotiated until the connection is to be closed.

5.The server or client closes the connection.

5 The server returns to step 2 accept and waits for the next connection

Construction method

Creates a server socket bound to a specific port.

Create a server socket with backlog as the length of the connection request queue.

Create a server with the specified port, listening backlog, and local IP address to bind to.

Create unbound server sockets.

ServerSocket(int port)

Creates a server socket bound to a specific port.

this one Construction method It's already been used earlier, See the grammar below。

ServerSocket server = new ServerSocket(10000);

A few points to note

1. Port numbers should never be specified for ports that are already occupied by server processes

2. Try not to specify a port between 1-1023, because in some operating systems the operating system refuses to bind ports between 1-1023 if the service is not run as an administrator.

3. If you set the port to 0, it is an anonymous port, which means that the system will assign a random port, and it is generally not recommended to use it this way. And the anonymous port is has a special meaning and use of the chart, which will be explained later.

ServerSocket(int port, int backlog)

Create a server socket with backlog as the length of the connection request queue.

The task of managing client connection requests is performed by the operating system. The operating system stores these connection requests in a first-in-first-out queue. Many operating systems limit the maximum length of a queue, typically to 50. When the connection requests in the queue reach the maximum capacity of the queue, the host where the server process is located rejects the new connection requests. Only when the server process removes the connection request from the queue via the accept method of the Serversocket, freeing up the queue, can the queue continue to be filled with new connection requests.

For client processes, If it sends a connection request that is added to the server's queue, Then the connection to the server has been established successfully。 If a connection request from a client process is rejected by the server, Socket Construction method It will throw outConnection Exception,Serversocket Construction method of backlog parameter is used to explicitly set the length of the connection request queue, It will override the maximum length of the queue limited by the operating system。 it's worth noting that..., In the following cases, will still use the maximum length of the queue as limited by the operating system

1.The value of the backlog parameter is greater than the maximum length of the queue as defined by the operating system

2.The value of the backlog parameter is less than or equal to 0;

3. (located) at Server Socket Construction method There is no setting in backlog parameters。

Code Demo

Explain the above code, at the time of running the server side code,ServerSocket set the request queue length to 5, and hibernated for 10 minutes, and never accept out the connection, so in the client loop to the 6th socket connection is established when the above exception is reported.

Now add accept to take out the connection and look at it again, modifying the server-side code.

ServerSocket(int port, int backlog, InetAddress bindAddr)

Create a server with the specified port, listening backlog, and local IP address to bind to.

this one Construction method and the one above Construction method Similar use, Only one more localip parameters, Usually used when there are multiple NICs on the machine。 That is, fixing aip serve as a server-sideip

InetAddress address =InetAddress.getByName("192.168.105.126");

ServerSocket server=newServerSocket(3000,5,address);

ServerSocket()

Create unbound server sockets.

Serversocket There is a default without parameters Construction method。 created by this method ServerSocket Not bound to any port, The next step is also to pass thebind method is bound to a specific port。 This default Construction method The purpose of this is to allow the server to bind to a specific port before, Set up firstServerSocket Some options for the。 Because once a server is bound to a specific port, Some options can no longer be changed in the following code, first put ServerSocket of SO_EUSEADDR The option is set totrue, Then bind the port again

If you setReuseAddress (true) at the time of instantiating the ServerSocket object it has no effect.

Commonly used methods

Listens for and accepts connections to this socket.

This method mainly waits for a client to connect, if no client connects it will always wait. The front is basically used, the other methods are basically the same as the socket inside the method of use here is not too much to explain.


Recommended>>
1、Exploring Simpler Ether MultiSignature Contracts
2、dockerfile practice and personal conclusions
3、Your position is about to be replaced Are you afraid Driverless technology is on the way to experimentation
4、Big Data Crawler Project Video Tutorial
5、清河论金浅谈未来金融业的几个风口方向

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号