Client.c 4.3 KB

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