Send broadcast message from a server
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?
			
			
									
									
						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?
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);
}As soon as this code is entered, the WLAN Light goes off and my psp gets stuck?!?
you use  the variable   
in   
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
you should use PF_INET instead of AF_INET (most often PF_INET equals AF_INET but it is not guaranted)
[/edit]
			
			
									
									Code: Select all
struct hostent *he; Code: Select all
their_addr.sin_addr = *((struct in_addr *)he->h_addr); 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); [/edit]
--pspZorba--
NO to K1.5 !
						NO to K1.5 !
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);
}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
			
			
									
									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 !
						NO to K1.5 !
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);
}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
			
			
									
									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 !
						NO to K1.5 !
Corrected.pspZorba wrote:name.sin_family = PF_INET; <= AF_INT ( A for Address)
socket(PF_INET, ..) <= correct (P for Protocol)
Code: Select all
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);
}Yeah I got it!
Stupid fault xD
But thanks to you pspZorba for all your help!
			
			
									
									
						Code: Select all
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);
}But thanks to you pspZorba for all your help!