forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Socket client ...

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
spyk



Joined: 22 Mar 2006
Posts: 14

PostPosted: Thu Jun 01, 2006 1:45 am    Post subject: Socket client ... Reply with quote

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.
Back to top
View user's profile Send private message
Arwin



Joined: 12 Jul 2005
Posts: 426

PostPosted: Thu Jun 01, 2006 2:07 am    Post subject: Reply with quote

Have you looked into http://svn.ps2dev.org/psp section?
Back to top
View user's profile Send private message
spyk



Joined: 22 Mar 2006
Posts: 14

PostPosted: Thu Jun 01, 2006 5:23 am    Post subject: Reply with quote

of course :)
Back to top
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Thu Jun 01, 2006 9:04 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
Ess



Joined: 20 Apr 2006
Posts: 16

PostPosted: Sat Jun 03, 2006 2:21 am    Post subject: Reply with quote

If/when you finish your program, could you post it here? I would like to see it.
Back to top
View user's profile Send private message MSN Messenger
spyk



Joined: 22 Mar 2006
Posts: 14

PostPosted: Sat Jun 03, 2006 9:28 pm    Post subject: Reply with quote

Code:

#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(MODULE_NAME, 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);

/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
   sceKernelExitGame();
   return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);
   sceKernelSleepThreadCB();

   return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread,
                 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
   if(thid >= 0)
   {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}


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

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


 void abandon(char message[])
      {
        printf(MODULE_NAME ": Abandon\n");
       
      }

/* -- connexion vers un serveur TCP --------------------------- */
      int ouvrir_connexion_tcp(char nom_serveur[], int port_serveur)
      {
          struct sockaddr_in addr_serveur;
          struct hostent *serveur;
          int fd;

          fd = socket(AF_INET, SOCK_STREAM, 0);       /* création prise */
          if (fd < 0)
        {
              abandon("socket");
        }
             /* recherche adresse serveur */

        //Grace a netdb.h
          serveur = gethostbyname(nom_serveur);     
       
          if (serveur == NULL)
        {
              abandon("gethostbyname");
        }


          addr_serveur.sin_family = AF_INET;
          addr_serveur.sin_port = htons(port_serveur);
          addr_serveur.sin_addr = *(struct in_addr *) serveur->h_addr;
          /* connexion au serveur */

          if (connect(fd, (struct sockaddr *) &addr_serveur,sizeof addr_serveur)< 0)
        {
              abandon("connect");
        }

          return (fd);
      }


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

 /* Connect to an access point */
int connect_to_apctl(int config)
{
   int err;
   int stateLast = -1;

   /* Connect using the first profile */
   err = sceNetApctlConnect(config);
   if (err != 0)
   {
      printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
      return 0;
   }

   printf(MODULE_NAME ": Connecting...\n");
   while (1)
   {
      int state;
      err = sceNetApctlGetState(&state);
      if (err != 0)
      {
         printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
         break;
      }
      if (state > stateLast)
      {
         printf("  connection state %d of 4\n", state);
         stateLast = state;
      }
      if (state == 4)
         break;  // connected with static IP

      // wait a little before polling again
      sceKernelDelayThread(50*1000); // 50ms
   }
   printf(MODULE_NAME ": Connected!\n");

   if(err != 0)
   {
      return 0;
   }

   return 1;
}




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

      void demander_document(int fd, char adresse_document[])
      {
          char requete[TAILLE_TAMPON];
          int longueur;
          /* constitution de la requête, suivie d'une ligne vide */
          longueur = snprintf(requete, TAILLE_TAMPON,"GET %s HTTP/1.0" CRLF CRLF,adresse_document);
          write(fd, requete, longueur);       /* envoi */
      }

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

     void afficher_reponse(int fd)
      {
          char tampon[TAILLE_TAMPON];
          int longueur;
          while (1) {
              longueur = read(fd, tampon, TAILLE_TAMPON);     /* lecture par bloc */
              if (longueur <= 0)
                  break;
         /* copie sur sortie standard */

           // write(1, tampon, longueur); 
        //Revisited by moi pour la psp
        printf(MODULE_NAME ": tampon\n");
          };
      }

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


          nom_serveur = adress_server;
          port_serveur = atoi(port_server);
          adresse_document = adress_document;

          fd = ouvrir_connexion_tcp(nom_serveur, port_serveur);

          demander_document(fd, adresse_document);
          afficher_reponse(fd);
          close(fd);

     }

    //Le thread qui fait le boulo
int net_thread(SceSize args, void *argp)
{
   int err;

   do
   {
      if((err = pspSdkInetInit()))
      {
         printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
         break;
      }

      if(connect_to_apctl(1))
      {
         // On se connecte, on prend l'ip
         char szMyIPAddr[32];
         if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
            strcpy(szMyIPAddr, "unknown IP address");
           // On lance le bins
         start_client("www.luaplayer.org", "80", "/wlan-test.txt");
      }
   }
   while(0);

   return 0;
}


/* Simple thread */
int main(int argc, char **argv)
{
   SceUID thid;

   SetupCallbacks();

   pspDebugScreenInit();

   if(pspSdkLoadInetModules() < 0)
   {
      printf("Error, could not load inet modules\n");
      sceKernelSleepThread();
   }

   /* Create a user thread to do the real work */
   thid = sceKernelCreateThread("net_thread", net_thread, 0x18, 0x10000, PSP_THREAD_ATTR_USER, NULL);
   if(thid < 0)
   {
      printf("Error, could not create thread\n");
      sceKernelSleepThread();
   }

   sceKernelStartThread(thid, 0, NULL);

   sceKernelExitDeleteThread(0);

   return 0;
}

This homebrew download a TXT file on a HTTP server, and write it on the screen.
Back to top
View user's profile Send private message
danzel



Joined: 04 Nov 2005
Posts: 182

PostPosted: Sun Jun 04, 2006 7:34 am    Post subject: Reply with quote

You do know that we have libcurl on psp for fetching files over http/ftp:
http://svn.ps2dev.org/listing.php?repname=pspware&path=%2Ftrunk%2Flibcurl%2F&rev=0&sc=0

(No I don't know why its in the pspware repository)
Back to top
View user's profile Send private message
Ess



Joined: 20 Apr 2006
Posts: 16

PostPosted: Mon Jun 05, 2006 1:33 pm    Post subject: Reply with quote

There is no need to belittle him. Contribute your knowledge, but keep your dignity.
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group