 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
spyk
Joined: 22 Mar 2006 Posts: 14
|
Posted: Thu Jun 01, 2006 1:45 am Post subject: Socket client ... |
|
|
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 |
|
 |
Arwin
Joined: 12 Jul 2005 Posts: 426
|
|
| Back to top |
|
 |
spyk
Joined: 22 Mar 2006 Posts: 14
|
Posted: Thu Jun 01, 2006 5:23 am Post subject: |
|
|
| of course :) |
|
| Back to top |
|
 |
danzel
Joined: 04 Nov 2005 Posts: 182
|
Posted: Thu Jun 01, 2006 9:04 am Post subject: |
|
|
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 |
|
 |
Ess
Joined: 20 Apr 2006 Posts: 16
|
Posted: Sat Jun 03, 2006 2:21 am Post subject: |
|
|
| If/when you finish your program, could you post it here? I would like to see it. |
|
| Back to top |
|
 |
spyk
Joined: 22 Mar 2006 Posts: 14
|
Posted: Sat Jun 03, 2006 9:28 pm Post subject: |
|
|
| 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 |
|
 |
danzel
Joined: 04 Nov 2005 Posts: 182
|
|
| Back to top |
|
 |
Ess
Joined: 20 Apr 2006 Posts: 16
|
Posted: Mon Jun 05, 2006 1:33 pm Post subject: |
|
|
| There is no need to belittle him. Contribute your knowledge, but keep your dignity. |
|
| Back to top |
|
 |
|
|
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
|