473,471 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Socket broadcasting

6 New Member
Dear Mr,

I wrote one sample socket in linux, it broadcast an character to all the machines in my subnet.
I set socket options in udp to broadcast then sendto() the client, but I couldn't receive any thing in client.
I checked my program with several programs, it's same.but I can't receive from broadcast.
What should I do ?

Thanks for you incoming helps.
Aug 20 '07 #1
5 12622
sicarie
4,677 Recognized Expert Moderator Specialist
What did you write your program in?

Can you ping the machines from the computer your program is running on?

What programs did you "check yours against" and how did you check it?

Are you aware of the Networking definition of 'broadcast' and how that's different depending on your situation?
Aug 20 '07 #2
rezamirani
6 New Member
What did you write your program in?

Can you ping the machines from the computer your program is running on?

What programs did you "check yours against" and how did you check it?

Are you aware of the Networking definition of 'broadcast' and how that's different depending on your situation?
Dear Sicarie,
This is my program on both server and client side , please check and tell me more about Network definition od broad cast and different depending on my situation.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>Server Side>>>>>>>>>>>>>>>>>>>
Expand|Select|Wrap|Line Numbers
  1.   #include <stdio.h>
  2.           #include <sys/socket.h>
  3.           #include <arpa/inet.h>
  4.           #include <stdlib.h>
  5.           #include <string.h>
  6.           #include <unistd.h>
  7.           #include <netinet/in.h>
  8.  
  9.           #define BUFFSIZE 255
  10.           void Die(char *mess) { perror(mess); exit(1); }
  11.  
  12. int main(int argc, char *argv[]) {
  13.             int sock;
  14.             struct sockaddr_in echoserver;
  15.             struct sockaddr_in echoclient;
  16.             char buffer[BUFFSIZE];
  17.             unsigned int echolen, clientlen;
  18.             int received = 0;
  19.  
  20.             if (argc != 4) {
  21.               fprintf(stderr, "USAGE: %s <server_ip> <word> <port>\n", argv[0]);
  22.               exit(1);
  23.             }
  24.             /* Create the UDP socket */
  25.             if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  26.               Die("Failed to create socket");
  27.             }
  28.             /* Construct the server sockaddr_in structure */
  29.             memset(&echoserver, 0, sizeof(echoserver));       /* Clear struct */
  30.             echoserver.sin_family = AF_INET;                  /* Internet/IP */
  31.             echoserver.sin_addr.s_addr = inet_addr(argv[1]);  /* IP address */
  32.             echoserver.sin_port = htons(atoi(argv[3]));       /* server port */
  33.  
  34. /*Set socket option to broadcast*/
  35.  int opt = 1;
  36.  setsockopt(sock(), SOL_SOCKET, SO_BROADCAST, &opt, sizeof(int));
  37.  
  38.  /* Send the word to the server */
  39.             echolen = strlen(argv[2]);
  40.             if (sendto(sock, argv[2], echolen, 0,
  41.                        (struct sockaddr *) &echoserver,
  42.                        sizeof(echoserver)) != echolen) {
  43.               Die("Mismatch in number of sent bytes");
  44.             }
  45.  
  46.             close(sock);
  47.             exit(0);
<<<<<<<<<<<<<<<<<<<<<in other side program >>>>>>>>>>>>>>>>>
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.           #include <sys/socket.h>
  3.           #include <arpa/inet.h>
  4.           #include <stdlib.h>
  5.           #include <string.h>
  6.           #include <unistd.h>
  7.           #include <netinet/in.h>
  8.  
  9.           #define BUFFSIZE 255
  10.           void Die(char *mess) { perror(mess); exit(1); }
  11.  
  12. int main(int argc, char *argv[]) {
  13.             int sock;
  14.             struct sockaddr_in echoserver;
  15.             struct sockaddr_in echoclient;
  16.             char buffer[BUFFSIZE];
  17.             unsigned int echolen, clientlen, serverlen;
  18.             int received = 0;
  19.  
  20.             if (argc != 2) {
  21.               fprintf(stderr, "USAGE: %s <port>\n", argv[0]);
  22.               exit(1);
  23.             }
  24. /* Create the UDP socket */
  25.           if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  26.             Die("Failed to create socket");
  27.           }
  28.           /* Construct the server sockaddr_in structure */
  29.           memset(&echoserver, 0, sizeof(echoserver));       /* Clear struct */
  30.           echoserver.sin_family = AF_INET;                  /* Internet/IP */
  31.           echoserver.sin_addr.s_addr = htonl(INADDR_ANY);   /* Any IP address */
  32.           echoserver.sin_port = htons(atoi(argv[1]));       /* server port */
  33.  
  34.           /* Bind the socket */
  35.           serverlen = sizeof(echoserver);
  36.           if (bind(sock, (struct sockaddr *) &echoserver, serverlen) < 0) {
  37.             Die("Failed to bind server socket");
  38.           }
  39.  
  40.  /* Run until cancelled */
  41.             while (1) {
  42.               /* Receive a message from the client */
  43.               clientlen = sizeof(echoclient);
  44.               if ((received = recvfrom(sock, buffer, BUFFSIZE, 0,
  45.                                        (struct sockaddr *) &echoclient,
  46.                                        &clientlen)) < 0) {
  47.                 Die("Failed to receive message");
  48.               }
  49.               fprintf(stderr,
  50.                       "Client connected: %s\n", inet_ntoa(echoclient.sin_addr));
  51.  
  52.             }
  53.           }
  54.  
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>

Please check my mistake, thanks again for incoming helps,
Aug 20 '07 #3
rezamirani
6 New Member
I sniffed packets in other machines that connected to the network, packet received but my receive program work bad and can't receive the packets.
Please checking my receive program "in other side program".

Best Kings.
Aug 20 '07 #4
sicarie
4,677 Recognized Expert Moderator Specialist
I sniffed packets in other machines that connected to the network, packet received but my receive program work bad and can't receive the packets.
Please checking my receive program "in other side program".

Best Kings.
Not sure how many of the networking people here are coders, so since you know you are receiving the packet on the destination computer (but not processing it correctly) I'm going to shift this over to the C/C++ forum - hopefully get a few more ideas over there.
Aug 20 '07 #5
rezamirani
6 New Member
Thanks ,I hope receive any help about this.
Aug 20 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gordon Wetzstein | last post by:
Hello everyone, I have a problem with python sockets. I broadcast a lot of pickled objects with socket. sendto(...), that works. Ireceive them on the other site with socket.recvfrom(16384) The...
8
by: Brad Tilley | last post by:
I have a function that starts a socket server looping continuously listening for connections. It works. Clients can connect to it and send data. The problem is this: I want to get the data that...
1
by: Austin | last post by:
......................... data = .... self.broadcast_ip = 255.255.255.255 UDPSock.sendto(data,(self.broadcast_ip,36)) .......................... These codes run on Windows without errors...
4
by: Alexander Muylaert | last post by:
Hi I their a way I can interupt socket.Receive. I want my multi-threader server to be able to handle requests as wel as Broadcast messages. This broadcasting is done when the socket is not...
5
by: Justin Creasy | last post by:
If this is the wrong group for this posting please let me know and I'll move it. I have an application that has an ArrayList of sockets to clients. For standard one-to-one messages my...
4
by: rs | last post by:
how I the client tell the server that the socket is closed? or this there an even that informs the server that the clients socket is close? Oh, I am using vb.net 2003 Thanks
2
by: Islamegy® | last post by:
Hiiiiiii I'm working on a client server application which work like messnger.. I was able to capture the camera with no problem but now i want my my program to listen to incoming connection and...
5
by: Marco | last post by:
I have a problem with the establishement of a three way handshake through raw socket. The problem is that if in my application client I use the syscall socket(AF_INET,SOCK_RAW,IPPROTO_RAW) the...
1
luckysanj
by: luckysanj | last post by:
Dear Sir, Can you give me idea about Live Radio Broadcasting. I want to implement the Live Radio Broadcasting link on my web page to play radio on live. So i need basic php code for this. Your...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.