[Solved] Issue when someone want to connect to my PSP server

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

Moderators: cheriff, TyRaNiD

Post Reply
Poison_xtreamlua
Posts: 12
Joined: Fri Aug 10, 2007 2:03 am

[Solved] Issue when someone want to connect to my PSP server

Post by Poison_xtreamlua »

Hi,

I 'm trying to make a server, but I have a problem, in fact my server works if the client come from the same network that the server, for instance I have 2 psp at home, one is the server and the other is the client , the client connect perfectly to the server , but if I want one of my friend connect to my PSP server it doesn't work although I've modified client source code, modifying this line :

server.sin_addr.s_addr = inet_addr("192.168.1.13 ");

by this one :

server.sin_addr.s_addr = inet_addr("90.21.51.33");

But it still doesn't work (the client returns it cannot connect the server). And I really don't know how to solve the probleme so if you can help me :)

Here is psp server code :

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <string.h>
#include <pspnet.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <psputility.h>
#include <sys/select.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
 
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket&#40;s&#41; close&#40;s&#41;
#define MODULE_NAME "Test"
#define printf pspDebugScreenPrintf
 
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
 
PSP_MODULE_INFO&#40;"Test", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;
 
int exit_callback&#40;int arg1, int arg2, void *common&#41;;
int CallbackThread&#40;SceSize args, void *argp&#41;;
int SetupCallbacks&#40;void&#41;;
 
/* Connection a un point d'acces */
int connect_to_apctl&#40;int config&#41;
&#123;
   int err;
   int stateLast = -1;
 
   /* Connection utilisant un profile */
   err = sceNetApctlConnect&#40;config&#41;;
   if &#40;err != 0&#41;
   &#123;
      printf&#40;MODULE_NAME "&#58; sceNetApctlConnect returns %08X\n", err&#41;;
      return 0;
   &#125;
 
   printf&#40;MODULE_NAME "&#58; Connecting to internet...\n\n"&#41;;
   while &#40;1&#41;
   &#123;
      int state;
      err = sceNetApctlGetState&#40;&state&#41;;
      if &#40;err != 0&#41;
      &#123;
         printf&#40;MODULE_NAME "&#58; sceNetApctlGetState returns $%x\n", err&#41;;
         break;
      &#125;
      if &#40;state > stateLast&#41;
      &#123;
         printf&#40;"  connection state %d of 4.\n", state&#41;;
         stateLast = state;
      &#125;
      if &#40;state == 4&#41;
         break;  // connection avec Ip static
 
      // Attente
      sceKernelDelayThread&#40;50*1000&#41;; // 50ms
   &#125;
   printf&#40;MODULE_NAME "&#58; Connected to internet.\n\n"&#41;;
 
   if&#40;err != 0&#41;
   &#123;
      return 0;
   &#125;
 
   return 1;
&#125;
 
int main&#40;int argc, char **argv&#41;
&#123;
   SetupCallbacks&#40;&#41;;
   pspDebugScreenInit&#40;&#41;;
 
   //Chargement du Modules inet
  sceUtilityLoadNetModule&#40;1&#41;;
  sceUtilityLoadNetModule&#40;3&#41;;
 
   int err;
 
   do
   &#123;
      if&#40;&#40;err = pspSdkInetInit&#40;&#41;&#41;&#41;
      &#123;
         printf&#40;MODULE_NAME "&#58; Error, could not initialise the network %08X\n", err&#41;;
         break;
      &#125;
 
      if&#40;connect_to_apctl&#40;1&#41;&#41;
      &#123;
         // Connecté et lance un test d'IP
         char szMyIPAddr&#91;32&#93;;
         if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41; strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
 
         SOCKET s_server;       
         s_server = socket&#40;PF_INET, SOCK_STREAM, IPPROTO_TCP&#41;;
 
            if &#40;s_server == INVALID_SOCKET&#41; printf&#40;"La fonction socket a echoue.\n"&#41;;
            else
            &#123;
            SOCKADDR_IN server;
 
            server.sin_family       = AF_INET;
            server.sin_addr.s_addr  = htonl&#40;INADDR_ANY&#41;;
            server.sin_port         = htons&#40;3500&#41;;
 
               if &#40;bind&#40;s_server, &#40;SOCKADDR *&#41;&server, sizeof&#40;server&#41;&#41; == SOCKET_ERROR&#41; printf&#40;"La fonction bind a echoue.\n"&#41;;
               else
               &#123;
                  if &#40;listen&#40;s_server, 0&#41; == SOCKET_ERROR&#41; printf&#40;"La fonction listen a echoue.\n"&#41;;
                  else
                  &#123;
                  SOCKET s_client;
                  SOCKADDR_IN client;
                  size_t csize = sizeof&#40;client&#41;;
 
                  s_client = accept&#40;s_server, &#40;SOCKADDR *&#41;&client, &csize&#41;;
 
                     if &#40;s_client == INVALID_SOCKET&#41; printf&#40;"La fonction accept a echoue.\n"&#41;;
                     else
                     &#123;
                     char buffer&#91;100&#93;;
 
                     printf&#40;"Le client %s s'est connecte !\n", inet_ntoa&#40;client.sin_addr&#41;&#41;;
 
                     sceNetInetSend&#40;s_client, "Bonjour", &#40;int&#41;strlen&#40;"Bonjour"&#41; + 1, 0&#41;;
 
                     sceNetInetRecv&#40;s_client, buffer, sizeof&#40;buffer&#41;, 0&#41;;
                     printf&#40;"%s\n", buffer&#41;;
 
                     closesocket&#40;s_client&#41;;
                     &#125;
                  &#125;
               &#125;
                closesocket&#40;s_server&#41;;
            &#125;
      &#125;
    &#125;   while&#40;0&#41;;
 
 
    return 0;
&#125;
 
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
   sceKernelExitGame&#40;&#41;;
   return 0;
&#125;
 
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
   int cbid;
 
   cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
   sceKernelRegisterExitCallback&#40;cbid&#41;;
   sceKernelSleepThreadCB&#40;&#41;;
 
   return 0;
&#125;
 
int SetupCallbacks&#40;void&#41;
&#123;
   int thid = 0;
 
   thid = sceKernelCreateThread&#40;"update_thread", CallbackThread,
                 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0&#41;;
   if&#40;thid >= 0&#41;
   &#123;
      sceKernelStartThread&#40;thid, 0, 0&#41;;
   &#125;
 
   return thid;
&#125;
And here client source code :

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <string.h>
#include <pspnet.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <psputility.h>
#include <sys/select.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>

#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket&#40;s&#41; close&#40;s&#41;
#define MODULE_NAME "Test"
#define printf pspDebugScreenPrintf

typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
 
PSP_MODULE_INFO&#40;"Test", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;
 
int exit_callback&#40;int arg1, int arg2, void *common&#41;;
int CallbackThread&#40;SceSize args, void *argp&#41;;
int SetupCallbacks&#40;void&#41;;

/* Connection a un point d'acces */
int connect_to_apctl&#40;int config&#41;
&#123;
	int err;
	int stateLast = -1;
 
	/* Connection utilisant un profile */
	err = sceNetApctlConnect&#40;config&#41;;
	if &#40;err != 0&#41;
	&#123;
		printf&#40;MODULE_NAME "&#58; sceNetApctlConnect returns %08X\n", err&#41;;
		return 0;
	&#125;
 
	printf&#40;MODULE_NAME "&#58; Connecting to internet...\n\n"&#41;;
	while &#40;1&#41;
	&#123;
		int state;
		err = sceNetApctlGetState&#40;&state&#41;;
		if &#40;err != 0&#41;
		&#123;
			printf&#40;MODULE_NAME "&#58; sceNetApctlGetState returns $%x\n", err&#41;;
			break;
		&#125;
		if &#40;state > stateLast&#41;
		&#123;
			printf&#40;"  connection state %d of 4.\n", state&#41;;
			stateLast = state;
		&#125;
		if &#40;state == 4&#41;
			break;  // connection avec Ip static
 
		// Attente
		sceKernelDelayThread&#40;50*1000&#41;; // 50ms
	&#125;
	printf&#40;MODULE_NAME "&#58; Connected to internet.\n\n"&#41;;
 
	if&#40;err != 0&#41;
	&#123;
		return 0;
	&#125;
 
	return 1;
&#125;
 
int main&#40;int argc, char **argv&#41;
&#123;
   SetupCallbacks&#40;&#41;;
 
   pspDebugScreenInit&#40;&#41;;
 
   //Chargement du Modules inet
  sceUtilityLoadNetModule&#40;1&#41;; 
  sceUtilityLoadNetModule&#40;3&#41;;
 
	int err;
 
	do
	&#123;
		if&#40;&#40;err = pspSdkInetInit&#40;&#41;&#41;&#41;
		&#123;
			printf&#40;MODULE_NAME "&#58; Error, could not initialise the network %08X\n", err&#41;;
			break;
		&#125;
 
		if&#40;connect_to_apctl&#40;1&#41;&#41;
		&#123;
			// Connecté et lance un test d'IP
			char szMyIPAddr&#91;32&#93;;
			if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41; strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
			
 		SOCKET sockListen;
		sockaddr_in addrListen;
		int error;
		
		sockListen = sceNetInetSocket&#40;AF_INET, SOCK_STREAM, IPPROTO_TCP&#41;;
			if &#40;sockListen <= 0&#41;
			&#123;
			printf&#40;"socket returned $%x\n", sockListen&#41;;
			sceKernelDelayThread&#40;500*1000&#41;;
			break;
			&#125;
			
		addrListen.sin_family = AF_INET;
		addrListen.sin_addr.s_addr  = inet_addr&#40;"90.21.51.33"&#41;;
		addrListen.sin_port = htons&#40;3500&#41;;
		
		err = sceNetInetConnect&#40;sockListen, &#40;sockaddr *&#41;&addrListen, sizeof&#40;addrListen&#41;&#41;;
			if &#40;err != 0&#41;
			&#123;
			printf&#40;"Unable to connect!\n"&#41;;
			printf&#40;"connect returned $%x\n", err&#41;;
			printf&#40;"  errno=$%x\n", sceNetInetGetErrno&#40;&#41;&#41;;
			sceKernelDelayThread&#40;500*1000&#41;;
			break;
			&#125;
			else
			&#123;
			printf&#40;"A reussi a ce connecter"&#41;;
			char buffer&#91;&#93; = "Humand";
			char buffer2&#91;1000&#93;;
			sceNetInetSend&#40;sockListen, buffer, strlen&#40;buffer&#41;, 0&#41;;
			sceNetInetRecv&#40;sockListen, &#40;u8*&#41;buffer2, sizeof&#40;buffer2&#41;-1, 0&#41;;
			printf&#40;"%s",buffer2&#41;;
			&#125;
		&#125;
	&#125;
	while&#40;0&#41;;
 
return 0;
&#125;
 
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
   sceKernelExitGame&#40;&#41;;
   return 0;
&#125;
 
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
   int cbid;
 
   cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
   sceKernelRegisterExitCallback&#40;cbid&#41;;
   sceKernelSleepThreadCB&#40;&#41;;
 
   return 0;
&#125;
 
int SetupCallbacks&#40;void&#41;
&#123;
   int thid = 0;
 
   thid = sceKernelCreateThread&#40;"update_thread", CallbackThread,
                 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0&#41;;
   if&#40;thid >= 0&#41;
   &#123;
      sceKernelStartThread&#40;thid, 0, 0&#41;;
   &#125;
 
   return thid;
&#125;
Please, help me :) .

See you .
Last edited by Poison_xtreamlua on Thu Aug 07, 2008 8:09 pm, edited 1 time in total.
KickinAezz
Posts: 328
Joined: Sun Jun 03, 2007 10:05 pm

Post by KickinAezz »

Hve you Checked the firewall?
Intrigued by PSP system Since December 2006.
Use it more for Development than for Gaming.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Your router probably isn't forwarding the port to the PSP. Whatever port you have the server bound to needs to be forwarded at the router to the PSP's local IP address. That's how the router knows to send stuff directed at "90.21.51.33" to "192.168.1.13".
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

You can take a look at the libupnp sources to automatically forward the port using uPnP. You can't expect everyone who uses your app to manually configure their router.
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

I find it MUCH easier to forward a port than to get the damn router to properly use upnp. I finally disabled upnp altogether. It wasn't worth the effort.
Poison_xtreamlua
Posts: 12
Joined: Fri Aug 10, 2007 2:03 am

Post by Poison_xtreamlua »

Hi , the firewall is not the probleme because if I start the server on my PC and I ask a friend to launch client PSP it works. It didn't work, only if I start a server on my PSP and ask a friend to launch client PSP.

So I will try to forward a port, but I though I ve done it , look at my screen (sorry it's in french ... ):

Image

Some translation :

Serveurs Lan = Lan server
Sélection = selection
Nom = Name
Activé = Activated
Protocole = Protocole
Du port = From port
Au port = to port
Adresse IP locale = Local adress IP

Then I use port 3500 , as you can see, the port is activated ? But it is just activated or it is forward ? (I don't know the difference) And if it doesn't forwarded, how to forward a port please ?

Anyway, I thank you very much :)
J.F.
Posts: 2906
Joined: Sun Feb 22, 2004 11:41 am

Post by J.F. »

Looks forwarded just fine.
Poison_xtreamlua
Posts: 12
Joined: Fri Aug 10, 2007 2:03 am

Post by Poison_xtreamlua »

Ho god , I succeed to make my PSP server works !!!

Here is the solution for those who have the same probleme as me :

In fact the probleme is not due to the source code, it's a probleme with IP and port forwarding.

The probleme, it's I didn't know how a port forwarding works .. in fact you have to choose the port you want to forward AND to the psp IP , to know the PSP ip , it's quiet simple you juste have to put this line your code :

printf("%s",szMyIPAddr);

like that :

Code: Select all

      if&#40;connect_to_apctl&#40;1&#41;&#41;
      &#123;
         // Connecté et lance un test d'IP
         char szMyIPAddr&#91;32&#93;;
         if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41; strcpy&#40;szMyIPAddr, "unknown IP address"&#41;; 
         printf&#40;"%s",szMyIPAddr&#41;;
      &#125;
The probleme was I forward a port but without to put the PSP IP but I put PC IP , so it couldn't works ^^ .

Sum up :

1) Start your server PSP and thanks to : printf("%s",szMyIPAddr); you know the PSP IP
2) Now you know what PSP IS , forward your port (in my case it was port 3500) and enter the PSP IP in the appropriate field.

Exemple :

In this screen we will focus on the 2nd line where the name is server :

http://img297.imageshack.us/my.php?image=heheds5.jpg

The mistake I've done it's I set up the IP 192.168.1.13 which is my PC IP, so to fix the probleme I've just changed 192.168.1.11 which is my PSP IP . And it works

I hope it's quiet clear because I'm french, and I hope It will can help someone else.

See you guys and thanks.
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

J.F. wrote:I find it MUCH easier to forward a port than to get the damn router to properly use upnp. I finally disabled upnp altogether. It wasn't worth the effort.
I've got a cheapo modem and my primary PC is setup for DMZ (ya unsafe I know, I've got a meticulously configured software firewall though, but WTH), and all other PCs use uPnP for torrents etc. Works fine here.
Post Reply