Socket client ...

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

Moderators: cheriff, TyRaNiD

Post Reply
spyk
Posts: 14
Joined: Wed Mar 22, 2006 9:50 pm

Socket client ...

Post by spyk »

Hi!

I'm programming an hombrew that receive data from a program who is on my computer with the SOCKET..

For the Windows program, no problem, I usualy program on Windows..

But, for the PSP, i've never seen samples for create client with the socket...

If someone can give me an example, for using client socket on the PSP, because I will connect my psp, with the SERVER program who is in my computer...

Thank, and sorry for my english.
Arwin
Posts: 426
Joined: Tue Jul 12, 2005 7:00 pm

Post by Arwin »

Have you looked into http://svn.ps2dev.org/psp section?
spyk
Posts: 14
Joined: Wed Mar 22, 2006 9:50 pm

Post by spyk »

of course :)
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

There is a simple example included with the psp:
/usr/local/pspdev/psp/sdk/samples/net/simple/

You just use standard unix sockets, the only difference is that you need to connect to the wifi first.
Ess
Posts: 16
Joined: Thu Apr 20, 2006 8:16 am

Post by Ess »

If/when you finish your program, could you post it here? I would like to see it.
spyk
Posts: 14
Joined: Wed Mar 22, 2006 9:50 pm

Post by spyk »

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pspnet.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
//pour get hostbyname
#include <netdb.h>
//pour snprintf
#include <stdio.h>


#define printf pspDebugScreenPrintf

#define MODULE_NAME "NetSample"
#define HELLO_MSG   "Hello there. Type away.\r\n"

PSP_MODULE_INFO&#40;MODULE_NAME, 0x1000, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;0&#41;;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	sceKernelExitGame&#40;&#41;;
	return 0;
&#125;

/* Callback thread */
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;

/* Sets up the callback thread and returns its thread id */
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;


//*******************************************************************************************************
//Fin des Formalités...
//*******************************************************************************************************

#define CRLF "\r\n"
#define TAILLE_TAMPON 1000


 void abandon&#40;char message&#91;&#93;&#41;
      &#123;
		  printf&#40;MODULE_NAME "&#58; Abandon\n"&#41;;
		  
      &#125;

/* -- connexion vers un serveur TCP --------------------------- */
      int ouvrir_connexion_tcp&#40;char nom_serveur&#91;&#93;, int port_serveur&#41;
      &#123;
          struct sockaddr_in addr_serveur;
          struct hostent *serveur;
          int fd;

          fd = socket&#40;AF_INET, SOCK_STREAM, 0&#41;;       /* création prise */
          if &#40;fd < 0&#41;
		  &#123;
              abandon&#40;"socket"&#41;;
		  &#125;
             /* recherche adresse serveur */

		  //Grace a netdb.h
          serveur = gethostbyname&#40;nom_serveur&#41;;      
		  
          if &#40;serveur == NULL&#41;
		  &#123;
              abandon&#40;"gethostbyname"&#41;;
		  &#125;


          addr_serveur.sin_family = AF_INET;
          addr_serveur.sin_port = htons&#40;port_serveur&#41;;
          addr_serveur.sin_addr = *&#40;struct in_addr *&#41; serveur->h_addr;
          /* connexion au serveur */

          if &#40;connect&#40;fd, &#40;struct sockaddr *&#41; &addr_serveur,sizeof addr_serveur&#41;< 0&#41;
		  &#123;
              abandon&#40;"connect"&#41;;
		  &#125;

          return &#40;fd&#41;;
      &#125;


	  /* ------------------------------------------------------ */

 /* Connect to an access point */
int connect_to_apctl&#40;int config&#41;
&#123;
	int err;
	int stateLast = -1;

	/* Connect using the first 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...\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;  // connected with static IP

		// wait a little before polling again
		sceKernelDelayThread&#40;50*1000&#41;; // 50ms
	&#125;
	printf&#40;MODULE_NAME "&#58; Connected!\n"&#41;;

	if&#40;err != 0&#41;
	&#123;
		return 0;
	&#125;

	return 1;
&#125;




      /* ------------------------------------------------------ */

      void demander_document&#40;int fd, char adresse_document&#91;&#93;&#41;
      &#123;
          char requete&#91;TAILLE_TAMPON&#93;;
          int longueur;
          /* constitution de la requête, suivie d'une ligne vide */
          longueur = snprintf&#40;requete, TAILLE_TAMPON,"GET %s HTTP/1.0" CRLF CRLF,adresse_document&#41;;
          write&#40;fd, requete, longueur&#41;;       /* envoi */
      &#125;

      /* ------------------------------------------------------ */

	  void afficher_reponse&#40;int fd&#41;
      &#123;
          char tampon&#91;TAILLE_TAMPON&#93;;
          int longueur;
          while &#40;1&#41; &#123;
              longueur = read&#40;fd, tampon, TAILLE_TAMPON&#41;;     /* lecture par bloc */
              if &#40;longueur <= 0&#41;
                  break;
         /* copie sur sortie standard */

			  // write&#40;1, tampon, longueur&#41;;  
		  //Revisited by moi pour la psp
        printf&#40;MODULE_NAME "&#58; tampon\n"&#41;;
          &#125;;
      &#125;

     // La fonction qui se connecte, recupere le document, et se deconnecte en utilisant toutes les dernieres fontcions...
      void start_client&#40;char adress_server, char port_server, char adress_document&#41;
	  &#123;
          char *nom_serveur, *adresse_document;
          int port_serveur;
          int fd;


          nom_serveur = adress_server;
          port_serveur = atoi&#40;port_server&#41;;
          adresse_document = adress_document;

          fd = ouvrir_connexion_tcp&#40;nom_serveur, port_serveur&#41;;

          demander_document&#40;fd, adresse_document&#41;;
          afficher_reponse&#40;fd&#41;;
          close&#40;fd&#41;;

	  &#125;

    //Le thread qui fait le boulo
int net_thread&#40;SceSize args, void *argp&#41;
&#123;
	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;
			// On se connecte, on prend l'ip
			char szMyIPAddr&#91;32&#93;;
			if &#40;sceNetApctlGetInfo&#40;8, szMyIPAddr&#41; != 0&#41;
				strcpy&#40;szMyIPAddr, "unknown IP address"&#41;;
           // On lance le bins
			start_client&#40;"www.luaplayer.org", "80", "/wlan-test.txt"&#41;;
		&#125;
	&#125;
	while&#40;0&#41;;

	return 0;
&#125;


/* Simple thread */
int main&#40;int argc, char **argv&#41;
&#123;
	SceUID thid;

	SetupCallbacks&#40;&#41;;

	pspDebugScreenInit&#40;&#41;;

	if&#40;pspSdkLoadInetModules&#40;&#41; < 0&#41;
	&#123;
		printf&#40;"Error, could not load inet modules\n"&#41;;
		sceKernelSleepThread&#40;&#41;;
	&#125;

	/* Create a user thread to do the real work */
	thid = sceKernelCreateThread&#40;"net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL&#41;;
	if&#40;thid < 0&#41;
	&#123;
		printf&#40;"Error, could not create thread\n"&#41;;
		sceKernelSleepThread&#40;&#41;;
	&#125;

	sceKernelStartThread&#40;thid, 0, NULL&#41;;

	sceKernelExitDeleteThread&#40;0&#41;;

	return 0;
&#125;
This homebrew download a TXT file on a HTTP server, and write it on the screen.
danzel
Posts: 182
Joined: Fri Nov 04, 2005 11:03 pm

Post by danzel »

You do know that we have libcurl on psp for fetching files over http/ftp:
http://svn.ps2dev.org/listing.php?repna ... rev=0&sc=0

(No I don't know why its in the pspware repository)
Ess
Posts: 16
Joined: Thu Apr 20, 2006 8:16 am

Post by Ess »

There is no need to belittle him. Contribute your knowledge, but keep your dignity.
Post Reply