Advertisement
Guest User

Untitled

a guest
May 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #include <arpa/inet.h>
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9.  
  10. int main(int argc, char* argv[]) {
  11. short port_num = strtol(argv[1], NULL, 10);
  12. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  13. if (sock == -1) {
  14. perror("socket");
  15. abort();
  16. }
  17.  
  18. struct sockaddr_in addr = {
  19. .sin_family = AF_INET,
  20. .sin_addr = inet_addr("127.0.0.1"),
  21. .sin_port = htons(port_num)
  22. };
  23.  
  24. int send_num, recv_num;
  25. while (scanf("%d", &send_num) > 0) {
  26. sendto(sock,
  27. &send_num, sizeof(send_num),
  28. 0,
  29. (const struct sockaddr*) &addr,
  30. sizeof(addr));
  31. recvfrom(sock,
  32. &recv_num, sizeof(recv_num),
  33. 0, NULL, NULL);
  34. printf("%d\n", recv_num);
  35. }
  36.  
  37. close(sock);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement