Send broadcast message from a server

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Send broadcast message from a server

Post by steffven »

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?
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

take a look at UDP
--pspZorba--
NO to K1.5 !
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

Code: Select all

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?!?
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

you use the variable

Code: Select all

struct hostent *he; 
in

Code: Select all

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: Select all

  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 !
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

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: Select all

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);
}
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

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 !
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

Code: Select all

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 -.-
pspZorba
Posts: 156
Joined: Sat Sep 22, 2007 11:45 am
Location: NY

Post by pspZorba »

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 !
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

pspZorba wrote:name.sin_family = PF_INET; <= AF_INT ( A for Address)
socket(PF_INET, ..) <= correct (P for Protocol)
Corrected.

Code: Select all

void test&#40;&#41;
&#123;
	int sockfd;
   struct sockaddr_in name;
	char sendnumber&#91;8&#93; = "pspiprec";
	int leng = 8;
	int broadcast = 1;
	int abc = 0;
	
	name.sin_family = AF_INET;
	name.sin_port = htons&#40;673&#41;;
	name.sin_addr.s_addr = inet_addr&#40;"255.255.255.255"&#41;;
	
	sockfd = socket&#40;PF_INET, SOCK_DGRAM, 0&#41;;
		
	abc = setsockopt&#40;sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof&#40;broadcast&#41;&#41;; //&#40;char*&#41;
  printf&#40;"%d", abc&#41;; //<--- Returns 0
	abc = sendto&#40;sockfd, sendnumber, strlen&#40;sendnumber&#41;, 0, &#40;struct sockaddr *&#41; &name, leng&#41;;
  printf&#40;"%d", abc&#41;; //<--- Returns 1 !!!
	sceKernelDelayThread&#40;500 * 1000&#41;;
  
	close&#40;sockfd&#41;;

&#125;
So there's my fault, but I don't understand what's wrong^^
steffven
Posts: 28
Joined: Mon May 19, 2008 7:37 pm

Post by steffven »

Yeah I got it!

Code: Select all

void test&#40;&#41;
&#123;
	int sockfd;
   struct sockaddr_in name;
	char sendnumber&#91;8&#93; = "pspiprec";
	int leng = 8;
	int broadcast = 1;
	int abc = 0;
	
	name.sin_family = AF_INET;
	name.sin_port = htons&#40;673&#41;;
	name.sin_addr.s_addr = inet_addr&#40;"255.255.255.255"&#41;;
	
	sockfd = socket&#40;PF_INET, SOCK_DGRAM, 0&#41;;
		
	abc = setsockopt&#40;sockfd, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof&#40;broadcast&#41;&#41;; //&#40;char*&#41;
  printf&#40;"%d", abc&#41;;
	abc = sendto&#40;sockfd, sendnumber, leng, 0, &#40;struct sockaddr *&#41; &name, sizeof&#40;name&#41;&#41;; // Here was my fault! Not leng but sizeof&#40;name&#41;
  printf&#40;"%d", abc&#41;;
	sceKernelDelayThread&#40;500 * 1000&#41;;
  
	close&#40;sockfd&#41;;

&#125;
Stupid fault xD
But thanks to you pspZorba for all your help!
Post Reply