Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. //
  2.  
  3. #include <stdio.h>
  4.  
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <strings.h>
  8. #include <unistd.h>
  9. #include <arpa/inet.h>
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <sys/socket.h>
  13.  
  14.  
  15. #define MAXRCVLEN 500
  16. #define PORTNUM 2300
  17. #define LOGSIZE 50
  18.  
  19. int counter = 0 ;
  20.  
  21. typedef struct msg * msgref;
  22. typedef struct msg
  23. {
  24. char word[MAXRCVLEN + 2];
  25. char time[MAXRCVLEN];
  26. char user[MAXRCVLEN];
  27. }msg;
  28.  
  29. msg* logtable[LOGSIZE];
  30.  
  31.  
  32. int set_value(msgref N , char* w)
  33. {
  34. char* temp = w;
  35. //temp[strlen(temp) - 1] = '\0';
  36. strcpy(N-> word, temp );
  37. return 0;
  38. }
  39. msgref create_msg(char* w)
  40. {
  41. msgref e = malloc(4000);
  42. set_value(e, w);
  43. return e;
  44. }
  45. int logmessage(char *t)
  46. {
  47. //printf("log[%d] message :%s\n",counter,t);
  48. if(counter < LOGSIZE)
  49. {
  50. logtable[counter] = create_msg(t);
  51. counter++;
  52. }
  53. else
  54. {
  55. printf("out of space\n");
  56. for(int i = 0 ; i < counter -1 ; i++)
  57. {
  58. printf("[%d]\n",i);
  59. set_value(logtable[i],logtable[i+1]->word);
  60. }
  61. set_value(logtable[counter-1],t);
  62. }
  63. printf("logged!\n");
  64. return 0;
  65. }
  66. int showlog()
  67. {
  68. for(int i = 0 ; i < counter; i++)
  69. {
  70. printf("log[%d] = [%s]\n",i,logtable[i]->word);
  71. }
  72. return 0;
  73. }
  74.  
  75. int is_request(char* t)
  76. {
  77.  
  78. char type[6];
  79. strncpy(type,t,4);
  80.  
  81. printf("comparing request [%s]\n",type);
  82. if(strcasecmp(type,"GET ") == 0)
  83. {
  84. return 0;
  85. }
  86. else
  87. {
  88. return 1;
  89. }
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. if(is_request("GET ") == 0)
  94. {
  95. printf("GET is = 0\n");
  96. }
  97. if(is_request("ABC ") == 1)
  98. {
  99. printf("ABC is = 1\n");
  100. }
  101. printf("Server starting!\n");
  102. //char buffer[MAXRCVLEN + 1]; /* +1 so we can add null terminator */
  103. char*buffer = malloc(MAXRCVLEN +1);
  104.  
  105. struct sockaddr_in dest; /* socket info about the machine connecting to us */
  106. struct sockaddr_in serv; /* socket info about our server */
  107. int mysocket,len; /* socket used to listen for incoming connections */
  108. printf("Local vars initialized!\n");
  109. socklen_t socksize = sizeof(struct sockaddr_in);
  110.  
  111. memset(&serv, 0, sizeof(serv)); /* zero the struct before filling the fields */
  112. memset(&dest, 0, sizeof(dest)); /* zero the struct before filling the fields */
  113. printf("Memmory been reset!\n");
  114. serv.sin_family = AF_INET; /* set the type of connection to TCP/IP */
  115. serv.sin_addr.s_addr = htonl(INADDR_ANY); /* set our address to any interface */
  116. serv.sin_port = htons(PORTNUM); /* set the server port number */
  117. printf("Connecton types are set!\n");
  118. mysocket = socket(AF_INET, SOCK_STREAM, 0);
  119. if(mysocket == -1)
  120. {
  121. printf("Socket error!\n");
  122. close(mysocket);
  123. return EXIT_SUCCESS;
  124. }
  125. printf("Socket been created with signal : [%d]\n",mysocket);
  126. /* bind serv information to mysocket */
  127. int mybind = bind(mysocket, (struct sockaddr *)&serv, sizeof(struct sockaddr));
  128.  
  129. if(mybind == -1)
  130. {
  131. printf("ERROR! binding port with signal [%d]\n",mybind);
  132. close(mysocket);
  133. return EXIT_SUCCESS;
  134. }
  135.  
  136. /* start listening, allowing a queue of up to 1 pending connection */
  137. int g = listen(mysocket, 200);
  138. printf("listen to stocket with signal [%d]\n",g);
  139. int consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  140. printf("consocket has signal [%d]\n",consocket);
  141. while(1)
  142. {
  143. printf("Incoming connection from %s\n", inet_ntoa(dest.sin_addr));
  144.  
  145. len = recv(consocket, buffer, MAXRCVLEN, 0);
  146. printf("recived data len = %d\n",len);
  147. buffer[len] = '\0';
  148. if(len > 0)
  149. {
  150. if(is_request(buffer) == 1)
  151. {
  152. printf("Is not request\n");
  153. printf("Received %s (%d bytes).\n", buffer, len);
  154. logmessage(buffer);
  155. showlog();
  156. send(consocket, buffer, strlen(buffer), 0);
  157. close(consocket);
  158. consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  159. }
  160. else if(is_request(buffer) == 0)
  161. {
  162. printf("Is request\n");
  163. char*str = malloc(2000*20);
  164. strcpy(str,"");
  165. for(int i = 0 ; i < counter; i++)
  166. {
  167. printf("insert loggtable[%d] = %s\n",i,logtable[i]->word);
  168. strcat(str,logtable[i]->word);
  169. strcat(str,"<msg>\n");
  170. }
  171. printf("Sending data -->\n%s\n-end of data-",str);
  172. send(consocket, str, strlen(str), 0);
  173. close(consocket);
  174. consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  175. free(str);
  176. }
  177. }
  178. else
  179. {
  180. printf("empty message\n");
  181. close(consocket);
  182. consocket = accept(mysocket, (struct sockaddr *)&dest, &socksize);
  183. }
  184. }
  185.  
  186. close(mysocket);
  187. return EXIT_SUCCESS;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement