Client.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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->handler = NULL;
  21. }
  22. static void* serverHandler(void* data)
  23. {
  24. Client* c = (Client*) data;
  25. Stream in;
  26. while(1)
  27. {
  28. streamInit(&in, 1024);
  29. int size = receiveAll(c->socket, &in);
  30. if(size > 0)
  31. {
  32. c->handler(&in);
  33. }
  34. else if(size == 0)
  35. {
  36. printf("Server closed remote socket\n");
  37. streamRemove(&in);
  38. break;
  39. }
  40. else
  41. {
  42. streamRemove(&in);
  43. break;
  44. }
  45. streamRemove(&in);
  46. }
  47. __pid_t pid = getpid();
  48. kill(pid, SIGINT);
  49. return NULL;
  50. }
  51. int clientInit(Client* c, char* ip, short port)
  52. {
  53. c->port = port;
  54. c->socket = socket(AF_INET, SOCK_STREAM, 0);
  55. if(c->socket == -1)
  56. {
  57. perror("Cannot create socket");
  58. clientRemove(c);
  59. return -1;
  60. }
  61. struct sockaddr_in socketData;
  62. memset(&socketData, 0, sizeof(struct sockaddr_in));
  63. socketData.sin_family = AF_INET;
  64. socketData.sin_port = htons(port);
  65. if(inet_aton(ip, &socketData.sin_addr) == 0)
  66. {
  67. printf("'%s' is not a valid ip\n", ip);
  68. clientRemove(c);
  69. return -1;
  70. }
  71. if(connect(c->socket, (struct sockaddr*) &socketData, sizeof(struct sockaddr_in)) == 0)
  72. {
  73. printf("Connection with server (%s) established\n", inet_ntoa(socketData.sin_addr));
  74. Stream in;
  75. streamInit(&in, 1024);
  76. if(clientReceive(c, &in) == -1)
  77. {
  78. printf("Server did not respond");
  79. streamRemove(&in);
  80. clientRemove(c);
  81. return -1;
  82. }
  83. else
  84. {
  85. char answer;
  86. if(streamGetChar(&in, &answer) == -1)
  87. {
  88. printf("Server sent an invalid response");
  89. streamRemove(&in);
  90. clientRemove(c);
  91. return -1;
  92. }
  93. else if(answer > 0)
  94. {
  95. printf("Successfully received server response\n");
  96. char welcome[100];
  97. streamGetChars(&in, welcome, 100);
  98. printf("%s\n", welcome);
  99. if(pthread_create(&c->thread, NULL, serverHandler, (void*) c) != 0)
  100. {
  101. perror("Cannot create thread");
  102. streamRemove(&in);
  103. clientRemove(c);
  104. return -1;
  105. }
  106. }
  107. else
  108. {
  109. char error[100];
  110. streamGetChars(&in, error, 100);
  111. printf("%s\n", error);
  112. streamRemove(&in);
  113. clientRemove(c);
  114. return -1;
  115. }
  116. }
  117. streamRemove(&in);
  118. }
  119. else
  120. {
  121. perror("No server is available");
  122. clientRemove(c);
  123. return -1;
  124. }
  125. return 0;
  126. }
  127. int clientReceive(Client* c, Stream* in)
  128. {
  129. int size = receiveAll(c->socket, in);
  130. if(size > 0)
  131. {
  132. return 0;
  133. }
  134. return -1;
  135. }
  136. void clientRemove(Client* c)
  137. {
  138. c->port = -1;
  139. if(c->socket != -1)
  140. {
  141. if(close(c->socket) != 0)
  142. {
  143. perror("Cannot close client socket");
  144. }
  145. else
  146. {
  147. printf("socket closed\n");
  148. }
  149. }
  150. if(c->thread != -1)
  151. {
  152. pthread_cancel(c->thread);
  153. pthread_join(c->thread, NULL);
  154. printf("thread cancel\n");
  155. c->thread = -1;
  156. }
  157. c->handler = NULL;
  158. }
  159. int clientSendData(Client* c, Stream* out)
  160. {
  161. int bytes = sendAll(c->socket, out);
  162. if(bytes == -1)
  163. {
  164. return -1;
  165. }
  166. //printf("%d Bytes from %d sent\n", bytes, s->index);
  167. return 0;
  168. }
  169. void clientRegisterHandler(Client* c, ClientStreamFunction f)
  170. {
  171. c->handler = f;
  172. }