tags:

views:

498

answers:

1

Hi

I seem to be getting this particular always when i am trying to receive data in that socket. The socket is a valid socket and connection is also established and there is some data always that periodically comes in theparticular port. But some how the recv function gives this error. Any idea what may be the cause.

sample code present below.

thanks in advance

struct hostent  *hdata;        // Used when Node name is converted to IP-Address
struct sockaddr_in  insock;       // Socket address structure
unsigned long   ipaddr;       // IP-Address in numeric format
CString sIPAddr = _T("1.1.1.1");
char * cSBCS = (char*)sIPAddr.GetBuffer(sIPAddr.GetLength());
ipaddr = inet_addr( cSBCS/*sIPAddr*/ );

hdata = gethostbyname( sIPAddr );

insock.sin_family = AF_INET;
insock.sin_port  = htons( 2101 );
memcpy( &insock.sin_addr, *(hdata->h_addr_list), hdata->h_length );


 // Create socket
if(( m_Socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )) == INVALID_SOCKET );
// return FALSE;

// Connect to MSS-box
if(( connect( m_Socket, (struct sockaddr *)&insock, sizeof( insock ))) == SOCKET_ERROR )
DWORD dwNonBlocking = -1;
if( ioctlsocket( m_Socket, FIONBIO, &dwNonBlocking ))

if((pdwRxChars = recv( m_Socket, pRxBuffer,1024, 0)) == SOCKET_ERROR )

always getting WSAEWOULDBLOCK error

+1  A: 

You are setting socket to NON-BLOCKING:

DWORD dwNonBlocking = -1;
ioctlsocket( m_Socket, FIONBIO, &dwNonBlocking )

and then you try to use blocking recv() - in that case you will always get this error.

Look at asynchronous functions such as select, WSARecv...

Or disable Non-Blocking mode setting dwNonBlocking to zero.

Mihail
well i tried assigning dwnonblocking to zero and got a different error WSAEFAULT always. any idea
ckv
Did you initialized pRxBuffer correctly?
Mihail