Advertisement
vvccs

UDP_iterative

May 31st, 2023 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.35 KB | None | 0 0
  1. //Server:-
  2. #include <sys/socket.h>
  3. #include <sys/types.h>
  4. #include <netinet/in.h>
  5. #include <arpa/inet.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <netdb.h>
  11. #define MAX 100
  12. int main()
  13. {
  14. int sockfd,stat;
  15. struct sockaddr_in serveraddr;
  16. struct sockaddr_in clientaddr;
  17. char buf[MAX];
  18. socklen_t clientlen;
  19. sockfd = socket(AF_INET,SOCK_DGRAM,0);
  20. if(sockfd==-1)
  21. {
  22. printf("Error Connecting socket\n");
  23. exit(0);
  24. }
  25. else
  26. {
  27. printf("Socket is created\n");
  28. serveraddr.sin_family=AF_INET;
  29. serveraddr.sin_port=htons(7000);
  30. serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  31. stat=bind(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
  32. }
  33. if(stat==-1)
  34. {
  35. printf("Error assigning socket\n");
  36. exit(0);
  37. }
  38. else
  39. {
  40. printf("Socket assigned\n");
  41. }
  42. memset(buf,0,MAX);
  43. clientlen=sizeof(serveraddr);
  44. while(1)
  45. {
  46. memset(buf,0,MAX);
  47. recvfrom(sockfd,buf,MAX,0,(struct sockaddr *)&clientaddr,&clientlen);
  48. printf("Data from port number:\t");
  49. printf("%d\n",htons(clientaddr.sin_port));
  50. printf("Message from client:\t");
  51. printf("%s\n",buf);
  52. memset(buf,0,MAX);
  53. }
  54. close(sockfd);
  55. return 0;
  56. }
  57.  
  58.  
  59. //Client:-
  60. #include <sys/socket.h>
  61. #include <sys/types.h>
  62. #include <netinet/in.h>
  63. #include <arpa/inet.h>
  64. #include <string.h>
  65. #include <stdio.h>
  66. #include <stdlib.h>
  67. #include <unistd.h>
  68. #include <netdb.h>
  69. #define MAX 100
  70. int main()
  71. {
  72. int sockfd,stat;
  73. struct sockaddr_in serveraddr;
  74. struct sockaddr_in clientaddr;
  75. char buf[MAX];
  76. socklen_t serverlen;
  77. sockfd = socket(AF_INET,SOCK_DGRAM,0);
  78. if(sockfd==-1)
  79. {
  80. printf("Error Connecting socket\n");
  81. exit(0);
  82. }
  83. else
  84. {
  85. printf("Socket is created\n");
  86. clientaddr.sin_family=AF_INET;
  87. clientaddr.sin_port=htons(0);
  88. clientaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  89. stat=bind(sockfd,(struct sockaddr *)&clientaddr,sizeof(clientaddr));
  90. }
  91. if(stat==-1)
  92. {
  93. printf("Error assigning client socket\n");
  94. exit(0);
  95. }
  96. else
  97. {
  98. printf("Socket assigned");
  99. serveraddr.sin_family=AF_INET;
  100. serveraddr.sin_port=htons(7000);
  101. serveraddr.sin_addr.s_addr=inet_addr("127.0.0.1");
  102. memset(buf,0,MAX);
  103. printf("Enter the data\n");
  104. printf("'.' on nextline ends the socket\n");
  105. while(1)
  106. {
  107. memset(buf,0,MAX);
  108. scanf("\t%s",buf);
  109. if(strcmp(buf,".")==0)
  110. {
  111. break;
  112. }
  113. sendto(sockfd,buf,MAX,0,(struct sockaddr *)&serveraddr,sizeof(serveraddr));
  114. memset(buf,0,MAX);
  115. }
  116. close(sockfd);
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement