Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.6k views
in Technique[技术] by (71.8m points)

sockets - "close" windows scoket by calling closesocket or terminating process

It is about a very simple P2P server-client application implemented with direct Winsock.

The server runs on an unreliable platform (e.g. server application can terminate suddenly and restart without notice.) The worst thing is that the server implementation is unknown... and the connection is over WAN network...

In the long running client application I usually call ::shutdown/::closesocket once I get error like WSAECONNRESET 10054 from socket recv (and/or TCP Keep-alive), then I will try to reconnect the server again.

Basically, the client would rarely initiate a socket close actively. Server can stop/restart pretty often.

The whole application works most of time but sometimes the "reconnection" from client does not work. The new attempt with ::connect will fail (Unfortunately the exact error code is unknown to me..) However, if I terminate the client process and restart it again, the "reconnection" will work again.

The client socket creation and connect are pretty primitive:

  _socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  
  SOCKADDR_IN connection;
  ::SecureZeroMemory(&connection, sizeof(connection));
  connection.sin_family = AF_INET;
  connection.sin_port = ::htons(_port);
  connection.sin_addr.s_addr = _ip;

  ::connect(_socket, (SOCKADDR*)&connection, sizeof(connection));

Apparently terminating a process all opened socket handles will be closed/cleaned-up by Windows. Even for half-closed socket. Right?

why cannot mere calling ::closesocket achieve the same "effect"? due to linger? I have also tried to disable linger on the client side but it does not help.

BOOL val = TRUE;
::setsockopt(_socket, SOL_SOCKET, SO_DONTLINGER, (const char*)&val, sizeof(val));

LINGER lngr;
lngr.l_onoff = TRUE;
lngr.l_linger = 0;
::setsockopt(_socket, SOL_SOCKET, SO_LINGER, (const char*)&lngr, sizeof(lngr));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...