client.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9. #include <netdb.h>
  10. #include "functions.h"
  11. #define BUFLEN 256
  12. int main(int argc, char** argv)
  13. {
  14. FILE* output;
  15. int serverSocketTCP, numBytesRecived, it_socket;
  16. int auth = 0, tmp_nc, tr = 0;
  17. double tmp_s;
  18. struct sockaddr_in serverSocketAddrTCP;
  19. fd_set read_fds, tmp_fds;
  20. char buffer[BUFLEN], command[BUFLEN], filename[20];
  21. if (argc != 3)
  22. clientUsage();
  23. sprintf(filename, "client-%d.log", getpid());
  24. //Deschidere fisier output.
  25. output = fopen(filename, "w");
  26. //Deschidere socket.
  27. serverSocketTCP = socket(PF_INET, SOCK_STREAM, 0);
  28. //Configurare socket.
  29. serverSocketAddrTCP.sin_family = AF_INET;
  30. serverSocketAddrTCP.sin_port = htons(atoi(argv[2]));
  31. inet_aton(argv[1], &serverSocketAddrTCP.sin_addr);
  32. //Connectare la server.
  33. if ((connect(serverSocketTCP, ((struct sockaddr*)&serverSocketAddrTCP),
  34. sizeof(struct sockaddr))) != 0) {
  35. fprintf(output, "-10 : Eroare apel connect \n");
  36. }
  37. //Adaugare stdin in multimea descriptorilor de citire.
  38. FD_SET(0, &read_fds);
  39. //Adaugare socket server in multimea descriptorilor de citire.
  40. FD_SET(serverSocketTCP, &read_fds);
  41. while (1) {
  42. tmp_fds = read_fds;
  43. if (select(serverSocketTCP + 1, &tmp_fds, NULL, NULL, NULL) == -1) {
  44. fprintf(output, "-10 : Eroare apel select \n");
  45. }
  46. for (it_socket = 0; it_socket <= serverSocketTCP; it_socket++) {
  47. if (FD_ISSET(it_socket, &tmp_fds)) {
  48. if (it_socket == serverSocketTCP) {
  49. numBytesRecived = recv(serverSocketTCP, &buffer, BUFLEN, 0);
  50. if (numBytesRecived <= 0) {
  51. if (numBytesRecived < 0) {
  52. fprintf(output, "-10 : Eroare apel recv \n");
  53. }
  54. fclose(output);
  55. close(it_socket);
  56. FD_CLR(it_socket, &read_fds);
  57. return 0;
  58. } else {
  59. //Am primit mesaj din partea server-ului.
  60. fprintf(output, "IBANK> %s", buffer);
  61. sscanf(buffer, "%s", command);
  62. if (strcmp(command, "Welcome") == 0) {
  63. auth = 1;
  64. break;
  65. }
  66. }
  67. } else if (it_socket == 0) {
  68. //Input pentru comanda ce va fi trimisa catre server.
  69. fgets(buffer, BUFLEN, stdin);
  70. fprintf(output, "%s", buffer);
  71. sscanf(buffer, "%s", command);
  72. if (strcmp(command, "login") == 0) {
  73. if (auth == 1) {
  74. //Nu mai trimit comanda la server.
  75. fprintf(output, "IBANK> -2 : Sesiune deja deschisa\n");
  76. break;
  77. } else {
  78. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  79. break;
  80. }
  81. } else {
  82. if (strcmp(command, "quit") == 0) {
  83. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  84. FD_CLR(serverSocketTCP, &read_fds);
  85. close(serverSocketTCP);
  86. fclose(output);
  87. return 0;
  88. }
  89. if (auth == 0) {
  90. fprintf(output, "IBANK> -1 : Clientul nu este autentificat \n");
  91. } else {
  92. if (strcmp(command, "logout") == 0) {
  93. if (auth == 0) {
  94. fprintf(output, "-1 : Clientul nu este autentificat \n");
  95. } else {
  96. auth = 0;
  97. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  98. break;
  99. }
  100. }
  101. if (strcmp(command, "listsold") == 0) {
  102. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  103. break;
  104. }
  105. if (strcmp(command, "transfer") == 0) {
  106. sscanf(buffer, "%s %d %lf", &command, &tmp_nc, &tmp_s);
  107. tr = 1;
  108. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  109. break;
  110. }
  111. if (tr == 1) {
  112. if (command[0] == 'y') {
  113. sprintf(buffer, "transfer2 %d %.2f", tmp_nc, tmp_s);
  114. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  115. tr = 0;
  116. break;
  117. } else {
  118. sprintf(buffer, "transfer2 0 0");
  119. send(serverSocketTCP, buffer, strlen(buffer) + 1, 0);
  120. tr = 0;
  121. break;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }
  129. }
  130. return 0;
  131. }