|
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
View previous topic :: View next topic |
Author |
Message |
r0b3rt
Joined: 26 May 2008 Posts: 17
|
Posted: Tue May 27, 2008 12:37 am Post subject: wlan "simple" sample for 3.xx (simple_prx) |
|
|
Hy, I'm new here, and I have a question about running a sample program from PSPSDK on my PSP (3.90M33-3).
I am trying to make a simple tcp server/client for PSP and ../sdk/samples/net/simple looks interesting. I managed to compile it but when I run it I get a 80020148 error. That's probably because that sample is for 1.5 firmware version.
But i can't compile "simple_prx" sample (which should be for 3.xx). This is the output I get after 'make':
Code: |
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O0 -G0 -Wall -g -D_PSP_FW_VERSION=150 -c -o main.o main.c
psp-build-exports -b exports.exp > exports.c
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O0 -G0 -Wall -g -D_PSP_FW_VERSION=150 -c -o exports.o exports.c
psp-gcc -I. -I/usr/local/pspdev/psp/sdk/include -O0 -G0 -Wall -g -D_PSP_FW_VERSION=150 -L. -L/usr/local/pspdev/psp/sdk/lib -specs=/usr/local/pspdev/psp/sdk/lib/prxspecs -Wl,-q,-T/usr/local/pspdev/psp/sdk/lib/linkfile.prx main.o exports.o -lpspdebug -lpspdisplay -lpspge -lpspctrl -lpspsdk -lc -lpspnet -lpspnet_inet -lpspnet_apctl -lpspnet_resolver -lpsputility -lpspuser -lpspkernel -o netsample.elf
psp-fixup-imports netsample.elf
psp-prxgen netsample.elf netsample.prx
rm exports.c
|
And I get a message:
main.c: in function 'start_server'
main.c:115: warning: passing argument 3 of 'accept' from incompatible pointer type.
This is line 115:
Code: |
new = accept(sock, (struct sockaddr *) &client, &size);
|
And no EBOOT.PBP is created.
So please help. What do I need to change to get the first sample to run or the second to compile properly?
Or maybe someone can send something similar (that works).
Thanks. |
|
Back to top |
|
|
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Tue May 27, 2008 5:45 am Post subject: |
|
|
for your warning :
it is because size should be a socklen_t and not size_t, (I guess it is the same).
As it is a prx, it is normal that you don't have an eboot. You should have a file like xxxx.prx ( may be netsample.prx) _________________ --pspZorba--
NO to K1.5 ! |
|
Back to top |
|
|
r0b3rt
Joined: 26 May 2008 Posts: 17
|
Posted: Tue May 27, 2008 6:32 pm Post subject: |
|
|
Hi.
socklen_t fixed the warning (thanks!) but I'm still a bit confused. EBOOT.PBP on PSP is like *.exe on windows. So if I want to run the sample I need a EBOOT.PBP. If I just copy prx and try to run it from XMB I get "Corrupted Data".
So do I need another main.c and a makefile, which will load functions from the prx?
And if so, why is there no module_start in the main.c, but there is a main function. And in exports.exp functions make_socket, start_server and connect_to_apctl are not mentioned.
I also tried to add "EXTRA_TARGETS = EBOOT.PBP" to the Makefile and EBOOT.PBP is built, but when I try to run it from XMB, my PSP freezes.
Thanks! |
|
Back to top |
|
|
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Tue May 27, 2008 9:12 pm Post subject: |
|
|
A prx is like a dll and yes you are right the eboot is like a windows exe.
Normally you need an eboot (another main.c, the prx stubs and makefile) to load end execute the functions contained in the prx.
But yes you should export the functions you want to use.
I don't know this SDK sample. I will try to have a look at it tonight if nobody else answered before. _________________ --pspZorba--
NO to K1.5 ! |
|
Back to top |
|
|
r0b3rt
Joined: 26 May 2008 Posts: 17
|
Posted: Tue May 27, 2008 9:37 pm Post subject: |
|
|
I'll paste the code from the sample (.../pspdev/psp/sdk/samples/net/simple_prx) so it's all here for anyone who wants to help.
Main.c:
Code: |
/*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* main.c - Simple prx based network example. Must load the net modules
* before running.
*
* Copyright (c) 2005 James F <tyranid@gmail.com>
* Some small parts (c) 2005 PSPPet
*
* $Id: main.c 1887 2006-05-01 03:54:06Z jim $
* $HeadURL: svn://svn.ps2dev.org/psp/trunk/pspsdk/src/samples/net/simple_prx/main.c $
*/
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdio.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>
#define MODULE_NAME "NetSample"
#define HELLO_MSG "Hello there. Type away.\r\n"
PSP_MODULE_INFO(MODULE_NAME, 0, 1, 1);
PSP_MAIN_THREAD_NAME("NetSample");
#define SERVER_PORT 23
int make_socket(uint16_t port)
{
int sock;
int ret;
struct sockaddr_in name;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
return -1;
}
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
if(ret < 0)
{
return -1;
}
return sock;
}
/* Start a simple tcp echo server */
void start_server(const char *szIpAddr)
{
int ret;
int sock;
int new = -1;
struct sockaddr_in client;
size_t size;
int readbytes;
char data[1024];
fd_set set;
fd_set setsave;
/* Create a socket for listening */
sock = make_socket(SERVER_PORT);
if(sock < 0)
{
printf("Error creating server socket\n");
return;
}
ret = listen(sock, 1);
if(ret < 0)
{
printf("Error calling listen\n");
return;
}
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
FD_ZERO(&set);
FD_SET(sock, &set);
setsave = set;
while(1)
{
int i;
set = setsave;
if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
{
printf("select error\n");
return;
}
for(i = 0; i < FD_SETSIZE; i++)
{
if(FD_ISSET(i, &set))
{
int val = i;
if(val == sock)
{
new = accept(sock, (struct sockaddr *) &client, &size);
if(new < 0)
{
printf("Error in accept %s\n", strerror(errno));
close(sock);
return;
}
printf("New connection %d from %s:%d\n", val,
inet_ntoa(client.sin_addr),
ntohs(client.sin_port));
write(new, HELLO_MSG, strlen(HELLO_MSG));
FD_SET(new, &setsave);
}
else
{
readbytes = read(val, data, sizeof(data));
if(readbytes <= 0)
{
printf("Socket %d closed\n", val);
FD_CLR(val, &setsave);
close(val);
}
else
{
write(val, data, readbytes);
printf("%.*s", readbytes, data);
}
}
}
}
}
close(sock);
}
/* 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;
}
/* Simple thread */
int main(int argc, char **argv)
{
int err;
do
{
if((err = pspSdkInetInit()))
{
printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
break;
}
if(connect_to_apctl(1))
{
// connected, get my IPADDR and run test
char szMyIPAddr[32];
if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
strcpy(szMyIPAddr, "unknown IP address");
start_server(szMyIPAddr);
}
}
while(0);
sceKernelSleepThread();
return 0;
}
int module_stop(SceSize args, void *argp)
{
(void) pspSdkInetTerm();
return 0;
}
|
exports.exp:
Code: |
# Define the exports for the prx
PSP_BEGIN_EXPORTS
# These four lines are mandatory (although you can add other functions like module_stop)
# syslib is a psynonym for the single mandatory export.
PSP_EXPORT_START(syslib, 0, 0x8000)
PSP_EXPORT_FUNC(module_start)
PSP_EXPORT_FUNC(module_stop)
PSP_EXPORT_VAR(module_info)
PSP_EXPORT_END
PSP_END_EXPORTS
|
Makefile:
Code: |
TARGET = netsample
OBJS = main.o
PRX_EXPORTS=exports.exp
BUILD_PRX=1
INCDIR =
CFLAGS = -O0 -G0 -Wall -g
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
LIBDIR =
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
|
|
|
Back to top |
|
|
Insert_witty_name
Joined: 10 May 2006 Posts: 376
|
Posted: Wed May 28, 2008 2:18 am Post subject: |
|
|
You'd be better off converting the 'simple' sample to 3xx from 1.5.
The simple_prx is a sample that shows how to create the 'simple' sample as a prx.
Here you go:
Code: | /*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* main.c - Simple elf based network example.
*
* Copyright (c) 2005 James F <tyranid@gmail.com>
* Some small parts (c) 2005 PSPPet
*
* $Id: main.c 1887 2006-05-01 03:54:06Z jim $
* $HeadURL: svn://svn.ps2dev.org/psp/trunk/pspsdk/src/samples/net/simple/main.c $
*/
#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 <psputility.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
#define printf pspDebugScreenPrintf
#define MODULE_NAME "NetSample"
#define HELLO_MSG "Hello there. Type away.\r\n"
PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 1, 1);
PSP_HEAP_SIZE_KB(20480);
static int running = 1;
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
running = 0;
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;
}
#define SERVER_PORT 23
int make_socket(uint16_t port)
{
int sock;
int ret;
struct sockaddr_in name;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
return -1;
}
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
if(ret < 0)
{
return -1;
}
return sock;
}
/* Start a simple tcp echo server */
void start_server(const char *szIpAddr)
{
int ret;
int sock;
int new = -1;
struct sockaddr_in client;
socklen_t size;
int readbytes;
char data[1024];
fd_set set;
fd_set setsave;
/* Create a socket for listening */
sock = make_socket(SERVER_PORT);
if(sock < 0)
{
printf("Error creating server socket\n");
return;
}
ret = listen(sock, 1);
if(ret < 0)
{
printf("Error calling listen\n");
return;
}
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
FD_ZERO(&set);
FD_SET(sock, &set);
setsave = set;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while(running)
{
int i;
set = setsave;
if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
{
printf("select error\n");
return;
}
for(i = 0; i < FD_SETSIZE; i++)
{
if(FD_ISSET(i, &set))
{
int val = i;
if(val == sock)
{
new = accept(sock, (struct sockaddr *) &client, &size);
if(new < 0)
{
printf("Error in accept %s\n", strerror(errno));
close(sock);
return;
}
printf("New connection %d from %s:%d\n", val,
inet_ntoa(client.sin_addr),
ntohs(client.sin_port));
write(new, HELLO_MSG, strlen(HELLO_MSG));
FD_SET(new, &setsave);
}
else
{
readbytes = read(val, data, sizeof(data));
if(readbytes <= 0)
{
printf("Socket %d closed\n", val);
FD_CLR(val, &setsave);
close(val);
}
else
{
write(val, data, readbytes);
printf("%.*s", readbytes, data);
}
}
}
}
}
close(sock);
}
/* 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 (running)
{
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;
}
/* Simple thread */
int main(int argc, char **argv)
{
SetupCallbacks();
pspDebugScreenInit();
sceUtilityLoadModule(PSP_MODULE_NET_COMMON);
sceUtilityLoadModule(PSP_MODULE_NET_INET);
int err;
while(running)
{
if((err = pspSdkInetInit()))
{
printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
break;
}
if(connect_to_apctl(1))
{
// connected, get my IPADDR and run test
char szMyIPAddr[32];
if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
strcpy(szMyIPAddr, "unknown IP address");
start_server(szMyIPAddr);
}
}
pspSdkInetTerm();
sceKernelExitGame();
return 0;
} |
Code: | TARGET = netsample
OBJS = main.o
INCDIR =
CFLAGS = -O0 -G0 -Wall -g
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
BUILD_PRX = 1
LIBDIR =
EXTRA_TARGETS = EBOOT.PBP
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak |
|
|
Back to top |
|
|
r0b3rt
Joined: 26 May 2008 Posts: 17
|
Posted: Wed May 28, 2008 6:12 pm Post subject: |
|
|
Thanks for the help, the code works great.
Perhaps it would be good to put it in sdk/samples/net. |
|
Back to top |
|
|
illostos
Joined: 14 May 2008 Posts: 7
|
Posted: Wed Jun 11, 2008 5:27 pm Post subject: |
|
|
does anyone knows how to send data over the network? we try to create a remote control for a mobile robot with a wlan host on it, but we need to send him data (with the arrow keys forward, backward, left, right and with the L+R Buttons slower and faster)
but were in the code can i implement this?
And before we can send something, the other side, in our case the ct'bot from heise has to accept the socket connection?
with when i read this right the socket, the ip adress, what is &client and &size in this case? |
|
Back to top |
|
|
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Mon Jul 05, 2010 9:31 am Post subject: |
|
|
Insert_witty_name wrote: | You'd be better off converting the 'simple' sample to 3xx from 1.5.
The simple_prx is a sample that shows how to create the 'simple' sample as a prx.
Here you go:
Code: | /*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* main.c - Simple elf based network example.
*
* Copyright (c) 2005 James F <tyranid@gmail.com>
* Some small parts (c) 2005 PSPPet
*
* $Id: main.c 1887 2006-05-01 03:54:06Z jim $
* $HeadURL: svn://svn.ps2dev.org/psp/trunk/pspsdk/src/samples/net/simple/main.c $
*/
#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 <psputility.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
#define printf pspDebugScreenPrintf
#define MODULE_NAME "NetSample"
#define HELLO_MSG "Hello there. Type away.\r\n"
PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 1, 1);
PSP_HEAP_SIZE_KB(20480);
static int running = 1;
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
running = 0;
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;
}
#define SERVER_PORT 23
int make_socket(uint16_t port)
{
int sock;
int ret;
struct sockaddr_in name;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
return -1;
}
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
if(ret < 0)
{
return -1;
}
return sock;
}
/* Start a simple tcp echo server */
void start_server(const char *szIpAddr)
{
int ret;
int sock;
int new = -1;
struct sockaddr_in client;
socklen_t size;
int readbytes;
char data[1024];
fd_set set;
fd_set setsave;
/* Create a socket for listening */
sock = make_socket(SERVER_PORT);
if(sock < 0)
{
printf("Error creating server socket\n");
return;
}
ret = listen(sock, 1);
if(ret < 0)
{
printf("Error calling listen\n");
return;
}
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
FD_ZERO(&set);
FD_SET(sock, &set);
setsave = set;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while(running)
{
int i;
set = setsave;
if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
{
printf("select error\n");
return;
}
for(i = 0; i < FD_SETSIZE; i++)
{
if(FD_ISSET(i, &set))
{
int val = i;
if(val == sock)
{
new = accept(sock, (struct sockaddr *) &client, &size);
if(new < 0)
{
printf("Error in accept %s\n", strerror(errno));
close(sock);
return;
}
printf("New connection %d from %s:%d\n", val,
inet_ntoa(client.sin_addr),
ntohs(client.sin_port));
write(new, HELLO_MSG, strlen(HELLO_MSG));
FD_SET(new, &setsave);
}
else
{
readbytes = read(val, data, sizeof(data));
if(readbytes <= 0)
{
printf("Socket %d closed\n", val);
FD_CLR(val, &setsave);
close(val);
}
else
{
write(val, data, readbytes);
printf("%.*s", readbytes, data);
}
}
}
}
}
close(sock);
}
/* 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 (running)
{
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;
}
/* Simple thread */
int main(int argc, char **argv)
{
SetupCallbacks();
pspDebugScreenInit();
sceUtilityLoadModule(PSP_MODULE_NET_COMMON);
sceUtilityLoadModule(PSP_MODULE_NET_INET);
int err;
while(running)
{
if((err = pspSdkInetInit()))
{
printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
break;
}
if(connect_to_apctl(1))
{
// connected, get my IPADDR and run test
char szMyIPAddr[32];
if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
strcpy(szMyIPAddr, "unknown IP address");
start_server(szMyIPAddr);
}
}
pspSdkInetTerm();
sceKernelExitGame();
return 0;
} |
Code: | TARGET = netsample
OBJS = main.o
INCDIR =
CFLAGS = -O0 -G0 -Wall -g
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
BUILD_PRX = 1
LIBDIR =
EXTRA_TARGETS = EBOOT.PBP
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak |
|
This works. But its freezing on exit!
Thats weird, since the running is supposed to be set to 0.
Any thoughts on that?
(Sorry for the post on the other thread) |
|
Back to top |
|
|
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Mon Jul 05, 2010 9:53 am Post subject: |
|
|
Fixed.
Code: | /*
* PSP Software Development Kit - http://www.pspdev.org
* -----------------------------------------------------------------------
* Licensed under the BSD license, see LICENSE in PSPSDK root for details.
*
* main.c - Simple elf based network example.
*
* Copyright (c) 2005 James F <tyranid@gmail.com>
* Some small parts (c) 2005 PSPPet
*
* $Id: main.c 1887 2006-05-01 03:54:06Z jim $
* $HeadURL: svn://svn.ps2dev.org/psp/trunk/pspsdk/src/samples/net/simple/main.c $
*/
#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 <psputility.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
#define printf pspDebugScreenPrintf
#define MODULE_NAME "NetSample"
#define HELLO_MSG "Hello there. Type away.\r\n"
PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 1, 1);
PSP_HEAP_SIZE_KB(20480);
static int running = 1;
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
running = 0;
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;
}
#define SERVER_PORT 23
int make_socket(uint16_t port)
{
int sock;
int ret;
struct sockaddr_in name;
sock = socket(PF_INET, SOCK_STREAM, 0);
if(sock < 0)
{
return -1;
}
name.sin_family = AF_INET;
name.sin_port = htons(port);
name.sin_addr.s_addr = htonl(INADDR_ANY);
ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
if(ret < 0)
{
return -1;
}
return sock;
}
/* Start a simple tcp echo server */
void start_server(const char *szIpAddr)
{
int ret;
int sock;
int new = -1;
struct sockaddr_in client;
socklen_t size;
int readbytes;
char data[1024];
fd_set set;
fd_set setsave;
/* Create a socket for listening */
sock = make_socket(SERVER_PORT);
if(sock < 0)
{
printf("Error creating server socket\n");
return;
}
ret = listen(sock, 1);
if(ret < 0)
{
printf("Error calling listen\n");
return;
}
printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
FD_ZERO(&set);
FD_SET(sock, &set);
setsave = set;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
while(running)
{
int i;
set = setsave;
if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
{
printf("select error\n");
return;
}
for(i = 0; i < FD_SETSIZE; i++)
{
if(FD_ISSET(i, &set))
{
int val = i;
if(val == sock)
{
new = accept(sock, (struct sockaddr *) &client, &size);
if(new < 0)
{
printf("Error in accept %s\n", strerror(errno));
close(sock);
return;
}
printf("New connection %d from %s:%d\n", val,
inet_ntoa(client.sin_addr),
ntohs(client.sin_port));
write(new, HELLO_MSG, strlen(HELLO_MSG));
FD_SET(new, &setsave);
}
else
{
readbytes = read(val, data, sizeof(data));
if(readbytes <= 0)
{
printf("Socket %d closed\n", val);
FD_CLR(val, &setsave);
close(val);
}
else
{
write(val, data, readbytes);
printf("%.*s", readbytes, data);
}
}
}
}
}
close(sock);
}
/* 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 (running)
{
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;
}
/* Simple thread */
int main(int argc, char **argv)
{
SetupCallbacks();
pspDebugScreenInit();
sceUtilityLoadModule(PSP_MODULE_NET_COMMON);
sceUtilityLoadModule(PSP_MODULE_NET_INET);
int err;
while(running)
{
if((err = pspSdkInetInit()))
{
printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
break;
}
if(connect_to_apctl(1))
{
// connected, get my IPADDR and run test
char szMyIPAddr[32];
if (sceNetApctlGetInfo(8, szMyIPAddr) != 0)
strcpy(szMyIPAddr, "unknown IP address");
start_server(szMyIPAddr);
}
}
pspSdkInetTerm();
sceKernelSleepThread();
return 0;
} |
|
|
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
|