Re-using a socket after a completed AcceptEx() call!
-
Hello, I have a little problem on re-using a socket after a completed AcceptEx call. The number of maximum clients is equal to the number of sockets in my program. On every socket I made an AcceptEx() call to accept a new connection and receive the data. So far so good. After receiving the data I need a new AcceptEx() call which can receive data from other clients who are trying to connect. But - something is going wrong! When I simply recall AcceptEx() on the same listener socket and the same accept socket I get an error (WSAEINVAL / 10022L). Do I have to close the socket before I try to make a new AcceptEx() call on that socket? I would like to re-use this socket and leave it open, because it's queued in a completion port. I hope you can help me. Thanks for your attention! bond006
-
Hello, I have a little problem on re-using a socket after a completed AcceptEx call. The number of maximum clients is equal to the number of sockets in my program. On every socket I made an AcceptEx() call to accept a new connection and receive the data. So far so good. After receiving the data I need a new AcceptEx() call which can receive data from other clients who are trying to connect. But - something is going wrong! When I simply recall AcceptEx() on the same listener socket and the same accept socket I get an error (WSAEINVAL / 10022L). Do I have to close the socket before I try to make a new AcceptEx() call on that socket? I would like to re-use this socket and leave it open, because it's queued in a completion port. I hope you can help me. Thanks for your attention! bond006
-
Yes, you must close the socket before assigning a new socket to that same variable. Kuphryn
-
Hello, I have a little problem on re-using a socket after a completed AcceptEx call. The number of maximum clients is equal to the number of sockets in my program. On every socket I made an AcceptEx() call to accept a new connection and receive the data. So far so good. After receiving the data I need a new AcceptEx() call which can receive data from other clients who are trying to connect. But - something is going wrong! When I simply recall AcceptEx() on the same listener socket and the same accept socket I get an error (WSAEINVAL / 10022L). Do I have to close the socket before I try to make a new AcceptEx() call on that socket? I would like to re-use this socket and leave it open, because it's queued in a completion port. I hope you can help me. Thanks for your attention! bond006
Hi, well, you have two possibilities - first is to close the socket before you use the new
AcceptEx
using theclosesocket()
. But then you simply destroyed everything and there's no reuse. Second is to re-use the socket - for this you can use this trick: Instead of callingclosesocket()
you can use this one:TransmitFile( sock, NULL, 0, 0, NULL, NULL, TF_DISCONNECT | TF_REUSE_SOCKET )
This will close the socket, but makes it ready for AcceptEx reuse. Works only on NT, but as you wrote you use IOCP, so should not be a problem here ;)