sceNetInetConnect not returning

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

Moderators: cheriff, TyRaNiD

Post Reply
poison
Posts: 17
Joined: Thu Dec 14, 2006 11:07 pm

sceNetInetConnect not returning

Post by poison »

I wrote a user mode prx to activate wifi device, and connect to a socket server, sceNetInetSocket and sceNetInetSetsockopt just work fine, but when I launch sceNetInetConnect, it never return but not crashing.
Here is a piece of my code:

Code: Select all

PSP_MODULE_INFO( "wifiuser", 0, 1, 0 );
PSP_MAIN_THREAD_ATTR( PSP_THREAD_ATTR_USER );

......

int wifiInit( const char * ip_addr, int port )
{
	struct sockaddr_in addr;
	int flag = 1, cmd, res;
	
	log( "ip %s port %d\n", ip_addr, port );
	if &#40; connectApctl&#40; 1 &#41; < 0 &#41;
	&#123;
		return -1;
	&#125;

	sock_server = sceNetInetSocket&#40; PF_INET, SOCK_STREAM, 0 &#41;;
	if &#40; sock_server < 0 &#41;
	&#123;
		log&#40; "Error create sock connection\n" &#41;;
		return -1;
	&#125;
	log&#40; "create sock %08x\n", sock_server &#41;;
	int ret = sceNetInetSetsockopt&#40; sock_server, SOL_TCP, TCP_NODELAY, &flag, sizeof&#40; flag &#41; &#41;;
	log &#40; "ret %08x\n", ret &#41;;
	addr.sin_family = AF_INET;
    addr.sin_port = htons&#40; 7513 &#41;; 
    addr.sin_addr.s_addr = sceNetInetInetAddr&#40; ip_addr &#41;;
	log&#40; "ip address %08x\n", &#40; unsigned int &#41;addr.sin_addr.s_addr &#41;;
	
	ret = sceNetInetConnect&#40; sock_server, &#40; struct sockaddr * &#41;&addr, sizeof&#40; addr &#41; &#41;;
	log &#40; "ret %08x\n", ret &#41;;
	if &#40; ret < 0 &#41;
	&#123;
		log&#40; "Error connect to sock %08x\n", sock_server &#41;;
		return -1;
	&#125; 
&#125;

......

int module_start&#40; SceSize args, void *argp &#41;
&#123;
	thid = sceKernelCreateThread&#40; "wifhost_thread", main_thread, 0x20, 0x10000, PSP_THREAD_ATTR_USER, NULL &#41;;
	if&#40; thid >= 0 &#41;
		sceKernelStartThread&#40; thid, 0, 0 &#41;;
	return 0;
&#125;

int module_stop&#40; SceSize args, void *argp &#41;
&#123;
	return 0;
&#125;
log.txt:

Code: Select all

connection state 0
connection state 2
connection state 3
connection state 4
ip 192.168.1.27 port 7513
create sock 0000000b
ret 00000000
ip address 1b01a8c0
As the log.txt shown, sceNetInetConnect keep running...
I had a lots modification try on the sockaddr_in structure and the thread stack size, but no result, is it just looping in sceNetInetConnect? or I passed something wrong? it almost drove me mad..
zmast
Posts: 3
Joined: Thu Apr 10, 2008 8:11 am

Post by zmast »

I'm not sure I'll be able to help you but I'll tell you what i know.

First, sceNetInetConnect is by default blocking. This means that it won't return until the connection is enstabilished.

If this is your case then something may be wrong in your network setup.
So you could think about firewalls etc, but i assume you already tried to disable them and verified that there is a service listening at port 7513 at 192.168.1.27.
One other problem may be in the AP connection, if I'm not wrong i read somewhere that old firmwares could only connect to profiles using static IP addresses. Personally i'm using DHCP on 3.90 M33 and it is ok.
So i would try to see if i'm connected to AP with a valid IP.
You can check this using this block of code taken from net.c in sdk samples:

Code: Select all

			// connected, get my IPADDR and run test
			char szMyIPAddr&#91;32&#93;;
			if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41;
				strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
I don't see strange things in your code, it should be okay, any way this is my function which i know for sure is perfectly working (it also supports timeout on connection).
Try it if you want to.

Code: Select all

int netutil_TcpConnect&#40;unsigned long RemoteIp, unsigned short RemotePort&#41;
&#123;
	int sock;
	struct sockaddr_in Addr;
	int err;
	int NoBlock;
	int ElapsedTime = 0;

	sock = sceNetInetSocket&#40;PF_INET, SOCK_STREAM, 0&#41;;
	if&#40;sock < 0&#41;
	&#123;
		return NETUTIL_ERR_INTERNAL;
	&#125;

	Addr.sin_family = AF_INET;
	Addr.sin_port = htons&#40;RemotePort&#41;;
	Addr.sin_addr.s_addr = htonl&#40;RemoteIp&#41;;
	
	NoBlock = 1;
	sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;;

	err = sceNetInetConnect&#40;sock, &#40;struct sockaddr *&#41;&#40;&Addr&#41;, sizeof&#40;Addr&#41;&#41;;
	if&#40;err == 0&#41;    //Connesso al primo compo? che culo!
	&#123;
		NoBlock = 0;
		sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;;
		return sock;
	&#125;
	else if&#40;err == -1 && sceNetInetGetErrno&#40;&#41; == EINPROGRESS&#41;
	&#123;
		do
		&#123;
			err = sceNetInetConnect&#40;sock, &#40;struct sockaddr *&#41;&#40;&Addr&#41;, sizeof&#40;Addr&#41;&#41;;
			if&#40;err == 0 || &#40;err == -1 && sceNetInetGetErrno&#40;&#41; == EISCONN&#41;&#41; //Connesso! &#58;&#41;
			&#123;
				NoBlock = 0;
				sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;;
				return sock;
			&#125;
			else
			&#123;
				sceKernelDelayThread&#40;50*1000&#41;; // 50ms
				ElapsedTime += 50;
			&#125;
		&#125; while&#40;ElapsedTime < NETUTIL_TCPCONNECT_TIMEOUT&#41;;
        
		NoBlock = 0;
		sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;;
		sceNetInetClose&#40;sock&#41;;
		return NETUTIL_ERR_TIMEOUT;
	&#125;
    
	NoBlock = 0;
	sceNetInetSetsockopt&#40;sock, SOL_SOCKET, SO_NONBLOCK, &NoBlock, sizeof&#40;NoBlock&#41;&#41;;
	sceNetInetClose&#40;sock&#41;;
	return NETUTIL_ERR_INTERNAL;
&#125;
Post Reply