Guest User

Untitled

a guest
Sep 7th, 2022
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. $ diff -u test.c.original test.c
  2. --- test.c.original 2022-09-07 11:10:57.427143497 -0700
  3. +++ test.c 2022-09-07 11:18:48.435146489 -0700
  4. @@ -5,6 +5,7 @@
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <pthread.h>
  8. +#include <fcntl.h>
  9.  
  10. void *recv_thread(void *arg)
  11. {
  12. @@ -14,8 +15,9 @@
  13. char recvbuf[1024] = {0};
  14. while(1)
  15. {
  16. - recvfrom(socket_fd, recvbuf, 1024, 0,(struct sockaddr*)&recv_addr,&addrlen);
  17. - printf("[recv from %s:%d]%s \n",inet_ntoa(*(struct in_addr*)&recv_addr.sin_addr.s_addr),ntohs(recv_addr.sin_port),recvbuf);
  18. + int rv = recvfrom(socket_fd, recvbuf, 1024, 0,(struct sockaddr*)&recv_addr,&addrlen);
  19. + if (rv > 0)
  20. + printf("[recv from %s:%d]%s \n",inet_ntoa(*(struct in_addr*)&recv_addr.sin_addr.s_addr),ntohs(recv_addr.sin_port),recvbuf);
  21. }
  22. }
  23.  
  24. @@ -32,9 +34,10 @@
  25. }
  26. struct sockaddr_in local_addr = {0};
  27. local_addr.sin_family = AF_INET;
  28. - local_addr.sin_addr.s_addr = inet_addr("192.168.0.107");
  29. + local_addr.sin_addr.s_addr = inet_addr("7.0.0.2");
  30. local_addr.sin_port = htons(12341);
  31. bind(sockfd, (struct sockaddr*)&local_addr, sizeof(local_addr));
  32. + fcntl(sockfd, F_SETFL, O_NONBLOCK);
  33. ret = pthread_create(&th, NULL, recv_thread, &sockfd);
  34.  
  35.  
  36. @@ -48,9 +51,10 @@
  37. }
  38. struct sockaddr_in local_addr2 = {0};
  39. local_addr2.sin_family = AF_INET;
  40. - local_addr2.sin_addr.s_addr = inet_addr("192.168.0.107");
  41. + local_addr2.sin_addr.s_addr = inet_addr("7.0.0.2");
  42. local_addr2.sin_port = htons(12342);
  43. bind(sockfd2, (struct sockaddr*)&local_addr2, sizeof(local_addr2));
  44. + fcntl(sockfd2, F_SETFL, O_NONBLOCK);
  45. ret2 = pthread_create(&th2, NULL, recv_thread, &sockfd2);
  46.  
  47. void *status;
Advertisement
Add Comment
Please, Sign In to add comment