 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Mon Jul 05, 2010 11:23 am Post subject: Threaded UDP client crashing |
|
|
I made a simple UDP client based on the sdk's sample/net/simple_prx (which is server side). It have worked very well.
But when i tried to run the client on a thread, it runs the main while() a few times (around 40) and then freezes. I cant exit.
| 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 <psputility.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <errno.h>
#include <fcntl.h>
#define printf pspDebugScreenPrintf
#define sleep sceKernelDelayThread
#define MODULE_NAME "NetSample"
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;
pspSdkInetTerm();
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;
}
int create_client()
{
char ip[] = "192.168.0.101";
int porta = 30666;
printf("\n\n\n\nOk, ip %s port: %d...\n\n", ip, porta);
int sock;
struct sockaddr_in address;
//Zero mem
memset(&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(porta);
inet_aton(ip, &address.sin_addr);
sock = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
//Non-blocking
int flags;
flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
int sizeof_msg = 7;
char message[7] = "TESTMSG";
while(running)
{
if(sendto(sock,message,sizeof_msg,0,(struct sockaddr *)&address,sizeof(address)) < 0) printf("failure on sendto()\n");
sleep(10000);
}
close(sock);
return 0;
}
/* 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;
}
int thread_client(void)
{
int thid = 0;
thid = sceKernelCreateThread("thread_cliente", create_client, 0x11, 0x100000, PSP_THREAD_ATTR_USER, 0);
if(thid >= 0) {
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
/* Simple thread */
int main(int argc, char **argv)
{
SceUID thid;
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");
//Starts main thread
thid = thread_client();
}
}
sleep(1000000);
sceKernelSleepThread();
return 0;
}
|
It prints "failure on sendto()" around 40 times and then freezes.
Also, i saw some examples where the modules are loaded in kernel mode, and some where they are loaded in user mode. I'm a little confused now.
Thanks in advance. |
|
| Back to top |
|
 |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Mon Jul 05, 2010 1:43 pm Post subject: |
|
|
Grrr! This is so embarrassing! I'm studying the samples, but they dont seem to work also!
Does anyone have a simple threaded networking working sample?
Many thanks. |
|
| Back to top |
|
 |
Criptych
Joined: 12 Sep 2009 Posts: 79
|
Posted: Tue Jul 06, 2010 1:53 am Post subject: |
|
|
Are you sure it's connecting to the network? I only got "failure on sendto" messages after I turned off the WLAN switch to keep it from connecting. _________________ PSP-2000 // CFW: 5.50 GEN-D2 ...and not upgrading until OFW supports homebrew!
(But I did downgrade to 1.50 with TimeMachine...)
"I want you to tell me how the machine makes you feel." |
|
| Back to top |
|
 |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Tue Jul 06, 2010 4:52 am Post subject: |
|
|
Thanks for the attention.
You mean it's working well on your psp?
I'm sure my wlan is ON, it connects to apctl fine. Thats just weird...
Is it possible that Makefile is making the difference?
And even if it were not connecting, it should not freeze, right?
Thanks! |
|
| Back to top |
|
 |
Criptych
Joined: 12 Sep 2009 Posts: 79
|
Posted: Tue Jul 06, 2010 7:45 am Post subject: |
|
|
| lgnr wrote: | | You mean it's working well on your psp? |
I did have to move the AP connection outside of the loop in main (or else it would crash immediately after connecting), but other than that it's working.
| lgnr wrote: | Is it possible that Makefile is making the difference?
And even if it were not connecting, it should not freeze, right? |
I don't really know. The Makefile shouldn't be at fault unless you're using it to #define things in a weird way, or it's linking with the wrong libraries. I just used a modified SDK sample Makefile and it worked fine. Maybe you're using a different SDK version (mine is R2494)? _________________ PSP-2000 // CFW: 5.50 GEN-D2 ...and not upgrading until OFW supports homebrew!
(But I did downgrade to 1.50 with TimeMachine...)
"I want you to tell me how the machine makes you feel." |
|
| Back to top |
|
 |
lgnr
Joined: 17 Dec 2009 Posts: 39
|
Posted: Tue Jul 06, 2010 10:02 am Post subject: |
|
|
I should have been seriously brain damaged.
Thank you very much. I completely removed the loop in main().
I still get some "failure on sendto()" but it stops failing and starts sending after a while. Is that normal?
I mean, should i sleep() somewhere to wait for the modules to load?
Thanks again :) |
|
| 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
|