views:

963

answers:

3

Problem:

  • Call to send(), returns Winsock Error 10038 against socket handle

Illustration:

acceptedSocket = accept (server, (sockaddr *)&sin, &len);
  • accept(), returns 0
    • A new thread, is created for each connection
    • send(), (in thread function) returns 10038

Illustration: - in thread function

//omitted
SOCKET RemoteSocket = (SOCKET) client;
//omitted
send (RemoteSocket, stringToSpend, strlen(stringToSpend), 0)

Suggestions:

  • Possible, race condition?
  • Could use I/O completion ports, but not at this stage
+2  A: 

accept() returns you a handle to a new connection-specific socket. for server code it's 2+ sockets involved: one is in listen state you are calling accept() for and second is one returned from accept() - it's an incoming connection socket. Following accept() can return socket for second incoming connection etc. if accept() returns 0 it's not an incoming connection - it's an error.

Eye of Hell
One correction: accept returning 0 is a normal (successful) execution. Errors are signalized by -1
jpalecek
A: 

Hmm, seems like your send is executing too fast before the accept happened. So the socket used in send is not valid at the point send is executed. One of the obnoxious feature of multithreading. You need to wait for an event at the send thread and fire an event when an accept occurs

rptony
OK, I'll try this...
Aaron
I am curious to know if this worked for you. :-)
rptony
A socket handle is defined as "a non-negative integer". So, I believe, zero is valid?
Aaron
+2  A: 

Isn't the problem in the line

acceptedSocket = accept (server, (sockaddr *)&sin, &len) == INVALID_SOCKET)

You make acceptedSocket the result of the comparison, but you should store the actual socket returned from accept somehow:

acceptedSocket = accept (server, (sockaddr *)&sin, &len);
isOK= acceptedSocket!=INVALID_SOCKET;

Although I'm a bit confused by the unbalanced parentheses in your post, so I may be wrong

jpalecek