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 

Send broadcast message from a server

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



Joined: 19 May 2008
Posts: 28

PostPosted: Sat Jun 14, 2008 12:40 am    Post subject: Send broadcast message from a server Reply with quote

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
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Sat Jun 14, 2008 3:15 am    Post subject: Reply with quote

take a look at UDP
_________________
--pspZorba--
NO to K1.5 !
Back to top
View user's profile Send private message
steffven



Joined: 19 May 2008
Posts: 28

PostPosted: Sat Jun 14, 2008 6:54 am    Post subject: Reply with quote

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
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Sun Jun 15, 2008 12:25 am    Post subject: Reply with quote

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
View user's profile Send private message
steffven



Joined: 19 May 2008
Posts: 28

PostPosted: Sun Jun 15, 2008 3:36 am    Post subject: Reply with quote

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
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Sun Jun 15, 2008 5:09 am    Post subject: Reply with quote

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
View user's profile Send private message
steffven



Joined: 19 May 2008
Posts: 28

PostPosted: Sun Jun 15, 2008 9:04 pm    Post subject: Reply with quote

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
View user's profile Send private message
pspZorba



Joined: 22 Sep 2007
Posts: 156
Location: NY

PostPosted: Mon Jun 16, 2008 12:13 am    Post subject: Reply with quote

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
View user's profile Send private message
steffven



Joined: 19 May 2008
Posts: 28

PostPosted: Mon Jun 16, 2008 3:07 am    Post subject: Reply with quote

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
View user's profile Send private message
steffven



Joined: 19 May 2008
Posts: 28

PostPosted: Mon Jun 16, 2008 3:15 am    Post subject: Reply with quote

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
View user's profile Send private message
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