Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <unistd.h>
  4. #include <netdb.h>
  5. #include <stdio.h>
  6. #include <string.h> /* strlen, memset */
  7. #include "list.h"
  8. #include "ports.h"
  9.  
  10. /* Connect to this server using: telnet localhost 12345
  11. It will respond to one request and close connections */
  12.  
  13. pthread_mutex_t list_mutex;
  14.  
  15. void* echo_thread(void* socket) {
  16. char str[BUFFER_SIZE];
  17.  
  18. int* comm_fd = (int*)socket;
  19. while(1){
  20.  
  21. memset(str, 0, BUFFER_SIZE);//initializing of str. (Where the request will be)
  22. int num_bytes = read(*comm_fd, str, BUFFER_SIZE-1);//reading client request
  23. if (num_bytes == -1) {
  24. fprintf(stderr, "Error: Reading from client failed, killing thread...\n");
  25. free(socket);
  26. return NULL;
  27. }
  28. else if (num_bytes == 0) {
  29. printf("Client disconnected normally, killing thread...\n");
  30. free(socket);
  31. return NULL;
  32. }
  33. else {
  34. if(strncmp(str, "ADD:", 4) == 0){
  35. char *toAdd = str+4;
  36.  
  37. /* Check for white spaces */
  38. char word[MAX_STR];
  39. int c = 0, d = 0;
  40. while (toAdd[c] != '\0') {
  41. if (toAdd[c] == ' ') {
  42. int temp = c;
  43. if (toAdd[temp] != '\0') {
  44. while (toAdd[temp] == ' ' && toAdd[temp] != '\0') {
  45. if (toAdd[temp] == ' ') {
  46. c++;
  47. }
  48. temp++;
  49. }
  50. }
  51. }
  52. word[d] = toAdd[c];
  53. c++;
  54. d++;
  55. }
  56.  
  57.  
  58.  
  59. word[d] = '\0';
  60.  
  61. if(search_by_name(word) == NULL){
  62. pthread_mutex_lock(&list_mutex);
  63. writeFile(word);
  64. pthread_mutex_unlock(&list_mutex);
  65. generate_item(word);
  66. write(*comm_fd, "OK\n", strlen("OK\n"));
  67.  
  68.  
  69. }
  70. else{
  71. write(*comm_fd, "ERROR\n", strlen("ERROR\n"));
  72. }
  73. } else if(strncmp(str, "COUNT", 5) == 0){
  74. int count = count_list_size();
  75. sprintf(str, "%d\n", count); //int value of count read and saved in buffer str
  76. if(strncmp(str, "0", 1) == 0){
  77. write(*comm_fd, str, strlen("0"));
  78. }
  79. else {
  80. write(*comm_fd, str, strlen(str));
  81. }
  82.  
  83. } else if(strncmp(str, "RANDOM", 6) == 0){
  84. if(count_list_size() == 0){
  85. write(*comm_fd, "ERROR\n", strlen("ERROR\n"));
  86. }
  87. else {
  88. item *randItem = get_item_at_index(rand() % count_list_size()); //pointer
  89. strcpy(str, randItem->name); //point -> notation?
  90. write(*comm_fd, str, strlen(str)); //+1 or -1?
  91. }
  92. } else if(strncmp(str, "CLEAR", 5) == 0){ //use toupper to compare
  93. free_list();
  94. clearFile();
  95. write(*comm_fd, "OK\n", strlen("OK\n")+1); //why strlen\n ?
  96. } else {
  97. write(*comm_fd, "UNKNOWN\n", strlen("UNKNOWN\n"));
  98. }
  99.  
  100. }
  101. if (write(*comm_fd, str, strlen(str)+1) < 0) {
  102. fprintf(stderr, "Error: failed to write to client, killing thread...\n");
  103. free(socket);
  104. return NULL;
  105. }
  106. printf("Echoed: %s", str);
  107. }
  108. }
  109.  
  110. int main(int argc, char *argv[]) { //Accepts parameters
  111.  
  112. openFile();
  113. pthread_mutex_init(&list_mutex, NULL);
  114.  
  115. int listen_fd;
  116. struct sockaddr_in cliaddr;
  117. struct sockaddr_in servaddr;
  118. socklen_t clilen = sizeof(cliaddr);
  119. pthread_t mythread;
  120.  
  121. listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //socket
  122. if (listen_fd == -1) {
  123. fprintf(stderr, "ERROR: socket()\n");
  124. goto cleanup;
  125. }
  126.  
  127.  
  128.  
  129. if(listen_fd < 0){
  130. printf("Failed to open socket");
  131. }
  132.  
  133. //initialize socket
  134. memset(&servaddr, 0, sizeof(servaddr));
  135. servaddr.sin_family = AF_INET;
  136. servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  137. servaddr.sin_port = htons(LISTEN_PORT);
  138.  
  139. //Allows re-use of the port as soon as the server terminates
  140. /* int enable = 1;
  141. setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
  142. */
  143. if(bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < -1){
  144. printf("bind failed");
  145. goto cleanup;
  146. }
  147.  
  148. if(listen(listen_fd, 10)){
  149. printf("listen failed");
  150. goto cleanup;
  151. }
  152.  
  153. while(1) {
  154. printf("Accepting new connections...\n");
  155. int* comm_fd = malloc(sizeof(int));
  156. if (comm_fd == NULL) {
  157. fprintf(stderr, "ERROR: malloc() failed\n");
  158. goto cleanup;
  159. }
  160. *comm_fd = accept(listen_fd, (struct sockaddr *)&cliaddr, &clilen);
  161. if (*comm_fd == -1) {
  162. fprintf(stderr, "ERROR: accept()\n");
  163. free(comm_fd);
  164. goto cleanup;
  165. }
  166. printf(" -> New Client\n");
  167. if (pthread_create(&mythread, NULL, echo_thread, comm_fd) != 0) {
  168. fprintf(stderr, "ERROR: pthread_create()\n");
  169. pthread_mutex_destroy(&list_mutex);
  170.  
  171. free(comm_fd);
  172. goto cleanup;
  173. }
  174.  
  175.  
  176. }
  177.  
  178. cleanup:
  179. close(listen_fd);
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement