News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

winsock - socket closing

Started by RedXVII, June 19, 2006, 08:13:00 PM

Previous topic - Next topic

RedXVII

Quick question on winsock, im trying to make disconnection tidy.

One user (Mr. Anderson) invokes closesocket to disconnect. This sends a message to my other user (Mr. Bob) FD_CLOSE, (see WSAAsyncSelect) that the connection has been ended. Does Mr. Bob have to invoke closesocket on that socket aswell in order to "complete" the disconnection?

Cheers  :U

James Ladd

Yes.
It's always good practice to close resources you opened unless the api specifically says not to.

Tedd

FD_CLOSE indicates that the connection has closed - any resources allocated by the socket are still allocated to the socket (just as they were before it was connected.)
closesocket actually releases the resources allocated to the socket, and discards its descriptor.
No snowflake in an avalanche feels responsible.

RedXVII

Thanks for that, so i close the socket. Anything else i need to do, or can i start making more sockets again without any problems?


James Ladd

After closing the socket keep in mind you cant use that socket handle again.
so YES just keep making new sockets and doing what you need to do with them.

What is your socket application doing ?

RedXVII

its a kind of messanger program im doing to learn winsock.

it works well, i was just wondering about closing sockets ^^, because i designed it so multiple uses can connect to it.

actually, - another recent problem ive been having is when leaving sockets for a while after inactivity - they stop working, as in, i cant send/recieve even though i havent closed them. i was just going to fix that by sending an "are you dead or not" kindof ping packet to keep the connection active. Anyone know what the default timeout (?) length for this is? will my solution be alright you think?

Tedd

You should be able to continue making new sockets with "socket." Although if you're receiving connections, then simply leave the initial listening socket open, and then the call to "accept" creates the new socket (for that connection) for you, you don't need to keep (explicitly) creating new sockets.

Assuming you're using tcp (otherwise there's no 'connection') use "getsockopt" with SO_KEEPALIVE to make sure keep-alive packets are actually being sent (supposedly) since tcp is supposed to be doing automatically. (And "setsockopt" if they're not.) [Otherwise, for udp, your solution sounds reasonable :wink]
QuoteA Windows Sockets provider need not support the use of keep-alive

The default time you need to send them depends on a number things, including your isp. There should be some way of finding out the time winsock uses, but the isp will be a different matter. Experiment (send 1 packet, start the clock, and then wait) and then maybe use half of that time. For bonus points, make it a user-modifiable setting :U
No snowflake in an avalanche feels responsible.

James Ladd