Hi, decided to get my feet wet with winsock. Quickly came up with a few q's:

1. From reading some of the other posts on the topic, it would seem that the normal way to do things is:
- 1.1 create socket
- 1.2 modify socket (for non-blocking sockets)
- 1.3connect
- 1.4 send (in my case, http request)
- 1.5 recv
- 1.6 shutdown
- 1.7 closesocket

Now, the question of course is, can a socket be used again, i.e. go from 1.5 to 1.3, or would one have to go thru all the steps, for every connect? Haven't found anything explicit on this, and searching the board points in different directions. Here for instance.

2. I thought that when I receive a FD_CLOSE message, my socket would be closed, well at least that the connection would be dropped.
However, if I try to use connect on the socket (inside the FD_CLOSE handler or outside) I get WSAEISCONN ... a recv on the socket also points
to the fact that it is closed, however. Is the socket closed on the server-side but not on mine?

Posted on 2003-02-24 19:12:05 by Fake51
In order to use a socket it must be have a valid socket handle. By calling the close socket function, you close the socket and release the handle. If you close a socket, it's basically gone. If you want to use another socket you must use CreateSocket again.

When you recieve the FD_CLOSE message, that means that the other side of the connection has been closed and now it's your turn. So if you recieve this message then you call the CloseSocket function.

Also, you don't call Shutdown before closing a socket, you call Shutdown just after (or is it before?) WSACleanup.

What you would do is something like this:

Start up Winsock:

Create a new connection (Perhaps on a new thread for blocking sockets):
Set socket modes

Get State Loop/Messages (For async sockets this would be in the WndProc):
End of Loop/Messages

If socket is not closed, CloseSocket (When you close first)

Cleanup Winsock:

You may also want to read this excellent tut by Thomas:

Posted on 2003-03-11 03:48:44 by IRBMe
Remarks for shutdown from the PSDK:
An application should not rely on being able to reuse a socket after it has been shut down. In particular, a Windows Sockets provider is not required to support the use of connect on a socket that has been shut down.

Posted on 2003-03-11 04:20:37 by Thomas
Cheers. That sorta answers my question ;) I was pretty much figuring this was the nice way about it.

Anyway, from a winsock help file:

Description: Disable sends and/or receives on a socket.

So it would be used before closesocket if at all ;)

Posted on 2003-03-12 02:47:34 by Fake51