next up previous
Next: サンプルソースIPv4対応 server4.c Up: プログラムソース Previous: IPv4,IPv6対応 server0.c

サンプルソースIPv4対応 client4.c

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>

int main __P((int, char **));

int
main(argc, argv)
     int argc;
     char **argv;
{
  struct hostent *hp;
  struct servent *sp;
  unsigned long lport;
  u_int16_t port;
  char *ep;
  struct sockaddr_in dst;
  int dstlen;
  ssize_t l;
  int s;
  char hbuf[INET_ADDRSTRLEN];
  char buf[1024];

  /* 引数の数をチェック */
  if (argc != 3) {
    fprintf(stderr, "usage: test host port\n");
    exit(1);
    /*NOTREACHED*/
  }

  /* アドレスをバイナリに変換 */
  hp = gethostbyname(argv[1]);
  if (!hp) {
    fprintf(stderr, "%s: %s\n", argv[1], hstrerror(h_errno));
    exit(1);
    /*NOTREACHED*/
  }

  if (hp->h_length != sizeof(dst.sin_addr)) {
    fprintf(stderr, "%s: unexpected address length\n", argv[1]);
    exit(1);
    /*NOTREACHED*/
  }

  /* ポート番号をバイナリに変換 */
  sp = getservbyname(argv[2], "TCP");
  if (sp) {
    port = sp->s_port & 0xffff;
  } else {
    ep = NULL;
    errno = 0;
    lport = strtoul(argv[2], &ep, 10);
    if (!*argv[2] || errno || !ep || *ep) {
      fprintf(stderr, "%s: no such service\n", argv[2]);
      exit(1);
      /*NOTREACHED*/
    }
    if (lport & ~0xffff) {
      fprintf(stderr, "%s: out of range\n", argv[2]);
      exit(1);
      /*NOTREACHED*/
    }

    port = htons(lport & 0xffff);
  }
  endservent();

  /* 最初に帰ってきたアドレスに接続する */
  memset(&dst, 0, sizeof(dst));
  dst.sin_family = AF_INET;
  /* linux/Solarisでは以下の行は不要 */
  dst.sin_len = sizeof(struct sockaddr_in);
  memcpy(&dst.sin_addr, hp->h_addr, sizeof(dst.sin_addr));
  dst.sin_port = port;
  dstlen = sizeof(struct sockaddr_in);

  s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (s < 0) {
    perror("socket");
    exit(1);
    /*NOTREACHED*/
  }

  inet_ntop(AF_INET, hp->h_addr, hbuf, sizeof(hbuf));
  fprintf(stderr, "trying %s port %u\n", hbuf, ntohs(port));

  if (connect(s, (struct sockaddr *)&dst, dstlen) < 0) {
    perror("connect");
    exit(1);
    /*NOTREACHED*/
  }

  while ((l = read(s, buf, sizeof(buf))) > 0)
    write(STDOUT_FILENO, buf, l);
  close(s);
  exit(0);
  /*NOTREACHED*/
}



fumiya nakamura 2006-02-10