 |
forums.ps2dev.org Homebrew PS2, PSP & PS3 Development Discussions
|
| View previous topic :: View next topic |
| Author |
Message |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Sat Jun 14, 2008 12:40 am Post subject: Send broadcast message from a server |
|
|
I set up a tcp server on my psp.
Now i want to send a broadcast message (192.168.2.255), but how?
It's no problem for me to send messages to registered clients, but i don't know how to send broadcast messages.
Can someone plz help me? |
|
| Back to top |
|
 |
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Sat Jun 14, 2008 3:15 am Post subject: |
|
|
take a look at UDP _________________ --pspZorba--
NO to K1.5 ! |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Sat Jun 14, 2008 6:54 am Post subject: |
|
|
| Code: | void test()
{
int sockfd;
struct sockaddr_in their_addr;
struct hostent *he;
int numbytes;
int broadcast = 1;
char szMyIPAddr[32];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast,
sizeof broadcast) == -1) {
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(SERVER_PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
numbytes=sendto(sockfd, szMyIPAddr, strlen(szMyIPAddr), 0,
(struct sockaddr *)&their_addr, sizeof their_addr);
close(sockfd);
} |
That's all I got for now, but this code won't work, why?
As soon as this code is entered, the WLAN Light goes off and my psp gets stuck?!? |
|
| Back to top |
|
 |
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Sun Jun 15, 2008 12:25 am Post subject: |
|
|
you use the variable
| Code: | | struct hostent *he; |
in
| Code: | | their_addr.sin_addr = *((struct in_addr *)he->h_addr); |
without initializing it, this probably why you have a crash.
same for : szMyIPAddr
---------------------
I am really far from being a UDP specialist (actually only played with it long long ago), but if I remember well, the address you should send your to-be-broadcasted message is something like x.x.x.255 or may be 255.255.255.255
on the client side the ip is (still if I remember well) x.x.x.0 or 0.0.0.0
[edit]
btw in
| Code: | | sockfd = socket(AF_INET, SOCK_DGRAM, 0); |
you should use PF_INET instead of AF_INET (most often PF_INET equals AF_INET but it is not guaranted)
[/edit] _________________ --pspZorba--
NO to K1.5 ! |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Sun Jun 15, 2008 3:36 am Post subject: |
|
|
Ok, i rewrote the code but it still won't work; my psp doesn't hang anymore, but i don't receive anything. My Code:
| Code: | void test()
{
int sockfd;
struct sockaddr_in name;
char sendnumber[10] = "pspiprec";
name.sin_family = AF_INET;
name.sin_port = htons(673);
name.sin_addr.s_addr = INADDR_BROADCAST;
sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
int broadcast = 1;
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, 1);
connect(sockfd, (struct sockaddr *) &name, sizeof(name));
send(sockfd, sendnumber, sizeof(sendnumber), 0);
sceKernelDelayThread(500 * 1000);
close(sockfd);
} |
|
|
| Back to top |
|
 |
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Sun Jun 15, 2008 5:09 am Post subject: |
|
|
I remember now that you have to use sendTo and recvFrom (and not send and recv)
And as far as I remember there is neither "connect" nor "listen/accept" to do (since you "sendto" to everybody and "recvFrom" from anybody ) but there is still the bind
so on the sender side you should have someting like:
-creation of the socket
-sendTo msg to 255.255.255.255 : port
on the receivers side you should have something like
-creation of the socket
-bind socket to o.o.o.o : port
- recvFrom from socket _________________ --pspZorba--
NO to K1.5 ! |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Sun Jun 15, 2008 9:04 pm Post subject: |
|
|
| Code: | void test()
{
int sockfd;
struct sockaddr_in name;
char sendnumber[8] = "pspiprec";
int leng = 8;
int broadcast = 1;
name.sin_family = PF_INET;
name.sin_port = htons(673);
name.sin_addr.s_addr = inet_addr("255.255.255.255");
sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (char*)&broadcast, 1);
sendto(sockfd, sendnumber, strlen(sendnumber), 0, (struct sockaddr *) &name, leng);
sceKernelDelayThread(500 * 1000);
close(sockfd);
} |
Still not working and I have no idea why -.- |
|
| Back to top |
|
 |
pspZorba
Joined: 22 Sep 2007 Posts: 156 Location: NY
|
Posted: Mon Jun 16, 2008 12:13 am Post subject: |
|
|
Ok at first sight that looks good for this part.
What does return sendTo ?
and could you provide the code that should receive the message?
is there a firewall ? if yes does it allow UDP message on the port 673?
btw you should choose a port above 1024, this port could be already used by the system (isn't it used by MS Exchange?)
to be clean:
name.sin_family = PF_INET; <= AF_INT ( A for Address)
socket(PF_INET, ..) <= correct (P for Protocol)
but it is not the problem _________________ --pspZorba--
NO to K1.5 ! |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Mon Jun 16, 2008 3:07 am Post subject: |
|
|
| pspZorba wrote: | name.sin_family = PF_INET; <= AF_INT ( A for Address)
socket(PF_INET, ..) <= correct (P for Protocol) |
Corrected.
| Code: | void test()
{
int sockfd;
struct sockaddr_in name;
char sendnumber[8] = "pspiprec";
int leng = 8;
int broadcast = 1;
int abc = 0;
name.sin_family = AF_INET;
name.sin_port = htons(673);
name.sin_addr.s_addr = inet_addr("255.255.255.255");
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
abc = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)); //(char*)
printf("%d", abc); //<--- Returns 0
abc = sendto(sockfd, sendnumber, strlen(sendnumber), 0, (struct sockaddr *) &name, leng);
printf("%d", abc); //<--- Returns 1 !!!
sceKernelDelayThread(500 * 1000);
close(sockfd);
} |
So there's my fault, but I don't understand what's wrong^^ |
|
| Back to top |
|
 |
steffven
Joined: 19 May 2008 Posts: 28
|
Posted: Mon Jun 16, 2008 3:15 am Post subject: |
|
|
Yeah I got it!
| Code: | void test()
{
int sockfd;
struct sockaddr_in name;
char sendnumber[8] = "pspiprec";
int leng = 8;
int broadcast = 1;
int abc = 0;
name.sin_family = AF_INET;
name.sin_port = htons(673);
name.sin_addr.s_addr = inet_addr("255.255.255.255");
sockfd = socket(PF_INET, SOCK_DGRAM, 0);
abc = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)); //(char*)
printf("%d", abc);
abc = sendto(sockfd, sendnumber, leng, 0, (struct sockaddr *) &name, sizeof(name)); // Here was my fault! Not leng but sizeof(name)
printf("%d", abc);
sceKernelDelayThread(500 * 1000);
close(sockfd);
} |
Stupid fault xD
But thanks to you pspZorba for all your help! |
|
| 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
|