 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Mon Nov 24, 2008 2:38 am Post subject: Connect Timeout |
|
|
*next post*
Last edited by steffven on Tue Nov 25, 2008 7:51 am; edited 1 time in total |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Mon Nov 24, 2008 6:35 am Post subject: |
|
|
The same problem with this code:
| Code: | pcip = ...
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
socketaddr.sin_family = AF_INET;
socketaddr.sin_addr.s_addr = inet_addr(pcip);
socketaddr.sin_port = htons(12345);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
fail = connect(sock,(struct sockaddr*)&socketaddr,sizeof(socketaddr));
if (fail<0)
{
sceKernelExitGame();
} |
I really don't understand why it doesn't work... |
|
| Back to top |
|
 |
Onii
Joined: 05 Oct 2008 Posts: 40
|
Posted: Mon Nov 24, 2008 12:08 pm Post subject: |
|
|
In what way does it not work? If it's not returning control back to your program after hitting the code listed, look into non blocking socket io.
If it's something else, let us know. |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Mon Nov 24, 2008 10:04 pm Post subject: |
|
|
| Code: | pcip = ...
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
socketaddr.sin_family = AF_INET;
socketaddr.sin_addr.s_addr = inet_addr(pcip);
socketaddr.sin_port = htons(12345);
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); // Returns 0
fail = connect(sock,(struct sockaddr*)&socketaddr,sizeof(socketaddr));
if (fail<0)
{
sceKernelExitGame();
} |
It works fine, but i can't set a timeout for the connect() procedure. I'm able to connect to a valid host with this code, but if i try to connect to an invalid host, it takes around 1 minute to timeout, but i want it to timeout after 5 seconds, so i used the following code to change the connect timeout to 5 seconds:
| Code: | struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); // Returns 0 |
But this code doesn't change anything, i still have to wait around 1 minute. I just want to change the time till the connect procedure times out from around 1 minute to 5 seconds |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Tue Nov 25, 2008 9:44 pm Post subject: |
|
|
| Nobody knows how to set the connect timeout? -.- |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Wed Nov 26, 2008 5:00 am Post subject: |
|
|
Take a look at existing code. For example, in PSPRadio/PSP/SharedLib/PSPApp/httpget.cpp, they use:
| Code: |
u32 timeo;
timeo = 3 *1000*1000; /** timeout is in microseconds */
Log(LOG_LOWLEVEL, "http_connect(): Calling Setsockopt()");
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)) < 0)
|
so it looks like your argument to setsockopt needs to be a u32 and microseconds instead of struct timeval. |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Wed Nov 26, 2008 6:39 am Post subject: |
|
|
Hmm, tested it but neither of this 2 work:
| Code: | int connectWithTimeout (int sfd,
struct sockaddr *addr,
int addrlen,
u32 *timeo)
{
struct timeval sv;
socklen_t svlen = sizeof sv;
int ret;
if (!timeo)
return connect (sfd, addr, addrlen);
if (getsockopt (sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&sv, &svlen) < 0)
return -1;
if (setsockopt (sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)timeo, sizeof *timeo) < 0)
return -1;
ret = connect (sfd, addr, addrlen);
setsockopt (sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&sv, sizeof sv);
return ret;
}
void ...
{
char *pcip;
int fail;
struct sockaddr_in socketaddr;
pcip = ...
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
socketaddr.sin_family = AF_INET;
socketaddr.sin_addr.s_addr = inet_addr(pcip);
socketaddr.sin_port = htons(12345);
u32 timeo;
timeo = 3 *1000*1000;
fail = connectWithTimeout(sock,(struct sockaddr*)&socketaddr,sizeof(socketaddr),&timeo);
if (fail<0)
{
sceKernelExitGame();
} |
| Code: | void ...
{
char *pcip;
int fail;
struct sockaddr_in socketaddr;
pcip = ...
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
socketaddr.sin_family = AF_INET;
socketaddr.sin_addr.s_addr = inet_addr(pcip);
socketaddr.sin_port = htons(12345);
u32 timeo;
timeo = 3 *1000*1000;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo)); // Still returns 0
fail = connect(sock,(struct sockaddr*)&socketaddr,sizeof(socketaddr));
if (fail<0)
{
sceKernelExitGame();
} |
I still have to wait around 1minute, frustrating... |
|
| Back to top |
|
 |
jimparis
Joined: 10 Jun 2005 Posts: 1179 Location: Boston
|
Posted: Wed Nov 26, 2008 9:46 am Post subject: |
|
|
| There's other code in PSPradio that does something different, sets the socket nonblocking then polls connect() for a result. Maybe try that. Or just find some application that behaves as you desire, and see if they figured it out. Or, reverse engineer the firmware further and find out how to do it the Sony way. |
|
| 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
|