Sign in to follow this  
johnnyBravo

(Winsock)How to reset socket for reuse?

Recommended Posts

Hi, with winsock sockets, ive binded one to a specific port and set it on listening. Ive worked out how to stop it from listening etc, but i can't work out how to rebind the socket with another port. Any ideas? thanks

Share this post


Link to post
Share on other sites
I wouldn't worry about reusing the socket itself. Close it and get a new one. Odds are that, if everything got cleaned up properly, you will be handed back the very same file descriptor by the OS.

If you were counting on the socket structure to remain unchanged (because, e.g. you have multiple copies of it lying around in your program), I'd either consider keeping a pointer to that structure instead, or switch to a handle-based system.

Share this post


Link to post
Share on other sites
True, alas there is no unbind function available, so closing the socket will be neccesary.

Make sure you only use WSAStartup and WSACleanup at the beginning and end of your very program, do not use them for purposes describebed above.

Share this post


Link to post
Share on other sites
Ah I see, another reason i couldn't get it working either was because i somehow deleted the wsastatup code :)

thanks for the info

edit:

By the way i am using asychronous sockets and i am wondering to i call all this or would i leave wsaayncselect out of my disconnect button method?

Quote:

closesocket(s);
s= socket(AF_INET,SOCK_STREAM,0);
WSAAsyncSelect(s, GetSafeHwnd(), WM_ONSOCKET, FD_WRITE | FD_CONNECT | FD_READ | FD_CLOSE);


s being the socket name.



thanks

Share this post


Link to post
Share on other sites
Also , do i have to call this
s = socket (AF_INET, SOCK_STREAM, 0);
again when reinitialising the socket?
thx

edit also do i have to call this again
WSAAsyncSelect(sHost, GetSafeHwnd(), WM_USER+1, FD_WRITE | FD_CONNECT | FD_READ | FD_CLOSE);
?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this