netclient.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * Simple OSInet TCP test client
  3. *
  4. * WARNING: contains various assumptions regarding the use of IPv4
  5. * This is NOT production-quality code, just a quick debug test
  6. *
  7. * Sample values:
  8. * HOST "profile.microscopy-analysis.com"
  9. * PORT 8080
  10. * @version $Id: netclient.c,v 1.2 2008-08-01 09:00:33 marand Exp $
  11. * @copyright (c) 2008 Ouest Systemes Informatiques (OSInet). All rights reserved
  12. */
  13. #include "features.h"
  14. #include "string.h"
  15. #include "stdlib.h"
  16. #include "unistd.h"
  17. #include "sys/socket.h"
  18. #include "netinet/in.h"
  19. #include "arpa/inet.h"
  20. #include "netdb.h"
  21. #include "stdio.h"
  22. #define MAX_BUF 100
  23. /**
  24. * Dump client socket info for IPv4
  25. */
  26. void showServ(struct sockaddr_in *sn)
  27. {
  28. printf("IP Dump\n");
  29. printf(" Family [%d]\n", sn->sin_family);
  30. printf(" Host [%s]\n", inet_ntoa(sn->sin_addr));
  31. printf(" Port [%d]\n", ntohs(sn->sin_port));
  32. }
  33. /**
  34. * Return the last IP in the resolution list for host name
  35. */
  36. char *getServIp(char *name)
  37. {
  38. struct hostent *he = gethostbyname(name);
  39. int i;
  40. static char addr[MAX_BUF];
  41. struct in_addr *pin;
  42. if (he == NULL)
  43. return NULL;
  44. printf("Host check:\n Name %s\n Canonical name %s\n Addr len %d\n Family %d\n",
  45. name, he->h_name, he->h_length, he->h_addrtype);
  46. i = 0;
  47. while (he->h_addr_list[i] != NULL)
  48. {
  49. pin = (struct in_addr *) he->h_addr_list[i];
  50. strcpy(addr, inet_ntoa(*pin));
  51. printf(" IP %s\n", addr);
  52. i++;
  53. }
  54. return addr;
  55. }
  56. /**
  57. * Main function
  58. */
  59. int main (int argc, char **argv)
  60. {
  61. int sockd;
  62. struct sockaddr_in serv_name;
  63. char buf[MAX_BUF];
  64. int status;
  65. char *ip;
  66. if (argc != 3)
  67. {
  68. fprintf(stderr, "Syntax, %s <host> <port>\n", argv[0]);
  69. exit(1);
  70. }
  71. strcpy(buf, argv[1]);
  72. ip = getServIp(buf); /* warning: static result, overwritten by each call */
  73. sockd = socket(AF_INET, SOCK_STREAM, 0);
  74. if (sockd == -1)
  75. {
  76. perror("Socket creation");
  77. exit(2);
  78. }
  79. serv_name.sin_family = AF_INET;
  80. /* fprintf(stderr, "Inet_aton on %s returns %d\n", ip, */ inet_aton(ip, &serv_name.sin_addr);
  81. serv_name.sin_port = htons(atoi(argv[2]));
  82. showServ(&serv_name);
  83. fprintf(stderr, "Trying to connect to %s:%d\n", argv[1], atoi(argv[2]));
  84. status = connect(sockd, (struct sockaddr *) &serv_name, sizeof(serv_name));
  85. if (status == -1)
  86. {
  87. perror("Connection error");
  88. exit(3);
  89. }
  90. fputs("Connection successful, closing\n", stderr);
  91. shutdown(sockd, 1);
  92. close(sockd);
  93. return 0;
  94. }