int main(int argc, char *argv[]) { char buf[BUFSIZE]; ssize_t bytesread; char hostinfo[BUFSIZE]; int mcastfd; u_buf_t mcastinfo; u_port_t mcastport; u_buf_t senderinfo; if (argc != 3) { fprintf(stderr, "Usage: %s multicast-address multicast-port\n", argv[0]); return 1; } mcastport = (u_port_t)atoi(argv[2]); /* join the multicast group */ if ((mcastfd = u_join(argv[1], mcastport, &mcastinfo)) == -1) { perror("Failed to join multicast group"); return 1; } u_gethostinfo(&mcastinfo, buf, BUFSIZE); fprintf(stderr, "Info: %s\n", buf); fprintf(stderr, "mcastfd is %d\n", mcastfd); /* read information from multicast, send to standard output */ while ((bytesread = u_recvfrom(mcastfd, buf, BUFSIZE, &senderinfo)) > 0) { u_gethostinfo(&senderinfo, hostinfo, BUFSIZE); if ((r_write(STDOUT_FILENO, hostinfo, strlen(hostinfo)) == -1) || (r_write(STDOUT_FILENO, buf, bytesread) == -1)) { perror("Failed to echo message received to standard output"); break; } } return 0; }
int main(int argc, char *argv[]) { char buf[BUFSIZE]; char host_info[BUFSIZE]; int mcastfd; u_buf_t mcast_info; u_port_t mcastport; ssize_t nbytes_read; u_buf_t sender_info; int count; if (argc != 3) { fprintf(stderr, "Usage: %s Multicast-Address Multicast-Port\n", argv[0]); return 1; } /* join the multicast group */ mcastport = (u_port_t)atoi(argv[2]); if ((mcastfd = u_join(argv[1], mcastport, &mcast_info)) == -1) { perror("Receiver failed to join multicast group"); return 1; } u_gethostinfo(&mcast_info,buf,BUFSIZE); fprintf(stderr,"Info: %s\n",buf); fprintf(stderr,"mcastfd is %d\n",mcastfd); /* multicast information read from standard input */ count = 0; while ((nbytes_read = u_recvfrom(mcastfd, buf, BUFSIZE, &sender_info)) > 0) { u_gethostinfo(&sender_info, host_info, BUFSIZE); if ((r_write(STDOUT_FILENO, host_info, strlen(host_info)) == -1) || (r_write(STDOUT_FILENO, buf, nbytes_read) == -1)) { perror("Error echoing sender or message to standard output"); break; } count++; if (count == 5) { fprintf(stderr,"Leaving group\n"); if (u_leave(mcastfd,&mcast_info) == -1) perror("Error leaving group"); } } return 0; }