I was wondering if anyone could help me out.
In the code below when the connect() function is called it waits for quite a while if it cannot connect to the PC, ie. the PC is turned off.
I was wondering how to implement a timeout for connect() so it only waits 1-2 seconds.
I have googled and used the forum search so I apologise ahead of time if I have missed anything.
Code: Select all
int netSocketConnect()
{
struct sockaddr_in server;
unsigned long addr;
sock = socket( AF_INET, SOCK_STREAM, 0 ); //Create the socket
if (sock < 0)
{
return (-1);
}
//Creating socketadress of the Server
memset( &server, 0, sizeof (server)); //Consists of type, IP-adress and portnumber
if ((addr = inet_addr(ip)) != INADDR_NONE)
{
memcpy( (char *)&server.sin_addr, &addr, sizeof(addr));
}
else
{
return (-1);
}
server.sin_family = AF_INET; //IPv4-connection
server.sin_port = htons(port); //Portnumber
if(connect(sock,(struct sockaddr*)&server,sizeof(server)) <0) //Connecting to server
{
return (-1);
}
return 0;
}
Nick