Question

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
Was it helpful?

Solution

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top