Client.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <sys/socket.h>
  2. #include <stdio.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #define __USE_MISC
  6. #include <arpa/inet.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "Stream.h"
  10. #include "Client.h"
  11. #include "SocketUtils.h"
  12. void clientInitDefaults(Client* c)
  13. {
  14. c->port = -1;
  15. c->socket = -1;
  16. }
  17. int clientInit(Client* c, char* ip, short port)
  18. {
  19. c->port = port;
  20. c->socket = socket(AF_INET, SOCK_STREAM, 0);
  21. if(c->socket == -1)
  22. {
  23. perror("Cannot create socket");
  24. clientRemove(c);
  25. return -1;
  26. }
  27. struct sockaddr_in socketData;
  28. memset(&socketData, 0, sizeof(struct sockaddr_in));
  29. socketData.sin_family = AF_INET;
  30. socketData.sin_port = htons(port);
  31. if(inet_aton(ip, &socketData.sin_addr) == 0)
  32. {
  33. printf("'%s' is not a valid ip\n", ip);
  34. clientRemove(c);
  35. return -1;
  36. }
  37. if(connect(c->socket, (struct sockaddr*) &socketData, sizeof(struct sockaddr_in)) == 0)
  38. {
  39. printf("Connection with server (%s) established\n", inet_ntoa(socketData.sin_addr));
  40. Stream in;
  41. streamInit(&in, 1024);
  42. if(clientReceive(c, &in) == -1)
  43. {
  44. printf("Server did not respond");
  45. streamRemove(&in);
  46. clientRemove(c);
  47. return -1;
  48. }
  49. else
  50. {
  51. char answer;
  52. if(streamGetChar(&in, &answer) == -1)
  53. {
  54. printf("Server sent an invalid response");
  55. streamRemove(&in);
  56. clientRemove(c);
  57. return -1;
  58. }
  59. else if(answer > 0)
  60. {
  61. printf("Successfully received server response\n");
  62. char welcome[100];
  63. streamGetChars(&in, welcome, 100);
  64. printf("%s\n", welcome);
  65. }
  66. else
  67. {
  68. char error[100];
  69. streamGetChars(&in, error, 100);
  70. printf("%s\n", error);
  71. streamRemove(&in);
  72. clientRemove(c);
  73. return -1;
  74. }
  75. }
  76. streamRemove(&in);
  77. }
  78. else
  79. {
  80. perror("No server is available");
  81. clientRemove(c);
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. int clientReceive(Client* c, Stream* in)
  87. {
  88. int size = receiveAll(c->socket, in);
  89. if(size > 0)
  90. {
  91. return 0;
  92. }
  93. return -1;
  94. }
  95. void clientRemove(Client* c)
  96. {
  97. c->port = -1;
  98. if(c->socket != -1)
  99. {
  100. if(close(c->socket) != 0)
  101. {
  102. perror("Cannot close client socket");
  103. }
  104. else
  105. {
  106. printf("socket closed\n");
  107. }
  108. }
  109. }
  110. void clearInput()
  111. {
  112. int c = getchar();
  113. while(c != EOF && c != '\n')
  114. {
  115. c = getchar();
  116. }
  117. }
  118. int readInt()
  119. {
  120. int type;
  121. while(scanf("%d", &type) == 0)
  122. {
  123. printf("Not a number\n");
  124. clearInput();
  125. }
  126. clearInput();
  127. return type;
  128. }
  129. void clientWaitForData(Client* c)
  130. {
  131. while(1)
  132. {
  133. printf("Choose a package: ");
  134. int type = readInt();
  135. if(type == -1)
  136. {
  137. break;
  138. }
  139. else if(type >= 0 && type < 4)
  140. {
  141. Stream out;
  142. streamInit(&out, 16);
  143. streamWriteChar(&out, type);
  144. switch(type)
  145. {
  146. case 0:
  147. printf("first number: ");
  148. streamWriteInt(&out, readInt());
  149. printf("second number: ");
  150. streamWriteInt(&out, readInt());
  151. break;
  152. case 1:
  153. printf("text: ");
  154. char buffer[128];
  155. fgets(buffer, 128, stdin);
  156. if(strlen(buffer) == 127 && buffer[126] != '\n')
  157. {
  158. clearInput();
  159. }
  160. streamWriteChars(&out, buffer);
  161. break;
  162. case 2:
  163. for(int i = 0; i < 260; i++)
  164. {
  165. streamWriteInt(&out, i);
  166. }
  167. break;
  168. }
  169. if(clientSendData(c, &out) == -1)
  170. {
  171. streamRemove(&out);
  172. break;
  173. }
  174. streamRemove(&out);
  175. }
  176. else
  177. {
  178. printf("%d is not a valid package number\n", type);
  179. }
  180. }
  181. }
  182. int clientSendData(Client* c, Stream* s)
  183. {
  184. int bytes = sendAll(c->socket, s);
  185. if(bytes == -1)
  186. {
  187. perror("Cannot send data");
  188. return -1;
  189. }
  190. //printf("%d Bytes from %d sent\n", bytes, s->index);
  191. return 0;
  192. }