Guest User

Untitled

a guest
Jun 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <sys/select.h>
  7. #include <errno.h>
  8. #include <sys/socket.h>
  9. #include <arpa/inet.h>
  10. #include <netinet/tcp.h>
  11. #include <stdbool.h>
  12. #include "init.h"
  13.  
  14. #define MAX_CLIENT 20000
  15. #define SIZE 220000
  16.  
  17. int server_listen(int argc, char **argv);
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21. int ret, accept_desc, n, ssd;
  22. unsigned int bytes = 1;
  23. unsigned char *buf;
  24. fd_set rfd;
  25.  
  26. ssd = server_listen(argc, argv); // --- return fd from listen()
  27. n = ssd;
  28.  
  29. FD_ZERO(&rfd); // --- Clears a set
  30. FD_SET(ssd, &rfd); // --- Add a given file descriptor from a set
  31.  
  32. while (true) {
  33. FD_ZERO(&rfd);
  34. FD_SET(n, &rfd);
  35. ret = select(n + 1, &rfd, NULL, NULL, NULL); // --- Waiting until one or more of the file descriptors become "ready"
  36.  
  37. if (ret < 0) {
  38. perror("Error in selectn");
  39. close(n);
  40. exit(EXIT_FAILURE);
  41. }
  42.  
  43. if (FD_ISSET(ssd, &rfd)) { // --- Tests to see if a file descriptor is part of the set
  44. accept_desc = server_accept(ssd); // ---Accept connection from client
  45. fprintf(stderr, "client %d is connectedn", accept_desc);
  46. if (n < accept_desc)
  47. n = accept_desc;
  48. }
  49.  
  50. if (FD_ISSET(accept_desc, &rfd)) {
  51. bytes++;
  52. buf = receive(accept_desc, bytes); // --- Receive buffer from client
  53. if(buf == NULL) {
  54. fprintf(stderr, "Memory not allocatedn");
  55. close(accept_desc);
  56. exit(EXIT_FAILURE);
  57. }
  58. send_str(accept_desc, buf, bytes); // --- Send received buffer to client
  59. fprintf(stderr, "sent %dn", bytes);
  60. free(buf); // --- Free the memory
  61. }
  62. }
  63. }
  64.  
  65.  
  66. int server_listen(int argc, char **argv)
  67. {
  68. int ssd, flag = 1, ret, portno;
  69. unsigned int addrlen;
  70. struct sockaddr_in serv_addr;
  71.  
  72. portno = get_option(argc, argv); // --- Get portno from user using getopt
  73.  
  74. ssd = socket(AF_INET, SOCK_STREAM, 0); // --- socket creates an endpoint for communication
  75.  
  76. if (ssd < 0) {
  77. perror("Error in socket");
  78. exit(EXIT_FAILURE);
  79. }
  80.  
  81. ret = setsockopt(ssd, IPPROTO_TCP, TCP_NODELAY,
  82. (char *) &flag, sizeof(int)); // --- TCP_NODELAY used to disable nagle's algorithm
  83. if (ret != 0) {
  84. perror("Erro in tcp_nodelay");
  85. close(ssd);
  86. exit(EXIT_FAILURE);
  87. }
  88.  
  89. ret = setsockopt(ssd, SOL_SOCKET, SO_REUSEADDR,
  90. (char *) &flag, sizeof(int)); // --- SO_REUSEADDR to reuse same port
  91.  
  92. if (ret != 0) {
  93. perror("Error in reuseAddr");
  94. close(ssd);
  95. exit(EXIT_FAILURE);
  96. }
  97.  
  98. serv_addr.sin_family = AF_INET;
  99. serv_addr.sin_addr.s_addr = INADDR_ANY;
  100. serv_addr.sin_port = htons((uint16_t)portno);
  101. bzero(&serv_addr.sin_zero, sizeof(serv_addr.sin_zero));
  102. addrlen = sizeof(serv_addr);
  103.  
  104. ret = bind(ssd, (struct sockaddr *)&serv_addr, addrlen); // --- Bind assigns the address specified by addr to socket
  105.  
  106. if (ret < 0) {
  107. perror("Error in binding");
  108. close(ssd);
  109. exit(EXIT_FAILURE);
  110. }
  111. fprintf(stderr, "Bind completedn");
  112. //Listen
  113. ret = listen(ssd, MAX_CLIENT); // --- ready to accept incoming connection requests using accept()
  114.  
  115. if (ret < 0) {
  116. perror("Error in listen");
  117. close(ssd);
  118. exit(EXIT_FAILURE);
  119. }
  120. fprintf(stderr, "listenn");
  121.  
  122. return ssd;
  123. }
  124.  
  125. #include <stdio.h>
  126. #include <stdlib.h>
  127. #include <string.h>
  128. #include <sys/socket.h>
  129. #include <netdb.h>
  130. #include <errno.h>
  131. #include <sys/select.h>
  132. #include <unistd.h>
  133. #include <time.h>
  134. #include <sys/types.h>
  135. #include <netinet/tcp.h>
  136. #include "init.h"
  137.  
  138. #define KB 1024
  139. #define NO_EXIT_OR_ERROR 1
  140. #define STATE_SEND 0
  141. #define STATE_RECEIVE 1
  142.  
  143. unsigned int bytes;
  144.  
  145. struct hostent *server;
  146.  
  147. int client_connect(int argc, char **argv);
  148.  
  149. unsigned char *fill_n_send(int fd);
  150.  
  151. void receive_n_compare(int fd, unsigned char *buf_send);
  152.  
  153. int main(int argc, char *argv[])
  154. {
  155. int fd, state = STATE_SEND, ret;
  156. struct timeval tv;
  157. unsigned char *buf_send;
  158. fd_set rfd;
  159.  
  160. fd = client_connect(argc, argv); // --- Return fd from connect()
  161. tv.tv_sec = 0;
  162. tv.tv_usec = 100;
  163.  
  164. FD_ZERO(&rfd); // --- Clears a set
  165. FD_SET(fd, &rfd);
  166.  
  167. while(NO_EXIT_OR_ERROR) {
  168. FD_ZERO(&rfd);
  169. FD_SET(fd, &rfd); // --- Add a given file descriptor from a set
  170. ret = select(fd + 1, &rfd, NULL, NULL, &tv);
  171.  
  172. if(ret < 0) { // --- Waiting until one or more of the file descriptors become "ready"
  173. perror("Select error");
  174. close(fd);
  175. exit(EXIT_FAILURE);
  176. }
  177.  
  178. if(state == STATE_SEND) {
  179. buf_send = fill_n_send(fd);
  180. state = STATE_RECEIVE;
  181.  
  182. }
  183.  
  184. if (FD_ISSET(fd, &rfd)) { // --- Tests to see if a file descriptor is part of the set
  185. receive_n_compare(fd, buf_send);
  186. state = STATE_SEND;
  187. }
  188. }
  189. }
  190.  
  191.  
  192. unsigned char *fill_n_send(int fd)
  193. {
  194. unsigned int start = 0;
  195. unsigned char *buf_send;
  196.  
  197. buf_send = malloc(bytes + 2);
  198. if(buf_send == NULL) {
  199. fprintf(stderr, "Memory not allocatedn");
  200. close(fd);
  201. exit(EXIT_FAILURE);
  202. }
  203.  
  204. while(start <= bytes){ // --- fill send buf like A, AA, AAA, AAAA ..... upto 200KB size and send one by one like first A then AA then AAA ...
  205. buf_send[start] = 'A';
  206. start++;
  207. }
  208. buf_send[++bytes] = 0;
  209.  
  210. if(bytes >= 200*KB) {
  211. free(buf_send);
  212. close(fd);
  213. exit(EXIT_SUCCESS);
  214. }
  215.  
  216. send_str(fd, buf_send, bytes + 1);
  217.  
  218. return buf_send;
  219. }
  220.  
  221.  
  222. void receive_n_compare(int fd, unsigned char *buf_send)
  223. {
  224. unsigned char *buf_recv;
  225.  
  226. buf_recv = receive(fd, bytes + 1);
  227.  
  228. if(buf_recv == NULL) {
  229. fprintf(stderr, "Memory not allocatedn");
  230. free(buf_send);
  231. close(fd);
  232. exit(EXIT_FAILURE);
  233. }
  234.  
  235. if(strcmp((const char*)buf_send, (const char*)buf_recv) == 0){
  236. fprintf(stderr, "Matchn");
  237. } else {
  238. fprintf(stderr, "Unmatch %ld %ldn", strlen((const char*)buf_send), strlen((const char*)buf_recv));
  239. close(fd);
  240. exit(EXIT_FAILURE);
  241. }
  242.  
  243. free(buf_send);
  244. free(buf_recv);
  245. }
  246.  
  247.  
  248. int client_connect(int argc, char **argv)
  249. {
  250. int fd1, ret, flag = 1, portno;
  251. struct sockaddr_in serv_addr;
  252.  
  253. portno = get_option(argc, argv); // --- Get portno from user using getopt
  254.  
  255. if(server == NULL) { // --- Check whether receive ip address of server
  256. fprintf(stderr, "Assign IPn");
  257. exit(EXIT_FAILURE);
  258. }
  259.  
  260. fd1 = socket(AF_INET, SOCK_STREAM, 0); // --- socket creates an endpoint for communication
  261. if (fd1 == -1) {
  262. perror("ERROR on creating Socketn");
  263. exit(EXIT_FAILURE);
  264. }
  265.  
  266. fprintf(stderr, "%dn", fd1);
  267. fprintf(stderr, "socket created successfully.n");
  268. bzero((char *) &serv_addr, sizeof(serv_addr));
  269. serv_addr.sin_family = AF_INET;
  270. bcopy((char *) server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
  271. (long unsigned int)server->h_length);
  272. serv_addr.sin_port = htons((uint16_t)portno);
  273.  
  274. ret = setsockopt(fd1, IPPROTO_TCP, TCP_NODELAY,
  275. (char *) &flag, sizeof(int)); // --- TCP_NODELAY used to disable nagle's algorithm
  276.  
  277. if (ret != 0) {
  278. perror("Error in setsockopt");
  279. close(fd1);
  280. exit(EXIT_FAILURE);
  281. }
  282.  
  283. if (connect(fd1, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == -1) { // --- Connect the socket to the server
  284. perror("ERROR in Connecting");
  285. close(fd1);
  286. exit(EXIT_FAILURE);
  287. }
  288.  
  289. printf("Client connected Successfullyn");
  290.  
  291. return fd1;
  292. }
  293.  
  294. #include "init.h"
  295. #include <stdio.h>
  296. #include <sys/socket.h>
  297. #include <sys/types.h>
  298. #include <stdlib.h>
  299. #include <string.h>
  300. #include <unistd.h>
  301. #include <errno.h>
  302. #include <netdb.h>
  303. #include <stdbool.h>
  304.  
  305. #define SIZE 220000
  306.  
  307. struct hostent *server;
  308.  
  309. unsigned char *receive(int csd, unsigned int size)
  310. {
  311. long int ret;
  312. unsigned char *buf = malloc(size), *buf1 = NULL;
  313.  
  314. if(buf != NULL) {
  315. memset(buf, 0,size);
  316. } else {
  317. fprintf(stderr, "Memory not allocatedn");
  318. close(csd);
  319. exit(EXIT_FAILURE);
  320. }
  321.  
  322. while(true) {
  323. ret = recv(csd, buf, size, 0);
  324.  
  325. if(buf[ret-1] > 0) {
  326. buf1 = malloc(size);
  327.  
  328. if(buf1 != NULL) {
  329. memset(buf1, 0,size);
  330. } else {
  331. fprintf(stderr, "Memory not allocatedn");
  332. free(buf);
  333. close(csd);
  334. exit(EXIT_FAILURE);
  335. }
  336.  
  337. strcpy((char*)buf1, (const char*)buf);
  338. memset(buf, 0, size);
  339. continue;
  340. }
  341.  
  342. if (ret < 0) {
  343. perror("Error in receiven");
  344. close(csd);
  345. exit(EXIT_FAILURE);
  346. } else if (ret == 0) {
  347. fprintf(stderr, "client disconnectedn");
  348. close(csd);
  349. } else {
  350.  
  351. if(buf1 != NULL) {
  352. strcat((char*)buf1, (const char*)buf);
  353. free(buf);
  354. return buf1;
  355. }
  356. return buf;
  357. }
  358. }
  359.  
  360. }
  361.  
  362.  
  363. void send_str(int csd, unsigned char *buf, unsigned int size)
  364. {
  365. long int ret;
  366.  
  367. ret = send(csd, buf, size, 0);
  368.  
  369. if (ret < 0) {
  370. perror("Error in sendn");
  371. close(csd);
  372. exit(EXIT_FAILURE);
  373. } else if(ret == 0) {
  374. fprintf(stderr, "Nothing to sendn");
  375. close(csd);
  376. }
  377. }
  378.  
  379.  
  380. int server_accept(int ssd)
  381. {
  382. int accept_desc;
  383. struct sockaddr_in cli_addr;
  384. socklen_t cli_addr_len;
  385.  
  386. accept_desc = accept(ssd, (struct sockaddr *)&cli_addr,
  387. (socklen_t *)&cli_addr_len);
  388.  
  389. if (accept_desc < 0) {
  390. perror("Error in acceptn");
  391. close(ssd);
  392. exit(EXIT_FAILURE);
  393. }
  394.  
  395. return accept_desc;
  396. }
  397.  
  398.  
  399. int get_option(int argc, char **argv)
  400. {
  401. int opt, portno = 0;
  402.  
  403. while ((opt = getopt(argc, argv, "p:i:")) != -1) {
  404.  
  405. switch (opt) {
  406.  
  407. case 'p':
  408. portno = atoi(optarg);
  409. memset(&optarg, 0, strlen(optarg));
  410. break;
  411.  
  412. case 'i':
  413. server = gethostbyname(optarg);
  414. memset(&optarg, 0, strlen(optarg));
  415. break;
  416.  
  417. default: /* '?' */
  418. fprintf(stderr, "Usage: %sn", argv[0]);
  419. exit(EXIT_FAILURE);;
  420. }
  421. }
  422.  
  423. if (portno == 0) {
  424. fprintf(stderr, "Assign port numbern");
  425. exit(EXIT_FAILURE);
  426. }
  427.  
  428. printf("portno = %d optind = %dn", portno, optind);
  429.  
  430. return portno;
  431. }
  432.  
  433. #ifndef INIT_H
  434. #define INIT_H
  435.  
  436. int get_option(int argc, char *argv[]);
  437.  
  438. unsigned char *receive(int csd, unsigned int size);
  439.  
  440. void send_str(int csd, unsigned char *buf, unsigned int size);
  441.  
  442. int server_accept(int ssd);
  443.  
  444. #endif // INIT_H
  445.  
  446. .PHONY: all server1 client1
  447.  
  448. all: server1 client1
  449.  
  450. server1: server.c init.c
  451. gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 server.c init.c -o s
  452.  
  453. client1: client.c init.c
  454. gcc -Wall -Wextra -Wconversion -pedantic -std=gnu11 client.c init.c -o c
  455.  
  456. server:
  457. ./s -p 13000
  458.  
  459. client:
  460. ./c -p 13000 -i 127.0.0.1
Add Comment
Please, Sign In to add comment