Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
this code sometimes hang the system on recv 
int CLTIUReadWrite::Read(  char *data, int nbytes)
{
    int nleft, nread;
    char *ptr2;

    ptr2 = data;

     nleft = nbytes;
        while (nleft > 0) {
        nread = read(m_Client->Socket, ptr2, nleft);
        if (nread < 0)
            return nread;
        else if (nread == 0)
            break;

        nleft -= nread;
        ptr2 += nread;
    }
    return (nbytes - nleft);
}
void CLTIUReadWrite::Write(  char *buff, int size)
{
	int h = m_Client->Write(( char*)buff,false,size);
	
	printf(" \n data write %i data actual %i \n ",h,size);
}
Posted
Comments
Richard MacCutchan 3-Aug-15 11:46am    
Is that a blocking socket? It may just be waiting for data from the remote end.
Mahmoud_Gamal 5-Aug-15 6:47am    
ok but when send and recv from windows to this device work well
but when change windows ->unix the system sometimes hange sometimes work well
Richard MacCutchan 5-Aug-15 7:23am    
Then you need to do some debugging to find out why. There is no way anyone here can guess what is happening.
Ashish Tyagi 40 5-Aug-15 20:50pm    
Are you running same code on windows and Linux?
Looks like there is an issue while calculating number of bytes to read (int nbytes for LTIUReadWrite::Read() call)
How do you calculate that?
Also are you sending files?
Put some more code to get answer.

1 solution

If you don't want the application hangs on the recv call then you have to put it in a separate thread.
 
Share this answer
 
Comments
Ashish Tyagi 40 4-Aug-15 19:37pm    
As you said "this code sometimes hang the system on recv ", where is recv call?
Mahmoud_Gamal 5-Aug-15 6:45am    
thanks for replay but i want to complete sequence no hang at half
the code run sometimes right some times complete
so i want guarantee to send guarantee to receive

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900