Re: Sockets causing deadlock

Tech-Archive recommends: Fix windows errors by optimizing your registry



My responses:

- No data is shared between the threads.

- My mistake if I said non-blocking. They are actually blocking. The sockets
are made non-blocking for receives, but during the connect(), shutdown(),
and disconnect() calls they are made blocking to make sure the call
finishes.

- A separate thread handles the connect, so that the main thread can
continue processing. In the main program this is necessary, but I have
duplicated the problem in a test program that simple waits for the connect
thread to finish. It just goes into a loop (using SleepEx) waiting for the
connect thread to signal that it is finished.

- I do use SleepEx() in most places. If I am not mistaken, this is needed
for receive callbacks.

Unfortunately the code is spread out in several files, and is difficult to
paste here (and some of it is copyrighted). Here is the basic algorithm:

<Main Loop>

while (Not time to shut down)
{
if (we don't have a socket)
{
Allocate a new socket
Set it to blocking
}

CheckConnection() //See below

SleepEx()
}
-------------------------------------
CheckConnection()
{
switch (status)
{
case not_connected:
Start the connection thread //see below
Status = connecting
break;
case connecting:
Do nothing - just wait
break;
case connection_failed:
call shutdown()
call closesocket();
break;
}
}
--------------------------------
ConnectionThread
{
call connect()
if (failed)
{
status = connection_failed
}

// Note: there is some other code to handle successful, but it won't get
called

exit
}




.



Relevant Pages

  • Re: recv blocks although socket is ready
    ... of the members of the interface in my network layer have blocking ... Only one has non-blocking semantics. ... call any socket operation and rely on the socket to block. ... Now I have to implement blocking myself in all other ...
    (microsoft.public.win32.programmer.networks)
  • Re: Socket overlapped I/O and FIONBIO
    ... Non blocking mode demand overlapped mode too, so the socket will be ... even if socket itself is non-blocking ...
    (microsoft.public.win32.programmer.networks)
  • Re: Socket overlapped I/O and FIONBIO
    ... is for the case you haven't set the socket in non-blocking mode. ... BSD socket approach. ... WSA_FLAG_OVERLAPPED that forces blocking mode. ... kernel completes if that will occur in fixed time. ...
    (microsoft.public.win32.programmer.networks)
  • Re: Threading and Timeouts
    ... I found that blocking i/o with select or poll works fine. ... > that Linux has a bug where certain socket activities trigger select ... > select is primarily designed for non-blocking I/O. ...
    (comp.programming.threads)
  • Re: SetSockOpt with SO_REUSEADDR parameter
    ... So I create multiple UDP sockets with the same port to send data. ... happening is that you are throwing away the old socket and replacing it with the new ... When the second client is connecting to server, I still get the error code ...
    (microsoft.public.vc.mfc)