Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <strings.h>
  5. #include <signal.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/un.h>
  9. #include <errno.h>
  10. #include <netdb.h>
  11. #include <netinet/in.h>
  12. #include <arpa/inet.h>
  13.  
  14. #define TRIES 5 /* maximum number of attempts to conect to server */
  15. #define MAXCMD 500 /* max command length */
  16.  
  17.  
  18. static int connect_client(in_addr_t, int);
  19. static void client_handler(void);
  20. static void handle_reply(void);
  21.  
  22. static char *SIP, *SPORT;
  23. static int socketFD = -1;
  24.  
  25. int main(int argc, char *argv[]) {
  26. in_addr_t serverIP;
  27. int portNumber;
  28.  
  29. if (argc != 3) {
  30. printf("Usage: client serverIPAdr portNumber\n");
  31. exit(0);
  32. }
  33.  
  34. if (-1 == (serverIP = inet_addr(argv[1]))) {
  35. perror("First argument was not an IP adress");
  36. exit(0);
  37. }
  38. SIP = argv[1];
  39.  
  40. if (1 != sscanf(argv[2], "%d", &portNumber)) {
  41. perror("Second argument was not a port number");
  42. exit(0);
  43. }
  44. SPORT = argv[2];
  45.  
  46. /* connect client to server */
  47. if (-1 != (socketFD = connect_client(serverIP, portNumber))) {
  48. /* handle the connection */
  49. client_handler();
  50. /* clean up after us */
  51. close(socketFD);
  52. }
  53. }
  54.  
  55. static void client_handler() {
  56. char cmd[MAXCMD];
  57. for (;;){
  58. cmd[0] = '\0'; // reset the buffer to be an empty string
  59. fprintf(stdout, "%s:%s> ", SIP, SPORT);
  60. /* TODO get command for the commandline */
  61. printf("Enter command");
  62. fgets(cmd, MAXCMD, stdin);
  63.  
  64. /* TODO send command length to the server */
  65. unsigned long len = strlen(cmd);
  66. write(socketFD, &len, sizeof(len));
  67.  
  68. /* TODO send command to the server */
  69.  
  70. write(socketFD, cmd, sizeof(cmd));
  71.  
  72. /* If command was "exit" we should also terminate on this side */
  73. if (strncmp(cmd, "exit", 4) == 0) {
  74. return;
  75. }
  76.  
  77. /* If not we wait for the server's reply */
  78. handle_reply();
  79. }
  80. }
  81. static void handle_reply() {
  82. unsigned long bufferlen = 0;
  83. for(;;) {
  84. /* TODO receive message length...remember to check for errors! */
  85.  
  86. if((read(socketFD, &bufferlen, sizeof(unsigned long)))<0){
  87. printf("Could not read from server");
  88. bufferlen = 0;
  89. return;
  90. }
  91.  
  92. if (bufferlen == 0) {
  93. return; // finish on 0 length message
  94. }else{
  95.  
  96. char *buffer = malloc(bufferlen * sizeof(char));
  97. read(socketFD, buffer, bufferlen);
  98.  
  99. /* TODO print message to stdout */
  100. printf("%s", buffer);
  101.  
  102. /* TODO remember to allocate and free the receive buffer */
  103. free(buffer);
  104.  
  105. bufferlen = 0; // reset bufferlen
  106. }
  107. }
  108. }
  109.  
  110. static int connect_client(in_addr_t serverIP, int portNumber) {
  111. int attempts = 0, result = -1;
  112. struct sockaddr_in serverINETAdress;
  113.  
  114. /* TODO create a socket to server in socketFD, with default protocol for stream (TCP) */
  115.  
  116. socketFD = socket(AF_INET, SOCK_STREAM, 0);
  117. if (socketFD < 0)
  118. error("ERROR opening socket");
  119.  
  120. bzero((void *) &serverINETAdress, sizeof(serverINETAdress));
  121.  
  122. serverINETAdress.sin_family = AF_INET; /* Internet domain */
  123. serverINETAdress.sin_addr.s_addr = serverIP; /* IP adress of server */
  124. serverINETAdress.sin_port = htons((short)portNumber);
  125.  
  126. fprintf(stderr, "Connecting to: %s %d\n", inet_ntoa(serverINETAdress.sin_addr), portNumber);
  127.  
  128. /* TODO try to connect until number of TRIES is exceeded */
  129. while (attempts < TRIES) {
  130. printf("Attempting to connect: %i\n", attempts);
  131. if (connect(socketFD, (struct sockaddr *) &serverINETAdress,
  132. sizeof(serverINETAdress)) < 0) {
  133. error("ERROR connecting");
  134. attempts++;
  135. } else {
  136. printf("Connected\n");
  137. return socketFD; // only if successful
  138. }
  139. }
  140. return -1;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement