Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been using MFC in VC++

I am coding a client program using TCP/IP socket comm with Server which have a KEEP ALIVE socket option, and the client receive every data from the server.

1. If or when the client close the socket, and I check and run 'netstat' in command line of PC, I always find the same ip:portno is Fin_wait or Close_wait that mean not-closed rightly.

2. At that time the server send the control packet to sync of KEEP ALIVE signal, so the client reply to sync msg to the server.

3. So, the Client cannot close same socket because of the reponse to KEEP ALIVE operation.

4. When the client try to open another socket or to do new and any other operations to the server like as data sending or receiving, that makes no normal results, it fail.

5. Sometimes, the client make error messages "TCP Spurious Retransmission" to the server. Is this one clue of the above problem?

In conclusion, I want to close my client socket any time and make new connection to any other servers.

Furthermore, let me clarify the network operations using TCP sockets in this KEEP ALIVE option of situation.

After deeper investigation, the error code of closesocket is mainly case WSAENOTSOCK: "The descriptor is not a socket." I don't know what cause this error code.

There is simple diagram for this TCP data flow. I want for it to help you.

CLIENT --------------------------------------------------------------------- SERVER

1. Request ----------------------> Socket Open -------------------------> Reply

2. Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data
Recv Data <----------------------------------------------------------- Send Data

3. Request ----------------------> closesocket -------------------------> ???

4. Reply with error code WSAENOTSOCK

Thank you.

What I have tried:

More than 20 days wasted for this problem.
My program is half completed for this error.
Posted
Updated 9-Jun-16 20:55pm
v2

1 solution

Why you choose the server to send the keepalive packets? In most cases the client is interested in keeping a connection open.

You did not tell us about the used protocol and the typical data flow. Depending on that it might help to define a disconnect message.

It might be even not necessary to use keepalive. See also http://tldp.org/HOWTO/TCP-Keepalive-HOWTO/overview.html[^].

"TCP Spurious Retransmission"

Is this a Wireshark message? Then they might be repeated SYN packets. See also Spurious Retransmissions | Packet Foo | Analyzing network packets since 2003[^].

I suggest to rethink your setup. If we should help you should tell us about the data flow, when and why connections should be closed programmatically and how closed connections should be detected on both sides (keepalive, time outs, sending fails).

[EDIT: For updated question]
Quote:
After deeper investigation, the error code of closesocket is mainly case WSAENOTSOCK: "The descriptor is not a socket." I don't know what cause this error code.
The most common reason for this return code is that the socket has already been closed (assuming that the socket has been successfully created). If you call for example closesocket for a valid socket two times in a row, the second call will fail with this error code. Because sockets are reused by the system, this behaviour is not guaranteed (especially if some time has elapsed).

A common method to avoid duplicate calls is assigning INVALID_SOCKET after a socket has been closed and checking for this value before trying to close it.

I suggest to post some code snippets to get further help. Please try to post only necessary code (which might be difficult because such errors require a clear view on what is going on).
[/EDIT]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900