Client.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #include "Client.h"
  2. #include <sys/socket.h>
  3. #include <stdio.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #define __USE_MISC
  7. #include <arpa/inet.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #define __USE_POSIX
  12. #include <sys/types.h>
  13. #include <signal.h>
  14. #include "SocketUtils.h"
  15. void clientInitDefaults(Client* c)
  16. {
  17. c->port = -1;
  18. c->socket = -1;
  19. c->thread = -1;
  20. c->hAmount = -1;
  21. c->hIndex = -1;
  22. c->handlers = NULL;
  23. }
  24. static void* serverHandler(void* data)
  25. {
  26. Client* c = (Client*) data;
  27. Stream in;
  28. while(1)
  29. {
  30. streamInit(&in, 1024);
  31. int size = receiveAll(c->socket, &in);
  32. if(size > 0)
  33. {
  34. int package;
  35. if(streamGetInt(&in, &package) == -1)
  36. {
  37. printf("Invalid package %d from server\n", package);
  38. }
  39. else
  40. {
  41. if(package >= 0 && package < c->hIndex)
  42. {
  43. //printf("Received package %d from server\n", package);
  44. c->handlers[package](&in);
  45. }
  46. else
  47. {
  48. printf("Invalid package %d from server\n", package);
  49. }
  50. }
  51. }
  52. else if(size == 0)
  53. {
  54. printf("Server closed remote socket\n");
  55. streamRemove(&in);
  56. break;
  57. }
  58. else
  59. {
  60. streamRemove(&in);
  61. break;
  62. }
  63. streamRemove(&in);
  64. }
  65. __pid_t pid = getpid();
  66. kill(pid, SIGINT);
  67. return NULL;
  68. }
  69. int clientInit(Client* c, char* ip, short port)
  70. {
  71. c->port = port;
  72. c->hAmount = 0;
  73. c->hIndex = 0;
  74. c->socket = socket(AF_INET, SOCK_STREAM, 0);
  75. if(c->socket == -1)
  76. {
  77. perror("Cannot create socket");
  78. clientRemove(c);
  79. return -1;
  80. }
  81. struct sockaddr_in socketData;
  82. memset(&socketData, 0, sizeof(struct sockaddr_in));
  83. socketData.sin_family = AF_INET;
  84. socketData.sin_port = htons(port);
  85. if(inet_aton(ip, &socketData.sin_addr) == 0)
  86. {
  87. printf("'%s' is not a valid ip\n", ip);
  88. clientRemove(c);
  89. return -1;
  90. }
  91. if(connect(c->socket, (struct sockaddr*) &socketData, sizeof(struct sockaddr_in)) == 0)
  92. {
  93. printf("Connection with server (%s) established\n", inet_ntoa(socketData.sin_addr));
  94. Stream in;
  95. streamInit(&in, 1024);
  96. if(clientReceive(c, &in) == -1)
  97. {
  98. printf("Server did not respond");
  99. streamRemove(&in);
  100. clientRemove(c);
  101. return -1;
  102. }
  103. else
  104. {
  105. char answer;
  106. if(streamGetChar(&in, &answer) == -1)
  107. {
  108. printf("Server sent an invalid response");
  109. streamRemove(&in);
  110. clientRemove(c);
  111. return -1;
  112. }
  113. else if(answer > 0)
  114. {
  115. printf("Successfully received server response\n");
  116. char welcome[100];
  117. streamGetChars(&in, welcome, 100);
  118. printf("%s\n", welcome);
  119. if(pthread_create(&c->thread, NULL, serverHandler, (void*) c) != 0)
  120. {
  121. perror("Cannot create thread");
  122. streamRemove(&in);
  123. clientRemove(c);
  124. return -1;
  125. }
  126. }
  127. else
  128. {
  129. char error[100];
  130. streamGetChars(&in, error, 100);
  131. printf("%s\n", error);
  132. streamRemove(&in);
  133. clientRemove(c);
  134. return -1;
  135. }
  136. }
  137. streamRemove(&in);
  138. }
  139. else
  140. {
  141. perror("No server is available");
  142. clientRemove(c);
  143. return -1;
  144. }
  145. return 0;
  146. }
  147. int clientReceive(Client* c, Stream* in)
  148. {
  149. int size = receiveAll(c->socket, in);
  150. if(size > 0)
  151. {
  152. return 0;
  153. }
  154. return -1;
  155. }
  156. void clientRemove(Client* c)
  157. {
  158. c->port = -1;
  159. if(c->socket != -1)
  160. {
  161. if(close(c->socket) != 0)
  162. {
  163. perror("Cannot close client socket");
  164. }
  165. else
  166. {
  167. printf("socket closed\n");
  168. }
  169. }
  170. if(c->thread != -1)
  171. {
  172. pthread_cancel(c->thread);
  173. pthread_join(c->thread, NULL);
  174. printf("thread cancel\n");
  175. c->thread = -1;
  176. }
  177. c->hAmount = -1;
  178. c->hIndex = -1;
  179. if(c->handlers != NULL)
  180. {
  181. free(c->handlers);
  182. c->handlers = NULL;
  183. }
  184. }
  185. int clientSendData(Client* c, Stream* out)
  186. {
  187. int bytes = sendAll(c->socket, out);
  188. if(bytes == -1)
  189. {
  190. return -1;
  191. }
  192. //printf("%d Bytes from %d sent\n", bytes, s->index);
  193. return 0;
  194. }
  195. void clientRegisterHandler(Client* c, ClientStreamFunction f)
  196. {
  197. if(c->hAmount == c->hIndex)
  198. {
  199. if(c->hAmount == 0)
  200. {
  201. c->handlers = malloc(sizeof(ClientStreamFunction));
  202. c->hAmount = 1;
  203. }
  204. else
  205. {
  206. c->hAmount *= 2;
  207. ClientStreamFunction* functions = malloc(sizeof(ClientStreamFunction) * c->hAmount);
  208. for(int i = 0; i < c->hIndex; i++)
  209. {
  210. functions[i] = c->handlers[i];
  211. }
  212. free(c->handlers);
  213. c->handlers = functions;
  214. }
  215. }
  216. c->handlers[c->hIndex] = f;
  217. c->hIndex++;
  218. }