[En-Nut-Discussion] Thread is interrupted by another thread and doesn't come back.

Petr Odlozil zip357 at centrum.cz
Tue Nov 28 08:42:39 CET 2006


Hello,

  I'm kindly asking you for the help.

  We're building some kind of a converter, which receives data from a 
client and resends them to a server, then receives data from this server 
and resends them to the connected client.

There are two threads which accept connections and save sockets to the 
que. (After the mail there is a simplified code ...)
Then there is one thread which does the communication. In the while loop 
gets sockets from the que, receives data and does the resending.

What happens is that the receiving thread interrupts the sending thread 
somewhere in NutTcpReceive() or NutTcpSend().

And even if there is no conection comming .. so the NutTcpAccept is just 
blocking the receive thread, it doesn't go back to the send thread. So 
the SEND hangs in the while loop. This happens just every other day.

Most of the time the code works fine.

I would really appreciate your help.

Best regards

Petr Odlozil
-------------------------------------------------------------
static u_long timeout = 500

THREAD RECEIVE - receiving commands

for(;;)
{	
  if ((serversock=NutTcpCreateSocket()) != 0)
   {
	if( NutTcpAccept(serversock,commandport)==0) {

	buff[0] = (((unsigned int)serversock) & 0x00FF);
	buff[1] = ((((unsigned int)serversock) & 0xFF00) >> 8);
	if (savetoque(buff,2,&sendque)==ERRQUE_FULL)
	{	
	   NutTcpCloseSocket(serversock);
	}
	NutEventPostAsync(&queries_comming);
        }
   }
}	

THREAD SEND --- resending commands
for(;;)
{
if (readfromque(pombuff,&n,0,&sendque) == ERRQUE_NOERR)
{
  // here is getting the serversock from pombuff and opening connection 
the clientsock
  // then I go to receive data from serversock sending them to client 
sock, receive replies and sending them to serversock.

NutTcpSetSockOpt(serversock,SO_SNDTIMEO,&timeout,sizeof(timeout));	 
NutTcpSetSockOpt(serversock,SO_RCVTIMEO,&timeout,sizeof(timeout));

//***somewhere in this cycle it is interrupted by the thread RECEIVE*** 
and it doesn't come back to this thread

while ((!NutTcpError(serversock)) && (!NutTcpError(clientsock)))
{
    if ((bytes= NutTcpReceive(serversock,buff,commandsize))<0)
  	{	break;
	};
     if ((errcode=NutTcpSend(clientsock,buff,bytes))<bytes)
	{	  break;
	}		
     if ((bytes = NutTcpReceive(clientsock,buff,commandsize))<0)
	{	break;	};				
     if ((errcode=NutTcpSend(serversock,buff,bytes))<bytes)
	{  break; }					
}

//if I finish everything from serversock I close sockets and wait for 
the event ...
NutEventWait(&queries_comming,0);
}



More information about the En-Nut-Discussion mailing list