Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Winsock query

Status
Not open for further replies.

faisiaf

Programmer
Nov 16, 2002
3
US
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



 
I think this is the code of client side.
try using Timer.Serach msdn for &quot;HOWTO: Configure a Time-Out on a CSocket Operation&quot;...
Revert back to me if you have some idea of SSPI for sockets...Earlier i was thinking to write app in MFC,later i dropped idea and came back on Win32. as i am less familiar with MFC
 
Hi Pankaj

This is the client side code. I have not done any work on SSPI socket. This is the first time I m writing a code for socket. Could you help me with that

regards
 
Hi Faisal,
I think we are in same boat.I was also looking for socket codes. MFC i dropped and came back to Win32. I created SSPI codes for client and server.Basically sockets work on same funda in all,despite models put authentication on that

Server-1.Socket2.Bind3.Listen4.Accept(on request of client)5.Receive6.Send
Client-1.Socket2.Connect3.Send4.Receive

Socket is nothing but a virtual port location but it should be same on client and server.
Basically these sample codes echos on client and server
If you need that code i can mail you. My email id is pankaj_kum@yahoo.com
PANKAJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top