2 * Copyright (c) 2004-2005 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
32 #include <netinet/in.h>
34 #include <arpa/inet.h>
46 fprintf(stderr, "tcpconnect server port\n");
47 fprintf(stderr, "tcpconnect client ip port count\n");
52 tcpconnect_server(int argc, char *argv[])
54 int listen_sock, accept_sock;
55 struct sockaddr_in sin;
62 bzero(&sin, sizeof(sin));
63 sin.sin_len = sizeof(sin);
64 sin.sin_family = AF_INET;
65 sin.sin_addr.s_addr = htonl(INADDR_ANY);
67 port = strtoul(argv[0], &dummy, 10);
68 if (port < 1 || port > 65535 || *dummy != '\0')
70 sin.sin_port = htons(port);
72 listen_sock = socket(PF_INET, SOCK_STREAM, 0);
73 if (listen_sock == -1)
74 errx(-1, "socket: %s", strerror(errno));
76 if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
77 errx(-1, "bind: %s", strerror(errno));
79 if (listen(listen_sock, -1) == -1)
80 errx(-1, "listen: %s", strerror(errno));
83 accept_sock = accept(listen_sock, NULL, NULL);
89 tcpconnect_client(int argc, char *argv[])
91 struct sockaddr_in sin;
99 bzero(&sin, sizeof(sin));
100 sin.sin_len = sizeof(sin);
101 sin.sin_family = AF_INET;
102 if (inet_aton(argv[0], &sin.sin_addr) == 0)
103 errx(-1, "listen: %x", strerror(errno));
105 port = strtoul(argv[1], &dummy, 10);
106 if (port < 1 || port > 65535 || *dummy != '\0')
108 sin.sin_port = htons(port);
110 count = strtoul(argv[2], &dummy, 10);
111 if (count < 1 || count > 100000 || *dummy != '\0')
114 for (i = 0; i < count; i++) {
115 sock = socket(PF_INET, SOCK_STREAM, 0);
117 errx(-1, "socket: %s", strerror(errno));
120 if (fcntl(sock, F_SETFL, O_NONBLOCK) != 0)
121 errx(-1, "fcntl(F_SETFL): %s", strerror(errno));
123 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1
124 && errno != EINPROGRESS)
125 errx(-1, "connect: %s", strerror(errno));
127 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
128 errx(-1, "connect: %s", strerror(errno));
136 main(int argc, char *argv[])
142 if (strcmp(argv[1], "server") == 0)
143 tcpconnect_server(argc - 2, argv + 2);
144 else if (strcmp(argv[1], "client") == 0)
145 tcpconnect_client(argc - 2, argv + 2);