Browse Source

Netclient initial commit

Frederic G. Marand 15 years ago
commit
b84d6a8405
3 changed files with 122 additions and 0 deletions
  1. 6 0
      Makefile
  2. 14 0
      README.txt
  3. 102 0
      netclient.c

+ 6 - 0
Makefile

@@ -0,0 +1,6 @@
+
+netclient: netclient.o
+	cc netclient.o -o netclient
+
+clean:
+	rm -f *.o netclient

+ 14 - 0
README.txt

@@ -0,0 +1,14 @@
+/**
+ * Simple TCP connection check
+ * (c) 1993-2008 OSI 
+ *
+ * Proprietary unpublished code
+ *
+ * WARNING: contains various assumptions regarding the use of IPv4
+ * This is NOT production-quality code, just a quick debug test
+ * for systems without a version of netcat, nc,  or an equivalent
+ */
+
+
+Use like:
+  ./netclient profile/microscopy-analysis.com 8080

+ 102 - 0
netclient.c

@@ -0,0 +1,102 @@
+/**
+ * Simple TCP connection check
+ * (c) 1993-2008 OSI 
+ *
+ * Proprietary unpublished code
+ *
+ * WARNING: contains various assumptions regarding the use of IPv4
+ * This is NOT production-quality code, just a quick debug test
+ */
+#include "sys/socket.h"
+#include "netinet/in.h"
+#include "arpa/inet.h"
+#include "netdb.h"
+#include "stdio.h"
+
+//	HOST	"profile.microscopy-analysis.com"
+//	PORT	8080
+//
+#define MAX_BUF	100
+
+/**
+ * Dump client socket info for IPv4
+ */
+int 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;
+  int					count;
+  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;
+  }