How to set a timeout for connect() in sys/socket.h?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
darksaboteur
Posts: 18
Joined: Tue Dec 11, 2007 8:44 pm
Location: Australia
Contact:

How to set a timeout for connect() in sys/socket.h?

Post by darksaboteur »

Hi,
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 &#40;sock < 0&#41;
    &#123;
    return &#40;-1&#41;;
    &#125;
                                                                    //Creating socketadress of the Server
    memset&#40; &server, 0, sizeof &#40;server&#41;&#41;;                           //Consists of type, IP-adress and portnumber

    if &#40;&#40;addr = inet_addr&#40;ip&#41;&#41; != INADDR_NONE&#41;
    &#123;
    memcpy&#40; &#40;char *&#41;&server.sin_addr, &addr, sizeof&#40;addr&#41;&#41;;
    &#125;
    else
    &#123;
    return &#40;-1&#41;;
    &#125;

    server.sin_family = AF_INET;                                    //IPv4-connection
    server.sin_port = htons&#40;port&#41;;                                  //Portnumber

    if&#40;connect&#40;sock,&#40;struct sockaddr*&#41;&server,sizeof&#40;server&#41;&#41; <0&#41;    //Connecting to server
    &#123;
    return &#40;-1&#41;;
    &#125;
return 0;
&#125;
Thanks,
Nick
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

I haven't used the Berkley sockets on PSP, but when I use sceNetInetConnect to connect to my PC's IP it return immediately if the PC is off, or even if its on and there's no server on that port.
User avatar
darksaboteur
Posts: 18
Joined: Tue Dec 11, 2007 8:44 pm
Location: Australia
Contact:

Thanks

Post by darksaboteur »

Will give it a go and post back with how it goes.

Thanks,
Nick
User avatar
darksaboteur
Posts: 18
Joined: Tue Dec 11, 2007 8:44 pm
Location: Australia
Contact:

Got It Fixed :)

Post by darksaboteur »

Just thought I'd post this in case anyone else has this problem.
Changing to the sce sockets did not fix the problem, but changing the socket to being non-blocking did.
The code now reads

Code: Select all

int netSocketConnect&#40;&#41;
&#123;
struct sockaddr_in server;
unsigned long addr;
int NoBlock;
    sock = sceNetInetSocket&#40; AF_INET, SOCK_STREAM, 0 &#41;;                       //Create the socket
    if &#40;sock < 0&#41;
    &#123;
    return &#40;-1&#41;;
    &#125;
                                                                    //Creating socketadress of the Server
    memset&#40; &server, 0, sizeof &#40;server&#41;&#41;;                           //Consists of type, IP-adress and portnumber

    if &#40;&#40;addr = inet_addr&#40;ip&#41;&#41; != INADDR_NONE&#41;
    &#123;
    memcpy&#40; &#40;char *&#41;&server.sin_addr, &addr, sizeof&#40;addr&#41;&#41;;
    &#125;
    else
    &#123;
    return &#40;-1&#41;;
    &#125;

    server.sin_family = AF_INET;                                    //IPv4-connection
    server.sin_port = htons&#40;port&#41;;                                  //Portnumber

    NoBlock = 1;
    sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;;

    if&#40;sceNetInetConnect&#40;sock,&#40;struct sockaddr*&#41;&server,sizeof&#40;server&#41;&#41; <0&#41;    //Connecting to server
    &#123;
    return &#40;-1&#41;;
    &#125;
return 0;
&#125;
Funnily enough I found the solution on ps2dev... just via google ;)
The important line being "sceNetInetSetsockopt(sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof(NoBlock));"
Here is the thread for those interested http://forums.ps2dev.org/viewtopic.php?p=67436
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Nice find. I dont have the line

Code: Select all

sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;; 
in my code at all though. Still responds within a second.
Post Reply