Advertisement
vvccs

UDP

May 31st, 2023 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. //Server
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <arpa/inet.h>
  6.  
  7. int main(void){
  8. int sockfd;
  9. struct sockaddr_in server_addr, client_addr;
  10. char server_message[2000], client_message[2000];
  11. int clientlen = sizeof(client_addr);
  12.  
  13. memset(server_message, '\0', sizeof(server_message));
  14. memset(client_message, '\0', sizeof(client_message));
  15. sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  16.  
  17. if(sockfd < 0)
  18. {
  19. printf("Error while creating socket\n");
  20. return -1;
  21. }
  22. else
  23. {
  24. printf("Socket created successfully\n");
  25. }
  26. server_addr.sin_family = AF_INET;
  27. server_addr.sin_port = htons(7000);
  28. server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  29. if(bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
  30. {
  31. printf("Couldn't bind to the port\n");
  32. return -1;
  33. }
  34. else
  35. {
  36. printf("Done with binding\n");
  37. printf("Listening for incoming messages...\n\n");
  38. }
  39. if (recvfrom(sockfd, client_message, sizeof(client_message), 0,
  40. (struct sockaddr*)&client_addr, &clientlen) < 0)
  41. {
  42. printf("Couldn't receive\n");
  43. return -1;
  44. }
  45. else
  46. {
  47. printf("Received message from IP: %s and port: %i\n",
  48. inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
  49. printf("Msg from client: %s\n", client_message);
  50. printf("Enter Response\n");
  51. gets(server_message);
  52. }
  53. if (sendto(sockfd, server_message, strlen(server_message), 0,
  54. (struct sockaddr*)&client_addr, clientlen) < 0)
  55. {
  56. printf("Can't send\n");
  57. return -1;
  58. }
  59. close(sockfd);
  60.  
  61. return 0;
  62. }
  63.  
  64. //Client
  65. #include <stdio.h>
  66. #include <string.h>
  67. #include <sys/socket.h>
  68. #include <arpa/inet.h>
  69.  
  70. int main(void){
  71. int sockfd;
  72. struct sockaddr_in server_addr;
  73. char server_message[2000], client_message[2000];
  74. int serverlen = sizeof(server_addr);
  75.  
  76. memset(server_message, '\0', sizeof(server_message));
  77. memset(client_message, '\0', sizeof(client_message));
  78. sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  79.  
  80. if(sockfd < 0)
  81. {
  82. printf("Error while creating socket\n");
  83. return -1;
  84. }
  85. else
  86. {
  87. printf("Socket created successfully\n");
  88. server_addr.sin_family = AF_INET;
  89. server_addr.sin_port = htons(7000);
  90. server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  91. printf("Enter message: ");
  92. gets(client_message);
  93. }
  94.  
  95. if(sendto(sockfd, client_message, strlen(client_message), 0,
  96. (struct sockaddr*)&server_addr, serverlen) < 0){
  97. printf("Unable to send message\n");
  98. return -1;
  99. }
  100.  
  101. if(recvfrom(sockfd, server_message, sizeof(server_message), 0,
  102. (struct sockaddr*)&server_addr, &serverlen) < 0)
  103. {
  104. printf("Error while receiving server's msg\n");
  105. return -1;
  106. }
  107. else
  108. {
  109. printf("Server's response: %s\n", server_message);
  110. close(sockfd);
  111. }
  112. return 0;
  113. }
Tags: #UDP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement