 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
darksaboteur

Joined: 11 Dec 2007 Posts: 18 Location: Australia
|
Posted: Tue May 26, 2009 5:54 pm Post subject: How to set a timeout for connect() in sys/socket.h? |
|
|
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 |
|
 |
Torch

Joined: 28 May 2008 Posts: 842
|
Posted: Tue May 26, 2009 6:54 pm Post subject: |
|
|
| 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 |
|
 |
darksaboteur

Joined: 11 Dec 2007 Posts: 18 Location: Australia
|
Posted: Tue May 26, 2009 7:39 pm Post subject: Thanks |
|
|
Will give it a go and post back with how it goes.
Thanks,
Nick |
|
| Back to top |
|
 |
darksaboteur

Joined: 11 Dec 2007 Posts: 18 Location: Australia
|
Posted: Tue May 26, 2009 11:59 pm Post subject: Got It Fixed :) |
|
|
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 |
|
 |
Torch

Joined: 28 May 2008 Posts: 842
|
Posted: Wed May 27, 2009 1:42 am Post subject: |
|
|
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 |
|
 |
|
|
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
|