main.c 780 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include "unp.h"
  2. void recv_all(int, socklen_t);
  3. void send_all(int, SA *, socklen_t);
  4. int
  5. main(int argc, char **argv)
  6. {
  7. int sendfd, recvfd;
  8. const int on = 1;
  9. socklen_t salen;
  10. struct sockaddr *sasend, *sarecv;
  11. if (argc != 3)
  12. err_quit("usage: sendrecv <IP-multicast-address> <port#>");
  13. sendfd = Udp_client(argv[1], argv[2], (void **) &sasend, &salen);
  14. recvfd = Socket(sasend->sa_family, SOCK_DGRAM, 0);
  15. Setsockopt(recvfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  16. sarecv = Malloc(salen);
  17. memcpy(sarecv, sasend, salen);
  18. Bind(recvfd, sarecv, salen);
  19. Mcast_join(recvfd, sasend, salen, NULL, 0);
  20. Mcast_set_loop(sendfd, 0);
  21. if (Fork() == 0)
  22. recv_all(recvfd, salen); /* child -> receives */
  23. send_all(sendfd, sasend, salen); /* parent -> sends */
  24. }