Guest User

Untitled

a guest
Jan 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. #include "header.h"
  2.  
  3. void app(SOCKET socket)
  4. {
  5. char pseudo[24];
  6. int statu;
  7. fd_set readfs;
  8.  
  9. strcpy(pseudo, "[");
  10. strcat(pseudo, getname(sizeof(pseudo)));
  11. strcat(pseudo, "] ");
  12.  
  13. while (1)
  14. {
  15. char *buffer = malloc(sizeof(char) * 1024);
  16. char *msg = malloc(sizeof(char) * 1000);
  17.  
  18. FD_ZERO(&readfs);
  19. FD_SET(socket, &readfs);
  20. FD_SET(STDIN_FILENO, &readfs);
  21.  
  22. if (select(socket + 1, &readfs, NULL, NULL, NULL) == -1)
  23. {
  24. perror("select()");
  25. exit(errno);
  26. }
  27. if (FD_ISSET(STDIN_FILENO, &readfs))
  28. {
  29. fgets(msg, sizeof(char) * 1000, stdin);
  30. strcpy(buffer, pseudo);
  31. strcat(buffer, msg);
  32. send_message(socket, buffer);
  33. cleanMsg(buffer, msg);
  34. }
  35. else if (FD_ISSET(socket, &readfs))
  36. {
  37. statu = receive_message(socket, buffer);
  38. if (statu == 0)
  39. {
  40. printf("Server disconnected !n");
  41. break;
  42. }
  43. else
  44. {
  45. printf("%sn", buffer);
  46. cleanMsg(buffer, msg);
  47. }
  48. }
  49. }
  50. close_connection(socket);
  51. }
  52.  
  53. void send_message(SOCKET socket, char *buffer)
  54. {
  55. int i;
  56.  
  57. for (i = 0; buffer[i] != 'n'; i++) ;
  58. buffer[i] = '';
  59.  
  60. if (send(socket, buffer, strlen(buffer), 0) < 0)
  61. {
  62. perror("send()");
  63. exit(errno);
  64. }
  65. }
  66.  
  67. int receive_message(SOCKET socket, char *buffer)
  68. {
  69. int statu = 0;
  70. if ((statu = recv(socket, buffer, 1024, 0)) < 0)
  71. perror("recv()");
  72. return statu;
  73. }
  74.  
  75. void cleanMsg(char *buffer, char *msg)
  76. {
  77. memset(buffer, 0, strlen(buffer));
  78. memset(msg, 0, strlen(msg));
  79. free(buffer);
  80. free(msg);
  81. }
  82.  
  83. char *getname(size_t namesize)
  84. {
  85. char *tmp = NULL;
  86.  
  87. tmp = malloc(namesize);
  88. getlogin_r(tmp, namesize);
  89.  
  90. return tmp;
  91. }
  92.  
  93. #include "header.h"
  94.  
  95. int app(SOCKET master_socket, serv_config *s_conf)
  96. {
  97. SOCKADDR_IN client_address = {0};
  98. SOCKET new_socket;
  99. int address_size = sizeof(client_address);
  100. int *clients_socket = NULL;
  101. int statu = 0;
  102. int fdmax;
  103. int sd;
  104. int i;
  105.  
  106. clients_socket = malloc(sizeof(int) * s_conf->max_client);
  107.  
  108. fd_set readfs;
  109.  
  110. for (i = 0; i < s_conf->max_client; i++)
  111. clients_socket[i] = 0;
  112.  
  113. while (1)
  114. {
  115.  
  116. char *buffer = malloc(sizeof(char) * 1024);
  117. char *msg = malloc(sizeof(char) * 1000);
  118.  
  119. FD_ZERO(&readfs);
  120. FD_SET(STDIN_FILENO, &readfs);
  121. FD_SET(master_socket, &readfs);
  122.  
  123. fdmax = master_socket;
  124.  
  125. for (i = 0; i < s_conf->max_client; i++)
  126. {
  127. sd = clients_socket[i];
  128. if (sd > 0)
  129. FD_SET(sd, &readfs);
  130. if (sd > fdmax)
  131. fdmax = sd;
  132. }
  133.  
  134. if (select(fdmax + 1, &readfs, NULL, NULL, NULL) == -1)
  135. {
  136. perror("select()");
  137. exit(errno);
  138. }
  139.  
  140. if (FD_ISSET(STDIN_FILENO, &readfs))
  141. {
  142. fgets(msg, sizeof(char) * 1000, stdin);
  143. strcpy(buffer, "[Server] ");
  144. strcat(buffer, msg);
  145. send_toall(clients_socket, 0, s_conf->max_client, buffer);
  146. cleanMsg(buffer, msg);
  147. }
  148. else if (FD_ISSET(master_socket, &readfs))
  149. {
  150. new_socket = accept(master_socket, (SOCKADDR *)&client_address, &address_size);
  151.  
  152. if (new_socket == INVALID_SOCKET)
  153. {
  154. perror("accept()");
  155. closesocket(new_socket);
  156. exit(errno);
  157. }
  158.  
  159. for (i = 0; i < s_conf->max_client; i++)
  160. {
  161. if (clients_socket[s_conf->max_client - 1] != 0)
  162. {
  163. strcpy(buffer, "Connection error: no more client can be connected.n");
  164. send_message(new_socket, buffer);
  165. cleanMsg(buffer, msg);
  166. shutdown(new_socket, 2);
  167. closesocket(new_socket);
  168. break;
  169. }
  170. else if (clients_socket[i] == 0)
  171. {
  172. clients_socket[i] = new_socket;
  173. printf("New client connected with socket %d from %s:%d, in slot %dn", clients_socket[i], inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port), i);
  174. strcpy(buffer, "Success connecting.n");
  175. send_message(clients_socket[i], buffer);
  176. cleanMsg(buffer, msg);
  177. break;
  178. }
  179. }
  180. }
  181. else
  182. {
  183. for (i = 0; i < s_conf->max_client; i++)
  184. {
  185. if (FD_ISSET(clients_socket[i], &readfs))
  186. {
  187. statu = receive_message(clients_socket[i], buffer);
  188. if (statu == 0)
  189. {
  190. printf("Socket %d Disconnectn", clients_socket[i]);
  191. shutdown(clients_socket[i], 2);
  192. closesocket(clients_socket[i]);
  193. clients_socket[i] = 0;
  194. break;
  195. }
  196. else
  197. {
  198. send_toall(clients_socket, clients_socket[i], s_conf->max_client, buffer);
  199. cleanMsg(buffer, msg);
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. }
  206. return *clients_socket;
  207. }
  208.  
  209. void send_toall(int *clients_socket, int actual_socket, int max, char *buffer)
  210. {
  211. int i;
  212.  
  213. for (i = 0; clients_socket[i] < max; i++)
  214. {
  215. if (clients_socket[i] != actual_socket && clients_socket[i] != 0)
  216. send_message(clients_socket[i], buffer);
  217. }
  218. }
  219.  
  220. void send_message(SOCKET socket, char *buffer)
  221. {
  222. int i;
  223.  
  224. for (i = 0; buffer[i] != 'n'; i++) ;
  225. buffer[i] = '';
  226.  
  227. if (send(socket, buffer, strlen(buffer), 0) < 0)
  228. {
  229. perror("send()");
  230. exit(errno);
  231. }
  232. }
  233.  
  234. int receive_message(SOCKET socket, char *buffer)
  235. {
  236. int statu = 0;
  237. if ((statu = recv(socket, buffer, 1024, 0)) < 0)
  238. perror("recv()");
  239. return statu;
  240. }
  241.  
  242. void cleanMsg(char *buffer, char *msg)
  243. {
  244. memset(buffer, 0, strlen(buffer));
  245. memset(msg, 0, strlen(msg));
  246. free(buffer);
  247. free(msg);
  248. }
Add Comment
Please, Sign In to add comment