Annotation of 43BSDReno/sys/nfs/TEST/unix-tests/tools/tcp.c, revision 1.1

1.1     ! root        1: /*     @(#)tcp.c       1.1 88/10/12 NFS Rev 2 Testsuite        */
        !             2: /*
        !             3:  *  client for simple tcp ping program.
        !             4:  *  open connection with server, who will echo request back.
        !             5:  */
        !             6: #define TCP_PORT        3456
        !             7: 
        !             8: #include <stdio.h>
        !             9: #include <sys/types.h>
        !            10: #include <sys/socket.h>
        !            11: #include <netinet/in.h>
        !            12: #include <netdb.h>
        !            13: 
        !            14: main(argc, argv)
        !            15: char **argv;
        !            16: {
        !            17:        int s, len, ret;
        !            18:         struct sockaddr_in addr;        /* socket address */
        !            19:         int addrlen = sizeof(struct sockaddr_in);
        !            20:        struct hostent *hp;
        !            21:        char *peer;
        !            22:        char buf[BUFSIZ];
        !            23:        char *msg = "This is a test message!";
        !            24: 
        !            25:        if (argc != 2) {
        !            26:                fprintf(stderr, "usage: %s hostname\n", argv[0]);
        !            27:                exit(1);
        !            28:        }
        !            29:        peer = argv[1];
        !            30: 
        !            31:        if ((hp = gethostbyname(peer)) == NULL) {
        !            32:                fprintf(stderr, "Can't find host %s\n", peer);
        !            33:                exit(1);
        !            34:        }
        !            35: 
        !            36:         if ((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
        !            37:                 xxit("socket");
        !            38:        addr.sin_family = AF_INET;
        !            39:        addr.sin_port = htons(TCP_PORT);
        !            40:        addr.sin_addr = *(struct in_addr *)hp->h_addr;
        !            41:        fprintf(stderr, "Connecting to %s (%x)\n", peer, 
        !            42:                        addr.sin_addr.s_addr);
        !            43:        if (connect(s, &addr, addrlen) < 0)
        !            44:                xxit("connect");
        !            45: 
        !            46:        strcpy(buf, msg);
        !            47:        len = strlen(buf);
        !            48: 
        !            49:        ret = write(s, buf, len);
        !            50:        fprintf(stderr, " write ret %d (len %d)\n", ret, len);
        !            51:        ret = read(s, buf, BUFSIZ);
        !            52:        fprintf(stderr, " read ret %d\n", ret);
        !            53: 
        !            54:        if (ret != len || *buf != *msg + 1 || strcmp(buf+1, msg+1))
        !            55:                fprintf(stderr, "Message error: sent '%s' recv '%s'\n",
        !            56:                        msg, buf);
        !            57:        else
        !            58:                fprintf(stderr, "tcp ping to %s ok\n", peer);
        !            59: }
        !            60: 
        !            61: xxit(s)
        !            62: char *s;
        !            63: {
        !            64:        perror(s);
        !            65:        exit(1);
        !            66: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.