+ printf("%s [-a ADDRESS] [-p PORT]\n"
+ "\n"
+ "Start a client to connect to a command line server, using\n"
+ "a TCP socket.\n"
+ "The default behaviour is to connect to 127.0.0.1:1234\n"
+ "\n"
+ " -a ADDR: IPv4 or IPv6 local address to listen on (default\n"
+ " is 127.0.0.1).\n"
+ " -p PORT: port to connect to (default is 1234).\n",
+ prgname);
+}
+
+/* Parse the argument given in the command line of the application */
+static int
+parse_args(int argc, char **argv)
+{
+ int opt, ret;
+ char **argvopt;
+ int option_index;
+ char *prgname = argv[0];
+ char *end = NULL;
+ static struct option lgopts[] = {
+ {0, 0, 0, 0}
+ };
+
+ argvopt = argv;
+
+ while ((opt = getopt_long(argc, argvopt, "a:p:",
+ lgopts, &option_index)) != EOF) {
+
+ switch (opt) {
+
+ case 'a':
+ if (strchr(optarg, '.') != NULL) {
+ struct sockaddr_in *sin =
+ (struct sockaddr_in *)&addr;
+ addr.sa_family = AF_INET;
+ ret = inet_pton(AF_INET, optarg,
+ &sin->sin_addr);
+ }
+ else {
+ struct sockaddr_in6 *sin6 =
+ (struct sockaddr_in6 *)&addr;
+ addr.sa_family = AF_INET6;
+ ret = inet_pton(AF_INET6, optarg,
+ &sin6->sin6_addr);
+ }
+
+ if (ret != 1) {
+ printf("Bad address\n");
+ usage(prgname);
+ return -1;
+ }
+ break;
+
+ case 'p':
+ port = strtoul(optarg, &end, 0);
+ if ((optarg[0] == '\0') ||
+ (end == NULL) ||
+ (*end != '\0')) {
+ printf("Invalid port\n");
+ usage(prgname);
+ return -1;
+ }
+
+ break;
+
+ /* long options */
+ case 0:
+ /* if (!strcmp(lgopts[option_index].name, "option")) */
+ break;
+
+ default:
+ usage(prgname);
+ return -1;
+ }
+ }
+
+ if (argc != optind) {
+ printf("Invalid argument\n");
+ usage(prgname);
+ return -1;
+ }
+
+ ret = optind-1;
+
+ return ret;
+}
+
+int main(int argc, char **argv)
+{
+ struct sockaddr_in *sin = (struct sockaddr_in *)&addr;
+ struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&addr;