client.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <sys/types.h>
  2. #include <sys/socket.h>
  3. #include <netinet/in.h>
  4. #define __USE_MISC
  5. #include <arpa/inet.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <signal.h>
  11. #include "Stream.h"
  12. #define BUFFER_SIZE 1024
  13. #define PORT 6543
  14. int clientSocket = -1;
  15. void cleanUp()
  16. {
  17. if(clientSocket != -1)
  18. {
  19. if(close(clientSocket) != 0)
  20. {
  21. perror("Cannot close client socket");
  22. }
  23. else
  24. {
  25. printf("socket closed\n");
  26. }
  27. }
  28. }
  29. void interruptHandler(int signal)
  30. {
  31. cleanUp();
  32. exit(EXIT_SUCCESS);
  33. }
  34. void safeExit(int status)
  35. {
  36. cleanUp();
  37. exit(status);
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. signal(SIGINT, interruptHandler);
  42. signal(SIGKILL, interruptHandler);
  43. char buffer[BUFFER_SIZE];
  44. int size;
  45. if(argc < 2)
  46. {
  47. printf("Usage: %s server_address\n", argv[0]);
  48. safeExit(EXIT_FAILURE);
  49. }
  50. clientSocket = socket(AF_INET, SOCK_STREAM, 0);
  51. if(clientSocket == -1)
  52. {
  53. perror("Cannot create socket");
  54. safeExit(EXIT_FAILURE);
  55. }
  56. struct sockaddr_in clientSocketData;
  57. memset(&clientSocketData, 0, sizeof (clientSocketData));
  58. clientSocketData.sin_family = AF_INET;
  59. clientSocketData.sin_port = htons(PORT);
  60. if(inet_aton(argv[1], &clientSocketData.sin_addr) == 0)
  61. {
  62. printf("'%s' is not a valid ip\n", argv[1]);
  63. safeExit(EXIT_FAILURE);
  64. }
  65. if(connect(clientSocket, (struct sockaddr*) &clientSocketData, sizeof(clientSocketData)) == 0)
  66. {
  67. printf("Connection with server (%s) established\n", inet_ntoa(clientSocketData.sin_addr));
  68. size = recv(clientSocket, buffer, BUFFER_SIZE - 1, 0);
  69. if(size > 0)
  70. {
  71. buffer[size] = '\0';
  72. printf("%s", buffer);
  73. if(strncmp(buffer, "error", 5) == 0)
  74. {
  75. safeExit(EXIT_FAILURE);
  76. }
  77. }
  78. }
  79. else
  80. {
  81. perror("No server is available");
  82. safeExit(EXIT_FAILURE);
  83. }
  84. while(strcmp(buffer, "quit\n") != 0)
  85. {
  86. printf("Send message: ");
  87. fgets(buffer, BUFFER_SIZE, stdin);
  88. Stream out;
  89. out.index = 0;
  90. out.size = BUFFER_SIZE;
  91. streamWriteChar(0, &out);
  92. streamWriteInt(-423, &out);
  93. streamWriteInt(-6456463, &out);
  94. streamWriteInt(443534, &out);
  95. streamWriteInt(75676, &out);
  96. if(send(clientSocket, out.data, out.index, MSG_NOSIGNAL) == -1)
  97. {
  98. perror("Cannot send data");
  99. break;
  100. }
  101. }
  102. safeExit(EXIT_SUCCESS);
  103. return EXIT_SUCCESS;
  104. }