forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
darksaboteur



Joined: 11 Dec 2007
Posts: 18
Location: Australia

PostPosted: Tue May 26, 2009 5:54 pm    Post subject: How to set a timeout for connect() in sys/socket.h? Reply with quote

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:
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;
}


Thanks,
Nick
Back to top
View user's profile Send private message Visit poster's website
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Tue May 26, 2009 6:54 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
darksaboteur



Joined: 11 Dec 2007
Posts: 18
Location: Australia

PostPosted: Tue May 26, 2009 7:39 pm    Post subject: Thanks Reply with quote

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

Thanks,
Nick
Back to top
View user's profile Send private message Visit poster's website
darksaboteur



Joined: 11 Dec 2007
Posts: 18
Location: Australia

PostPosted: Tue May 26, 2009 11:59 pm    Post subject: Got It Fixed :) Reply with quote

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:
int netSocketConnect()
{
struct sockaddr_in server;
unsigned long addr;
int NoBlock;
    sock = sceNetInetSocket( 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

    NoBlock = 1;
    sceNetInetSetsockopt(sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof(NoBlock));

    if(sceNetInetConnect(sock,(struct sockaddr*)&server,sizeof(server)) <0)    //Connecting to server
    {
    return (-1);
    }
return 0;
}


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
Back to top
View user's profile Send private message Visit poster's website
Torch



Joined: 28 May 2008
Posts: 842

PostPosted: Wed May 27, 2009 1:42 am    Post subject: Reply with quote

Nice find. I dont have the line
Code:
sceNetInetSetsockopt(sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof(NoBlock));

in my code at all though. Still responds within a second.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group