Hi,
We are writing an application in which we need to connect from one PC to another using socket. The code works fine, its just that if the remote machine is down or not on the ntwork the connect call take around 20 secs. Is there any way in which we can set the timeout for the connect call.
The code is given below :
------------------------------------------------------------
struct sockaddr_in sa;
struct hostent *hp;
const char *pcv = reinterpret_cast<const char *> (stHost.data());
//stHost contains host name to which we need to connect
mySocket = ::socket(AF_INET, SOCK_STREAM, 0);
if(mySocket == INVALID_SOCKET)
return false;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(pcv);
sa.sin_port = htons(nPort);
if (sa.sin_addr.s_addr == INADDR_NONE) {
hp = gethostbyname(pcv);
if (hp == NULL) return false;
sa.sin_addr = *((struct in_addr *)(hp->h_addr));
}
if (connect(AvHTTP::fSocket,(const struct sockaddr *)&sa,sizeof(sa)) < 0)
{
return false;
}
------------------------------------------------------------
We tried using setsockopt, but it doesnt seem to work
int nMillis = 2000 // 2 secs
setsockopt(AvHTTP::fSocket, SOL_SOCKET, SO_RCVTIMEO, (char *) &nMillis, sizeof(nMillis));
Is there a better way to set the timeout for the connect call?
Could anyone please help with a code for setting the timeout.
Thanks and Regards
Faisal
We are writing an application in which we need to connect from one PC to another using socket. The code works fine, its just that if the remote machine is down or not on the ntwork the connect call take around 20 secs. Is there any way in which we can set the timeout for the connect call.
The code is given below :
------------------------------------------------------------
struct sockaddr_in sa;
struct hostent *hp;
const char *pcv = reinterpret_cast<const char *> (stHost.data());
//stHost contains host name to which we need to connect
mySocket = ::socket(AF_INET, SOCK_STREAM, 0);
if(mySocket == INVALID_SOCKET)
return false;
sa.sin_family = AF_INET;
sa.sin_addr.s_addr = inet_addr(pcv);
sa.sin_port = htons(nPort);
if (sa.sin_addr.s_addr == INADDR_NONE) {
hp = gethostbyname(pcv);
if (hp == NULL) return false;
sa.sin_addr = *((struct in_addr *)(hp->h_addr));
}
if (connect(AvHTTP::fSocket,(const struct sockaddr *)&sa,sizeof(sa)) < 0)
{
return false;
}
------------------------------------------------------------
We tried using setsockopt, but it doesnt seem to work
int nMillis = 2000 // 2 secs
setsockopt(AvHTTP::fSocket, SOL_SOCKET, SO_RCVTIMEO, (char *) &nMillis, sizeof(nMillis));
Is there a better way to set the timeout for the connect call?
Could anyone please help with a code for setting the timeout.
Thanks and Regards
Faisal