Connect Timeout

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

Moderators: cheriff, TyRaNiD

Post Reply
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Connect Timeout

Post by steffven »

*next post*
Last edited by steffven on Tue Nov 25, 2008 7:51 am, edited 1 time in total.
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

The same problem with this code:

Code: Select all

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 &#40;fail<0&#41;
&#123;
	sceKernelExitGame&#40;&#41;;
&#125;
I really don't understand why it doesn't work...
Onii
Posts: 40
Joined: Sun Oct 05, 2008 1:07 pm

Post by Onii »

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.
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

Code: Select all

pcip = ...

sock = socket&#40;PF_INET, SOCK_STREAM, IPPROTO_TCP&#41;;

socketaddr.sin_family = AF_INET;
socketaddr.sin_addr.s_addr = inet_addr&#40;pcip&#41;;
socketaddr.sin_port = htons&#40;12345&#41;;

struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
	
setsockopt&#40;sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof&#40;timeout&#41;&#41;; // Returns 0
	
fail = connect&#40;sock,&#40;struct sockaddr*&#41;&socketaddr,sizeof&#40;socketaddr&#41;&#41;;
if &#40;fail<0&#41;
&#123;
	sceKernelExitGame&#40;&#41;;
&#125;
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: Select all

struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
	
setsockopt&#40;sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof&#40;timeout&#41;&#41;; // 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
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

Nobody knows how to set the connect timeout? -.-
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Take a look at existing code. For example, in PSPRadio/PSP/SharedLib/PSPApp/httpget.cpp, they use:

Code: Select all

                u32  timeo;
                timeo = 3 *1000*1000; /** timeout is in microseconds */
                Log&#40;LOG_LOWLEVEL, "http_connect&#40;&#41;&#58; Calling Setsockopt&#40;&#41;"&#41;;
                if &#40;setsockopt&#40;sock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof&#40;timeo&#41;&#41; < 0&#41; 
so it looks like your argument to setsockopt needs to be a u32 and microseconds instead of struct timeval.
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

Hmm, tested it but neither of this 2 work:

Code: Select all

int connectWithTimeout &#40;int sfd,
                        struct sockaddr *addr,
                        int addrlen,
                        u32 *timeo&#41;
&#123;
    struct timeval sv;
    socklen_t svlen = sizeof sv;
    int ret;

    if &#40;!timeo&#41;
        return connect &#40;sfd, addr, addrlen&#41;;
    if &#40;getsockopt &#40;sfd, SOL_SOCKET, SO_RCVTIMEO, &#40;char *&#41;&sv, &svlen&#41; < 0&#41;
        return -1;
    if &#40;setsockopt &#40;sfd, SOL_SOCKET, SO_RCVTIMEO, &#40;char *&#41;timeo, sizeof *timeo&#41; < 0&#41;
        return -1;
    ret = connect &#40;sfd, addr, addrlen&#41;;
    setsockopt &#40;sfd, SOL_SOCKET, SO_RCVTIMEO, &#40;char *&#41;&sv, sizeof sv&#41;;
    return ret;
&#125;

void ...
&#123;
	char *pcip;
	int fail;	
	struct sockaddr_in socketaddr;

pcip = ...

	sock = socket&#40;PF_INET, SOCK_STREAM, IPPROTO_TCP&#41;;

	socketaddr.sin_family = AF_INET;
	socketaddr.sin_addr.s_addr = inet_addr&#40;pcip&#41;;
	socketaddr.sin_port = htons&#40;12345&#41;;

	u32 timeo;
    timeo = 3 *1000*1000;

	fail = connectWithTimeout&#40;sock,&#40;struct sockaddr*&#41;&socketaddr,sizeof&#40;socketaddr&#41;,&timeo&#41;;

	if &#40;fail<0&#41;
	&#123;
		sceKernelExitGame&#40;&#41;;
	&#125;

Code: Select all

void ...
&#123;
	char *pcip;
	int fail;	
	struct sockaddr_in socketaddr;
	
	pcip = ...

	sock = socket&#40;PF_INET, SOCK_STREAM, IPPROTO_TCP&#41;;

	socketaddr.sin_family = AF_INET;
	socketaddr.sin_addr.s_addr = inet_addr&#40;pcip&#41;;
	socketaddr.sin_port = htons&#40;12345&#41;;

	u32 timeo;
    timeo = 3 *1000*1000;
	
    setsockopt&#40;sock, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof&#40;timeo&#41;&#41;; // Still returns 0

	fail = connect&#40;sock,&#40;struct sockaddr*&#41;&socketaddr,sizeof&#40;socketaddr&#41;&#41;;

	if &#40;fail<0&#41;
	&#123;
		sceKernelExitGame&#40;&#41;;
	&#125;
I still have to wait around 1minute, frustrating...
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

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.
Post Reply