Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <netdb.h>
  3. #include <netinet/in.h>
  4. #include <stdlib.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <string.h>
  8.  
  9. #include <unistd.h>
  10.  
  11. #define bufferSize 1024
  12. int main(int argc, char *argv[])
  13. {
  14. if(argc<2)
  15. {
  16. printf("Error. No port was entered.\n");
  17. fflush(stdout);
  18. exit(1);
  19. }
  20. int port = atoi(argv[1]);
  21. char buffer[bufferSize];
  22.  
  23. /*int server_fd = socket(AF_INET,SOCK_DGRAM,0);
  24. struct sockaddr_in server,client;
  25. if(server_fd<0)
  26. {
  27. if(server_fd)
  28. {
  29. close(server_fd);
  30. }
  31. printf("Error. Couldn't create socket on server.\n");
  32. exit(1);
  33. }
  34.  
  35. server.sin_family = AF_INET;
  36. server.sin_port = htons(port);
  37. server.sin_addr.s_addr=htonl(INADDRY_ANY);
  38.  
  39. if(bind(server_fd,(struct sockaddr *) &server, sizeof(server))<0)
  40. {
  41. printf("Error. Binding datagram socket server_fd to port %d failed.\n",port);
  42. if(server_fd)
  43. {
  44. close(server_fd);
  45. }
  46. exit(1);
  47. }
  48. while(1)
  49. {
  50. socklen_t client_len = sizeof(client);
  51.  
  52. while(1)
  53. {
  54. int readBytes = recvfrom(server_fd,buffer,bufferSize,0,(struct sockaddr *)&client,&client_len);
  55. if(readBytes<=0)
  56. {
  57. printf("Error. Could not read data from client.\n");
  58.  
  59. break;
  60. }
  61. buffer[readBytes]='\0';
  62. int sentBytes = sendto(server_fd,buffer,strlen(buffer)+1,0,(struct sockaddr *)&client, &client_len);
  63. if(sentBytes<=0)
  64. {
  65. printf("Error. Could not send data to client.\n");
  66. break;
  67. }
  68. }
  69. }*/
  70.  
  71.  
  72. int server_fd, client_fd, checker;
  73. struct sockaddr_in server, client;
  74.  
  75. server_fd = socket(AF_INET,SOCK_STREAM,0);
  76. if(server_fd < 0)
  77. {
  78. printf("Couldn't create socket.\n");
  79. fflush(stdout);
  80. exit(EXIT_FAILURE);
  81. }
  82. server.sin_family=AF_INET;
  83. server.sin_port=htons(port);
  84. server.sin_addr.s_addr = htonl(INADDR_ANY);
  85.  
  86. int reuseOn=1;
  87. if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEADDR,&reuseOn,sizeof(reuseOn)) < 0)
  88. {
  89. printf("Error. setsockopt(SO_REUSEADDR) failed.\n");
  90. fflush(stdout);
  91. exit(EXIT_FAILURE);
  92. }
  93.  
  94. if(bind(server_fd,(struct sockaddr *) &server, sizeof(server))<0)
  95. {
  96. printf("Error. Binding server_fd to port %d failed.\n",port);
  97. fflush(stdout);
  98. exit(EXIT_FAILURE);
  99. }
  100.  
  101. checker = listen(server_fd,128);
  102. if(checker<0)
  103. {
  104. printf("Error. Listening on socket failed.\n");
  105. fflush(stdout);
  106. }
  107.  
  108. printf("Echo server is listening on port %d.\n",port);
  109. fflush(stdout);
  110.  
  111.  
  112. int dataSendCounter=0;
  113. int dataReceivedCounter=0;
  114. while(1)
  115. {
  116. socklen_t client_len = sizeof(client);
  117. client_fd = accept(server_fd,(struct sockaddr*) &client,&client_len);
  118. if(client_fd<0)
  119. {
  120. printf("Error. Failed to establish new connection.");
  121. fflush(stdout);
  122. continue;
  123. }
  124. while(1)
  125. {
  126. printf("\nReceiving new data.\n");
  127. int readBytes = recv(client_fd,buffer,bufferSize,0);
  128. dataReceivedCounter++;
  129. printf("\n Times received data = %d \n",dataReceivedCounter);
  130. if(readBytes<=0)
  131. {
  132. printf("Error. Connection with client lost. Could not read message. readBytes = %d \n",readBytes);
  133. fflush(stdout);
  134. break;
  135. }
  136. printf("\nREADBYTES = %d\n",readBytes);
  137. buffer[readBytes]='\0';
  138. printf("From client: %s ",buffer);
  139. fflush(stdout);
  140. int timesSent=0;
  141. int bytesSent=0;
  142. int totalBytesSent=0;
  143. while(1)
  144. {
  145. bytesSent=totalBytesSent;
  146. if(totalBytesSent<readBytes)
  147. {
  148. timesSent++;
  149. printf("\nSending data.\n");
  150. totalBytesSent+=send(client_fd,buffer+bytesSent,readBytes-bytesSent,0);
  151. dataSendCounter++;
  152. printf("\nTimes sent data = %d\n",dataSendCounter);
  153. if(totalBytesSent<=bytesSent)
  154. {
  155. printf("Error. Could not send message.\n");
  156. fflush(stdout);
  157. close(client_fd);
  158. break;
  159. }
  160. printf("bytesSent = %d\n",bytesSent);
  161. printf("debug %d\n",timesSent);
  162. //close(client_fd);
  163. }
  164. else
  165. {
  166. break;
  167. }
  168. }
  169.  
  170. printf("To client: %s \n", buffer);
  171. fflush(stdout);
  172. if(strncmp(buffer,"over",4)==0)
  173. {
  174. printf("Exchange with client ended successfully.\n");
  175. fflush(stdout);
  176. break;
  177. }
  178. }
  179. close(client_fd);
  180.  
  181. }
  182.  
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement