Server.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "Server.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/socket.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <sys/types.h>
  10. #include "Stream.h"
  11. void serverInitDefaults(Server* s)
  12. {
  13. s->maxClients = -1;
  14. s->port = -1;
  15. s->threads = NULL;
  16. s->clientSockets = NULL;
  17. s->connectSocket = -1;
  18. s->hAmount = -1;
  19. s->hIndex = -1;
  20. s->handlers = NULL;
  21. }
  22. int serverInit(Server* s, int maxClients, short port)
  23. {
  24. s->maxClients = maxClients;
  25. s->port = port;
  26. s->hAmount = 0;
  27. s->hIndex = 0;
  28. // initialize storage for clients
  29. s->threads = malloc(sizeof(pthread_t) * maxClients);
  30. s->clientSockets = malloc(sizeof(int) * maxClients);
  31. for(int i = 0; i < maxClients; i++)
  32. {
  33. s->threads[i] = -1;
  34. s->clientSockets[i] = -1;
  35. }
  36. // create the socket for clients to connect
  37. s->connectSocket = socket(AF_INET, SOCK_STREAM, 0);
  38. if(s->connectSocket == -1)
  39. {
  40. perror("Cannot create socket");
  41. serverRemove(s);
  42. return -1;
  43. }
  44. // prevents clients from blocking the port if the server exits
  45. struct linger sl;
  46. sl.l_onoff = 1;
  47. sl.l_linger = 0;
  48. if(setsockopt(s->connectSocket, SOL_SOCKET, SO_LINGER, &sl, sizeof(struct linger)) == -1)
  49. {
  50. perror("Cannot set non lingering");
  51. serverRemove(s);
  52. return -1;
  53. }
  54. // specifies data of the port and binds it
  55. struct sockaddr_in connectSocketData;
  56. memset(&connectSocketData, 0, sizeof(struct sockaddr_in));
  57. connectSocketData.sin_family = AF_INET;
  58. connectSocketData.sin_addr.s_addr = INADDR_ANY;
  59. connectSocketData.sin_port = htons(port);
  60. if(bind(s->connectSocket, (struct sockaddr*) &connectSocketData, sizeof(struct sockaddr_in)) != 0)
  61. {
  62. perror("Cannot bind socket");
  63. serverRemove(s);
  64. return -1;
  65. }
  66. // mark this socket as handler for connection requests
  67. if(listen(s->connectSocket, 5) != 0)
  68. {
  69. perror("Cannot start listening");
  70. serverRemove(s);
  71. return -1;
  72. }
  73. return 0;
  74. }
  75. void serverRemove(Server* s)
  76. {
  77. s->port = -1;
  78. if(s->threads != NULL)
  79. {
  80. for(int i = 0; i < s->maxClients; i++)
  81. {
  82. if(s->threads[i] != -1)
  83. {
  84. printf("%d thread\n", i);
  85. pthread_cancel(s->threads[i]);
  86. pthread_join(s->threads[i], NULL);
  87. }
  88. }
  89. free(s->threads);
  90. s->threads = NULL;
  91. }
  92. if(s->clientSockets != NULL)
  93. {
  94. for(int i = 0; i < s->maxClients; i++)
  95. {
  96. if(s->clientSockets[i] != -1)
  97. {
  98. if(close(s->clientSockets[i]) == -1)
  99. {
  100. printf("%d", i);
  101. perror(" cannot close client socket");
  102. }
  103. else
  104. {
  105. printf("%d closed\n", i);
  106. }
  107. }
  108. }
  109. free(s->clientSockets);
  110. s->clientSockets = NULL;
  111. }
  112. s->maxClients = -1;
  113. if(s->connectSocket != -1)
  114. {
  115. if(close(s->connectSocket) != 0)
  116. {
  117. perror("Cannot close server socket");
  118. }
  119. else
  120. {
  121. printf("connection listener closed\n");
  122. }
  123. s->connectSocket = -1;
  124. }
  125. s->hAmount = -1;
  126. s->hIndex = -1;
  127. if(s->handlers != NULL)
  128. {
  129. free(s->handlers);
  130. s->handlers = NULL;
  131. }
  132. }
  133. void* clientHandler(void* data)
  134. {
  135. int id = ((ThreadData*) data)->id;
  136. Server* s = ((ThreadData*) data)->server;
  137. Stream in;
  138. while(strcmp(in.data, "quit") != 0)
  139. {
  140. int size = recv(s->clientSockets[id], in.data, BUFFER_SIZE - 1, 0);
  141. if(size > 0)
  142. {
  143. in.data[size] = '\0';
  144. int package = ((int) in.data[0]);
  145. if(package >= 0 && package < s->hIndex)
  146. {
  147. printf("Received package %d from %d: %s\n", package, id, in.data);
  148. in.index = 1;
  149. in.size = size;
  150. s->handlers[package](&in);
  151. }
  152. else
  153. {
  154. printf("Invalid package %d from %d\n", package, id);
  155. }
  156. }
  157. else if(size == 0)
  158. {
  159. printf("Client %d closed remote socket\n", id);
  160. break;
  161. }
  162. else
  163. {
  164. perror("recv error");
  165. break;
  166. }
  167. }
  168. if(close(s->clientSockets[id]) != 0)
  169. {
  170. printf("%d", id);
  171. perror(" cannot close client socket");
  172. }
  173. else
  174. {
  175. printf("%d closed\n", id);
  176. }
  177. s->clientSockets[id] = -1;
  178. return NULL;
  179. }
  180. void serverWaitForConnection(Server* s)
  181. {
  182. socklen_t addrlen = sizeof(struct sockaddr_in);
  183. char buffer[BUFFER_SIZE];
  184. while(1)
  185. {
  186. printf("Waiting for connections...\n");
  187. struct sockaddr_in clientSocketData;
  188. int clientSocket = accept(s->connectSocket, (struct sockaddr*) &clientSocketData, &addrlen);
  189. if(clientSocket >= 0)
  190. {
  191. printf("Client connected from %s:%d...\n", inet_ntoa(clientSocketData.sin_addr), (int) ntohs(clientSocketData.sin_port));
  192. int i = 0;
  193. while(1)
  194. {
  195. if(s->clientSockets[i] == -1)
  196. {
  197. if(s->threads[i] != -1)
  198. {
  199. pthread_cancel(s->threads[i]);
  200. pthread_join(s->threads[i], NULL);
  201. }
  202. ThreadData data;
  203. data.id = i;
  204. data.server = s;
  205. if(pthread_create(&s->threads[i], NULL, clientHandler, (void*) &data) != 0)
  206. {
  207. perror("Cannot create thread");
  208. strcpy(buffer, "error: cannot create thread\n");
  209. if(send(clientSocket, buffer, strlen(buffer), 0) == -1)
  210. {
  211. perror("Cannot send error");
  212. }
  213. close(clientSocket);
  214. }
  215. else
  216. {
  217. s->clientSockets[i] = clientSocket;
  218. strcpy(buffer, "Welcome to server, please enter your command:\n");
  219. if(send(clientSocket, buffer, strlen(buffer), 0) == -1)
  220. {
  221. perror("Cannot send welcome message");
  222. close(clientSocket);
  223. s->clientSockets[i] = -1;
  224. }
  225. }
  226. break;
  227. }
  228. i++;
  229. if(i >= s->maxClients)
  230. {
  231. printf("max clients reached\n");
  232. strcpy(buffer, "error: max clients reached\n");
  233. if(send(clientSocket, buffer, strlen(buffer), 0) == -1)
  234. {
  235. perror("Cannot send error");
  236. }
  237. close(clientSocket);
  238. break;
  239. }
  240. }
  241. }
  242. }
  243. }
  244. void serverRegisterHandler(Server* s, StreamFunction f)
  245. {
  246. if(s->hAmount == s->hIndex)
  247. {
  248. if(s->hAmount == 0)
  249. {
  250. s->handlers = malloc(sizeof(StreamFunction));
  251. s->hAmount = 1;
  252. }
  253. else
  254. {
  255. s->hAmount *= 2;
  256. StreamFunction* functions = malloc(sizeof(StreamFunction) * s->hAmount);
  257. for(int i = 0; i < s->hIndex; i++)
  258. {
  259. functions[i] = s->handlers[i];
  260. }
  261. free(s->handlers);
  262. s->handlers = functions;
  263. }
  264. }
  265. s->handlers[s->hIndex] = f;
  266. s->hIndex++;
  267. }