123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- /**
- * Simple OSInet TCP test client
- *
- * WARNING: contains various assumptions regarding the use of IPv4
- * This is NOT production-quality code, just a quick debug test
- *
- * Sample values:
- * HOST "profile.microscopy-analysis.com"
- * PORT 8080
- * @version $Id: netclient.c,v 1.2 2008-08-01 09:00:33 marand Exp $
- * @copyright (c) 2008 Ouest Systemes Informatiques (OSInet). All rights reserved
- */
-
- #include "features.h"
- #include "string.h"
- #include "stdlib.h"
- #include "unistd.h"
- #include "sys/socket.h"
- #include "netinet/in.h"
- #include "arpa/inet.h"
- #include "netdb.h"
- #include "stdio.h"
- #define MAX_BUF 100
- /**
- * Dump client socket info for IPv4
- */
- void showServ(struct sockaddr_in *sn)
- {
- printf("IP Dump\n");
- printf(" Family [%d]\n", sn->sin_family);
- printf(" Host [%s]\n", inet_ntoa(sn->sin_addr));
- printf(" Port [%d]\n", ntohs(sn->sin_port));
- }
- /**
- * Return the last IP in the resolution list for host name
- */
- char *getServIp(char *name)
- {
- struct hostent *he = gethostbyname(name);
- int i;
- static char addr[MAX_BUF];
- struct in_addr *pin;
- if (he == NULL)
- return NULL;
-
- printf("Host check:\n Name %s\n Canonical name %s\n Addr len %d\n Family %d\n",
- name, he->h_name, he->h_length, he->h_addrtype);
- i = 0;
- while (he->h_addr_list[i] != NULL)
- {
- pin = (struct in_addr *) he->h_addr_list[i];
- strcpy(addr, inet_ntoa(*pin));
- printf(" IP %s\n", addr);
- i++;
- }
- return addr;
- }
- /**
- * Main function
- */
- int main (int argc, char **argv)
- {
- int sockd;
- struct sockaddr_in serv_name;
- char buf[MAX_BUF];
- int status;
- char *ip;
- if (argc != 3)
- {
- fprintf(stderr, "Syntax, %s <host> <port>\n", argv[0]);
- exit(1);
- }
- strcpy(buf, argv[1]);
- ip = getServIp(buf); /* warning: static result, overwritten by each call */
- sockd = socket(AF_INET, SOCK_STREAM, 0);
- if (sockd == -1)
- {
- perror("Socket creation");
- exit(2);
- }
- serv_name.sin_family = AF_INET;
- /* fprintf(stderr, "Inet_aton on %s returns %d\n", ip, */ inet_aton(ip, &serv_name.sin_addr);
- serv_name.sin_port = htons(atoi(argv[2]));
- showServ(&serv_name);
- fprintf(stderr, "Trying to connect to %s:%d\n", argv[1], atoi(argv[2]));
- status = connect(sockd, (struct sockaddr *) &serv_name, sizeof(serv_name));
- if (status == -1)
- {
- perror("Connection error");
- exit(3);
- }
- fputs("Connection successful, closing\n", stderr);
- shutdown(sockd, 1);
- close(sockd);
- return 0;
- }
|