Advertisement
Guest User

Untitled

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