How does the client determine that the socket connection is disconnected from the server?

Source: Internet
Author: User

First of all, server's socekt is not close.
If u had closed client's socekt, u shoshould creat it again when u want to connect server's.

Even if the socket is closed, it will still exist unless the socket option is set to close immediately.
Let's take a look at the setsockopt () function.

 

My friend's question:

The server program is socket sever, port 12005 is bound, and listen is started. Then the client continuously connects and disconnects. After the server program is automatically closed at every day, another waiting program opens. After 8-9 days of running the server, the client cannot be connected, and the connected client cannot request data. That is to say, the server program does not accept socket connections, and the data sent cannot be accepted in onreceive, at this time, you can log on to the server remotely. The CPU usage is about 4%, and the memory usage of your server program is less than 10 MB (It is okay if you have reached 70 MB ), there was no congestion in a line of data (I opened the SQL2000 Enterprise Manager at the time), closed my server program, and the client could be connected again, however, after two hours, it was the case above. This time, my server program cannot be connected, so I had to shut down the operating system, restart the server, and restart the server.

It takes 8 to 9 days for repeated tasks to occur again. It's strange that there have been four times in a row and no reason can be found,

I think
1. Is it 8-9 days after the operating system port is used up? If yes
2. Is the memory used up? (1 GB system memory, which is always stable to about MB in the Resource Manager)

Bytes ---------------------------------------------------------------------------------------------------

If the TCP connection does not send and receive data, it will time out after dozens of seconds. After the timeout, the server will not receive onclose ()
In this way, there will be a lot of dead connections that cannot be cleared on the server side.
When no data is sent between B/S, some online packages must be sent. For example, if no data is transmitted, a space is sent every 20 seconds.
The server periodically checks Each connection as follows:
Onrecv ()
{
Last_recv_time = gettickcount ();
// Do Recv () here
}

Ontimer ()
{
If (gettickcount ()-last_recv_time> 35000) // close connection any way after 35 seconds 'idle
{
Closesocket (s)
}
}

The client must have a reconnection Mechanism

 

 

 

How to determine whether the socket is disconnected in C #

In C #, the connected attribute of the socket class only indicates the status of the last I/O operation. If [the other party connected] is disconnected after this, it will always return true, unless you send data through socket. Therefore, it cannot be determined through this attribute!
Someone said it could be determined by using the socket. Available attribute. msdn said: if [the other party connected] is disconnected, it will throw an exception. However, the bug report (http://dam.mellis.org/2004/08/net_socket_bugs_gotchas/) points out that the msdn statement is not completely correct, and this attribute throws an exception only in a few cases. So, this trick is still not feasible!
In fact, when the socket. Receive () method [the other party connected] is disconnected, it returns the result to inform that the read-only value is 0 bytes. We can find the answer from this point. This Q & A (http://www.dotnet247.com/247reference/msgs/36/182526.aspx) provides a good solution: Can call socket. poll () method, which is used to pass in the selectread value for the second parameter of the method. If this method returns true, it can be used by socket. to judge the return value of the receive () method, I will briefly write the code:
Socket S = new socket (..);
If (S. Poll (-1, selectmode. selectread ))
{
Int nread = S. Receive ();
If (nread = 0)
{
// The socket connection is disconnected.
}
}
If you still have a better method, please let me know ,:).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.