المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How can use Socket Connection Timeout?



C++ Programming
12-12-2010, 10:30 AM
Hi all,

Is it possilbe to set socket connection timeout? For e.g. If I want to connect to some host with socket connection within 5 seconds if I cant connect within 5 seconds,function return from this point without process further operation.

I have checked socket class, apprently I cant find out some way for connection timeout ? Can anybody help me regarding this?

i m using this for socket connection

WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
{
int i = WSAGetLastError();

}

sock = socket(AF_INET,SOCK_STREAM,0);
if(sock==-1)
{
int i = WSAGetLastError();

}

USES_CONVERSION;
struct sockaddr_in my_addr;
memset(&my_addr,0,sizeof(my_addr));

my_addr.sin_family = AF_INET; // host byte order
my_addr.sin_port = htons(MYPORT); // short, network byte order
LPSTR lpszAscii = T2A((LPTSTR)ServerName);
my_addr.sin_addr.s_addr = inet_addr(lpszAscii);
if (my_addr.sin_addr.s_addr == INADDR_NONE)
{
LPHOSTENT lphost;
lphost = gethostbyname(lpszAscii);
if (lphost != NULL)
{
my_addr.sin_addr.s_addr = ((LPIN_ADDR)lphost->h_addr)->s_addr;
}
else {
WSASetLastError(WSAEINVAL);
return WSAEINVAL;
}
}
if(connect(sock,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))==-1)
{
return FALSE;
}


please help me for this.

thanks in advance.