Wifi - Sockets Problems

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

Moderators: cheriff, TyRaNiD

Post Reply
DarkShadow44
Posts: 29
Joined: Thu Jul 09, 2009 4:13 am

Wifi - Sockets Problems

Post by DarkShadow44 »

Hi, I've some problems with sockets:

PSP

Code: Select all

char string[10];
sprintf(string,"PALIB_HI");

struct sockaddr_in i_addr;
i_addr.sin_family = AF_INET;
i_addr.sin_port = htons(6666);
i_addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
int i=0;

ClientSockSend=socket(AF_INET, SOCK_DGRAM, 0);
			

setsockopt(ClientSockSend,SOL_SOCKET,SO_BROADCAST,&optval,sizeof(char));


while(1)
{

     sendto(ClientSockSend,string,10,0, (struct sockaddr *) &i_addr, sizeof(i_addr));
			
     printf("\nSending: %d , %s",i,string);
     i++;
     sceDisplayWaitVblankStart();
}
PC

Code: Select all

struct sockaddr_in i_addr;
i_addr.sin_family = AF_INET;
i_addr.sin_port = htons(6666);
i_addr.sin_addr.s_addr = htonl(INADDR_ANY);

ServerSockListen=socket(AF_INET, SOCK_DGRAM, 0);
		
bind(ServerSockListen, (struct sockaddr *) &i_addr, sizeof(i_addr));

		

int i=0;
char buffer[10];
memset(buffer,'\0',10);
while(1)
{
			
	int size=sizeof(struct sockaddr);

			
	int rc=0;
	iprintf("Waiting: ",i,"");
	int ix=0;
	while(1)
	{
				
		rc=recvfrom(ServerSockListen,buffer,10,0, (struct sockaddr *) &i_addr, &size);
				
		if(rc==-1) break;


		iprintf("Buffer: ",buffer);
		if(buffer[0]=='P' && buffer[1]=='A')
		{
			//iprintf("Received:  ",buffer);
		}
			
	}
	i++;
	swiWaitForVBlank();
}

The PSP sends a string and the PC should receive it, but it doesn't receive anything... (Broadcast)

If I use the same code for the PC to send and the PSP to receive, it works...

Do you know why ??
jojojoris
Posts: 255
Joined: Sun Mar 30, 2008 4:06 am

Post by jojojoris »

Are you sure your psp is connected to an access point?
Are you using the right IP addresses?

Code: Select all

int main(){
     SetupCallbacks();
     makeNiceGame();
     sceKernelExitGame();
}
DarkShadow44
Posts: 29
Joined: Thu Jul 09, 2009 4:13 am

Post by DarkShadow44 »

1) --> "If I use the same code for the PC to send and the PSP to receive, it works..."

The PSP can receive the messages of the PC

2) I use Auto configured connections...

I use the "netdialog" sample it is the easiest (and best) for wifi connections.


P.S. I tried it with PC <-> PC and DS <-> PC, that works too :-(
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Are you in infrastructure mode or ad-hoc mode?
Getting broadcast to work with a wireless network could be tricky.
With an AP, you can send a directed IP-broadcast packet to the router which can then do a MAC-broadcast on the local network, but with ad-hoc, you need to do a broadcast at the MAC layer, and the PSP might not automatically detect and support that.

In fact it may not support broadcast at all -- it might just refuse to route outgoing packets destined for 255.255.255.255.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

Most routers will drop broadcast packets. If he's in infrastructure mode then just sending to the subnet's broadcast address is sufficient. No need to send to the router's address. WLAN is transparent usually to the physical lan connected to the AP.

E.g. Send to 192.168.1.255 (broadcast address) if your subnet is 192.168.1.0/24 (IP 192.168.1.xxx & Subnet Mask 255.255.255.0)

Try this manually first in case something is broken with SO_BROADCAST.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Two things I learned when doing networking on Doom for PSP: first, you need to do explicit waiting in places to give the network threads time to run; remember that the PSP uses cooperative multitasking, and if you don't give up time, the network threads won't run. Second, you use IP addresses directly in your packets. As Torch mentioned, broadcast packets don't seem to work well, and it's the router's responsibility to direct packets to the proper computer based on the IP address/port. If everything has the same external IP address, be sure to set your router's port forwarding to the correct local IP.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

From the looks of things he's got all devices on the same subnet. Sending a packet to the subnet's broadcast address will work in all cases but only devices on the same subnet with no router inbetween will receive it (LAN/WLAN). There is NO way a broadcast will work over the internet unless you have UDP tunnelling software like Gamer's Internet Tunnel which sniffs and forwards all LAN packets and replaces the header at both sending and receiving end according to the local subnet. From address is replaced with the sender's external IP etc. Once a client identifies a server with a broadcast, it tries to conntect to the IP the reply came from and ends up connecting to the external IP of the server on the other LAN across the internet (port forwarding for the actual connection should be done).

If you're trying to list servers over the internet, then thats why they have master servers with static addresses :)
DarkShadow44
Posts: 29
Joined: Thu Jul 09, 2009 4:13 am

Post by DarkShadow44 »

First, thanks for the replies.

I tried the Broadcasts PSP <-> PC ; PSP --> PC didn't work.

But the same code worked with the DS...


But why doesn't it work over the internet ?

I can't use master servers, because I don't want to pay anything (and I don't know how to write master servers for many clients...)

I want to create a little Game, that searches for hosts and then you can join. How could I do this (without having to pay for something) ???
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

DarkShadow44 wrote:First, thanks for the replies.

I tried the Broadcasts PSP <-> PC ; PSP --> PC didn't work.

But the same code worked with the DS...


But why doesn't it work over the internet ?

I can't use master servers, because I don't want to pay anything (and I don't know how to write master servers for many clients...)

I want to create a little Game, that searches for hosts and then you can join. How could I do this (without having to pay for something) ???
Did you try using the broadcast address directly?? Ex: Send to 192.168.1.255 if your IPs are 192.168.1.1....192.168.1.2...192.168.1.254 etc.

A broadcast is sent only to the same subnet as the broadcast address you used. Hence it won't work on the internet. There is no way to find games on the internet without a static master server.
DarkShadow44
Posts: 29
Joined: Thu Jul 09, 2009 4:13 am

Post by DarkShadow44 »

1) Do you know a page, where I could create a master server for free ?

2a)Or could I use a MySQL-Database on a free homepage ?
2b) Can I access such a Database with the PSP ?
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

The ideal master server would be a realtime application that receives notifications from a game server as soon as the game server has started and stores its IP. It should poll all the servers periodically to check if they are still online and remove dead servers. This would function at the application layer hence you'd need to communicate between the game server and master server using your own protocol/magicwords and stuff.

You could pull this off with a Perl script that listens for connections but most free webhosts won't let you run a realtime script. Most of them won't even let you receive or make connections from the script.

So you'd just need to use a Perl/PHP script that receives the server info through a standard HTTP request and stores the information in a SQL database. Clients can query the list of servers either directly from the database (if the webhost allows) or through a Perl/PHP script via HTTP.

You'll need to manually purge dead servers from the database using an external script/application on your PC.

You need to have some basic server side scripting knowledge to get this working. You can probably get away with using the Sony modules for HTTP requests from the PSP.
DarkShadow44
Posts: 29
Joined: Thu Jul 09, 2009 4:13 am

Post by DarkShadow44 »

Can you say me, how a simple PHP script must be for getting/writing all hosts to the Database ?

Dead servers wouldn't be a problem, or is it to much if the PSP creates checks if a connection to each server is possible ? (for example with ping?)

Would be nice if you can help me, I know that it isn't easy...

I only want a very basic script for "talking" with the PSP or Database.

P.S.

Would this or that be possible ?

Sorry that they are not english ^^

2) Do you know if this would be really "free" ?
3) What does "8 FTP-Accounts" mean, can I upload 8 Files ??
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

DarkShadow44 wrote: 3) What does "8 FTP-Accounts" mean, can I upload 8 Files ??
You've got a LOT to learn before you can attempt this...
Post Reply