connect, socket API, is sending the connection request after long delay

Tech-Archive recommends: Speed Up your PC by fixing your registry



Hello All,

In our application we need to communication with multiple devices
simultaneously. We're creating threads to communicate with individual
devices. We're are using non-blocking sockets. After making "connect",
we wait for 5 seconds for application to get connected with device.
Problem we're facing is that last device (10th) is online but we got
it as offline. Reason- till the last moment of wait "connect" has not
sent the connection request to device. After the 5 seconds device is
receiving the connection request. As as solution we can-

1. increase the wait time
2. submit the job only in 7-8 threads at a time.
3. combination of above both

But I'm just surprise on performance of socket API and wondering if
we're missing the settings, which can helps to boost the performance.

I'd appreciate any help. Please do let me know in case I need to give
more information.

Here is code we're using (it is legacy code)-


int Connect_To_Port(SOCKET win_socket, char *host, unsigned short
port, int connTimeout)
{

int retval, err;
SOCKADDR_IN server;
SOCKADDR_IN client;
unsigned long l;
WSAEVENT connEvent;



memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = htonl(INADDR_ANY);



l = inet_addr(host);
memset(&client, 0, sizeof(client));
client.sin_family = AF_INET;
client.sin_port = htons(port);
memcpy(&(client.sin_addr),&l,4);



char szBuf[MAX_PATH];
memset(szBuf,0,sizeof(szBuf));



if (connTimeout > 0 && connTimeout < 180000)
{
// Mark our socket non-blocking, and ask for asynch notifications.
connEvent = WSACreateEvent();
if(WSAEventSelect(win_socket,connEvent,FD_CONNECT) == SOCKET_ERROR)
{
return BII_ERR_OPENSOCKET;
}
}

DWORD dwStatus = 0;
timeval readTimeout;
fd_set fs;





// connect to remote host
retval = connect(win_socket,(sockaddr*)&client,sizeof(client));
if (retval == SOCKET_ERROR)
{
err = WSAGetLastError();
switch (err)
{
case (WSAEISCONN): // already connected! no problem man

return TRUE;
case (WSAEWOULDBLOCK): // not ready yet...return so that we
don't block
{


DWORD dwStatus = 0;
dwStatus = WSAWaitForMultipleEvents
(1,&connEvent,FALSE,connTimeout,TRUE);

if(dwStatus == WSA_WAIT_EVENT_0)
{
WSANETWORKEVENTS lpNetEvents;
WSAEnumNetworkEvents(win_socket,connEvent,&lpNetEvents);
if((lpNetEvents.lNetworkEvents & FD_CONNECT) &&
(lpNetEvents.iErrorCode[FD_CONNECT_BIT] == 0))
{

return TRUE;
}
else
{

return BII_ERR_OPENSOCKET;
}
}
else
{

return BII_ERR_OPENSOCKET;
}
}
default:

return BII_ERR_OPENSOCKET; // real error
}
}
return TRUE;
}



.



Relevant Pages

  • connect, socket API, is sending the connection request after long delay
    ... In our application we need to communication with multiple devices ... We're are using non-blocking sockets. ... sent the connection request to device. ... // Mark our socket non-blocking, ...
    (microsoft.public.win32.programmer.networks)
  • Re: TCP Streams from Unknown source to VB.Net
    ... communicate with any types of other platforms on TCP/IP communication. ... the data structure is sent in binary format out over a socket. ... can't contain arrays. ... from the server. ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Handling exceptions in Socket Callbacks
    ... why im using this complicated method of socket communication - ... there remains the problem of propogating the error generated in the callback ... > catch(Exception objException) ...
    (microsoft.public.dotnet.general)
  • Another question about OpenOffice (and component based programming in general)
    ... if my program is going to connect the OO server via socket 8100 ... there is some sort of communication going on. ... If pipe: what is the name of the pipe? ...
    (comp.lang.java.programmer)
  • Re: Handling exceptions in Socket Callbacks
    ... You're correct the asynchronous socket methods give more opportunities ... I always handle exceptions in callbacks in the callback itself. ... why im using this complicated method of socket communication - ...
    (microsoft.public.dotnet.general)