I am designing a system which will broadcast a message to all the nodes in a cluster.And the receiving node should send a feed back to the broadcasting node.When i get a single feedback from any of the receiving node, the broadcasting node should stop receiving the feedback.With that feedback i am doing some operations.This both actions should be implemented in a single program.As i am new to socket programming i don't know how to do tat.Can someone give me the code for this???
Thanx in advance

Dani AI

Generated

A simple, reliable pattern is: use UDP broadcast (or multicast) to announce a request, have every node listening on that port reply with a unicast UDP message back to the broadcaster, and have the broadcaster wait for the first reply (then stop). That meets the “stop after one feedback” requirement and keeps the code small. As asked for code and as and hinted, start small and try it — the pattern below is purposely minimal so it’s easy to experiment with.

Conceptual steps:

  • Broadcaster: create a UDP socket, enable SO_BROADCAST, sendto() the broadcast address (use the subnet broadcast rather than always 255.255.255.255), then select()/recvfrom() with a timeout. When the first recvfrom() returns, process it and close/ignore further replies.
  • Responder: bind a UDP socket to the chosen port on INADDR_ANY, recvfrom() the request, and sendto() a reply back to the sender address returned by recvfrom().
  • Single program: implement both roles and choose with a command-line flag (e.g., -b for broadcast, -r for responder).

Minimal C pattern (core calls):

/* broadcaster: send broadcast and wait for first reply */
int s = socket(AF_INET, SOCK_DGRAM, 0);
int one = 1;
setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
struct sockaddr_in b = {0};
b.sin_family = AF_INET;
b.sin_port = htons(PORT);
inet_aton("255.255.255.255", &b.sin_addr);
sendto(s, msg, msglen, 0, (struct sockaddr*)&b, sizeof b);

/* wait for one reply with timeout */
fd_set rfds; FD_ZERO(&rfds); FD_SET(s, &rfds);
struct timeval tv = {5, 0};
if (select(s+1, &rfds, NULL, NULL, &tv) > 0) {
  socklen_t alen = sizeof peer;
  recvfrom(s, buf, sizeof buf, 0, (struct sockaddr*)&peer, &alen);
  /* first reply received */
}
/* responder: bind, recv and reply */
int r = socket(AF_INET, SOCK_DGRAM, 0);
int reuse = 1; setsockopt(r, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
struct sockaddr_in a = {0};
a.sin_family = AF_INET; a.sin_port = htons(PORT); a.sin_addr.s_addr = INADDR_ANY;
bind(r, (struct sockaddr*)&a, sizeof a);
socklen_t alen = sizeof peer;
recvfrom(r, buf, sizeof buf, 0, (struct sockaddr*)&peer, &alen);
sendto(r, reply, rlen, 0, (struct sockaddr*)&peer, alen);

Notes & pitfalls: broadcasts often don’t cross routers and may be blocked by firewalls — use multicast or a rendezvous server for cross-subnet discovery. Include a tiny transaction ID in the payload so replies can be matched to the correct request. Use timeouts (select) to avoid hanging, and consider TCP only if you need guaranteed delivery. For a clear, practical tutorial on sockets, see Beej's Guide to Network Programming.

Recommended Answers

All 2 Replies

Can someone give me the code for this???

come on, man, you've been here long enough now to know better than that

the programming is all about implementing the thoughts you have in your mind..i think u should try first...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.