annotate tests/socket-client.h @ 40057:b06060465f09

maint: Run 'make update-copyright'
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 01 Jan 2019 00:25:11 +0100
parents 10eb9086bea0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14590
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Create sockets for use in tests (client side).
40057
b06060465f09 maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents: 19484
diff changeset
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
14590
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
19190
9759915b2aca all: prefer https: URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 18626
diff changeset
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
14590
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2011. */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <stdio.h>
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #include <sys/socket.h>
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <netinet/in.h>
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <arpa/inet.h>
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 /* Creates a client socket, by connecting to a server on the given port. */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 static int
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 create_client_socket (int port)
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 {
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 int client_socket;
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 /* Create a client socket. */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 client_socket = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 ASSERT (client_socket >= 0);
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 /* Connect to the server process at the specified port. */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 struct sockaddr_in addr;
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 memset (&addr, 0, sizeof (addr)); /* needed on AIX and OSF/1 */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 addr.sin_family = AF_INET;
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #if 0 /* Unoptimized */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 inet_pton (AF_INET, "127.0.0.1", &addr.sin_addr);
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 #elif 0 /* Nearly optimized */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 addr.sin_addr.s_addr = htonl (0x7F000001); /* 127.0.0.1 */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 #else /* Fully optimized */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 {
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 unsigned char dotted[4] = { 127, 0, 0, 1 }; /* 127.0.0.1 */
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 memcpy (&addr.sin_addr.s_addr, dotted, 4);
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 }
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 #endif
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 addr.sin_port = htons (port);
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 ASSERT (connect (client_socket,
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 (const struct sockaddr *) &addr, sizeof (addr))
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 == 0);
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 }
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 return client_socket;
e0d0a4052520 nonblocking: Add tests for sockets.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 }