Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.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 "Practical.h"
  11.  
  12. void DieWithUserMessage(const char *msg, const char *detail) {
  13. fputs(msg, stderr);
  14. fputs(": ", stderr);
  15. fputs(detail, stderr);
  16. fputc('\n', stderr);
  17. exit(1);
  18. }
  19.  
  20. void DieWithSystemMessage(const char *msg) {
  21. perror(msg);
  22. exit(1);
  23. }
  24.  
  25. int main(int argc, char *argv[]) {
  26. struct addrinfo *result, *rp;
  27. int sfd, s, j;
  28. char c;
  29.  
  30. if (argc != 3) // Check for correct number of parameters
  31. DieWithUserMessage("Parameter(s)", "<Server Address> [<Server Port>]");
  32.  
  33. char *servHostname = argv[1]; // First arg: Hostname
  34. char *servPort = argv[2]; // Second arg: Server port
  35.  
  36. // Obtain address(es) matching host/port
  37.  
  38. struct addrinfo addrCriteria; // Criteria for address match
  39. memset(&addrCriteria, 0, sizeof(addrCriteria)); // Zero out structure
  40. addrCriteria.ai_family = AF_UNSPEC; // Any address family
  41. addrCriteria.ai_socktype = SOCK_STREAM; // Only stream sockets
  42. addrCriteria.ai_protocol = IPPROTO_TCP; // Only TCP protocol
  43.  
  44. s = getaddrinfo(servHostname, servPort, &addrCriteria, &result);
  45. if (s != 0)
  46. DieWithUserMessage("getaddrinfo() failed", gai_strerror(s));
  47.  
  48. /* We should only get one struct back, but I'll do it the way the man pages say,
  49. Allowing for multiple possible addresses, trying each address until we connect.
  50. if socket() or connect() fail, close socket and try the next address */
  51.  
  52. for (rp = result; rp != NULL; rp = rp->ai_next) {
  53. sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  54. if (sfd == -1) /* Failure to connect */
  55. continue;
  56.  
  57. if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
  58. break; /* Successfully connected */
  59.  
  60. shutdown(sfd, SHUT_RDWR); /* Close the socket that failed */
  61. }
  62.  
  63. freeaddrinfo(result); // Free addrinfo allocated in getaddrinfo()
  64.  
  65. if (rp == NULL) { /* No address succeeded if the rp is still null */
  66. fprintf(stderr, "Could not connect\n");
  67. exit(EXIT_FAILURE);
  68. }
  69.  
  70. /* Read stdin */
  71. while (EOF != (c = fgetc(stdin))) {
  72. write(sfd, &c, sizeof(c));
  73. }
  74.  
  75. /* write the stuff coming back from the server */
  76. while (read(sfd, &c, sizeof(c)) > 0){
  77. printf("\n");
  78. fputc(c, stdout);
  79. }
  80.  
  81. shutdown(sfd, SHUT_RDWR); /* Close the socket that failed */
  82. exit(0);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement