]> granicus.if.org Git - apache/blob - test/test-writev.c
showstoppers--;
[apache] / test / test-writev.c
1 /*
2     test-writev: use this to figure out if your writev() does intelligent
3     things on the network.  Some writev()s when given multiple buffers
4     will break them up into multiple packets, which is a waste.
5
6     Linux prior to 2.0.31 has this problem.
7
8     Solaris 2.5, 2.5.1 doesn't appear to, 2.6 hasn't been tested.
9
10     IRIX 5.3 doesn't have this problem.
11
12     To use this you want to snoop the wire with tcpdump, and then run
13     "test-writev a.b.c.d port#" ... against some TCP service on another
14     box.  For example you can run it against port 80 on another server.
15     You want to look to see how many data packets are sent, you're hoping
16     only one of size 300 is sent.
17 */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <arpa/inet.h>
26 #include <sys/uio.h>
27 #include <errno.h>
28
29 #ifndef INADDR_NONE
30 #define INADDR_NONE (-1ul)
31 #endif
32
33 void main( int argc, char **argv )
34 {
35     struct sockaddr_in server_addr;
36     int s;
37     struct iovec vector[3];
38     char buf[100];
39     int i;
40     const int just_say_no = 1;
41
42     if( argc != 3 ) {
43 usage:
44         fprintf( stderr, "usage: test-writev a.b.c.d port#\n" );
45         exit( 1 );
46     }
47     server_addr.sin_family = AF_INET;
48     server_addr.sin_addr.s_addr = inet_addr( argv[1] );
49     if( server_addr.sin_addr.s_addr == INADDR_NONE ) {
50         fprintf( stderr, "bogus address\n" );
51         goto usage;
52     }
53     server_addr.sin_port = htons( atoi( argv[2] ) );
54
55     s = socket( AF_INET, SOCK_STREAM, 0 );
56     if( s < 0 ) {
57         perror("socket");
58         exit(1);
59     }
60     if( connect( s, (struct sockaddr *)&server_addr, sizeof( server_addr ) )
61         != 0 ) {
62         perror("connect");
63         exit(1);
64     }
65
66     if( setsockopt(s, IPPROTO_TCP, TCP_NODELAY, (char*)&just_say_no,
67         sizeof(just_say_no)) != 0 ) {
68         perror( "TCP_NODELAY" );
69         exit(1);
70     }
71     /* now build up a two part writev and write it out */
72     for( i = 0; i < sizeof( buf ); ++i ) {
73         buf[i] = 'x';
74     }
75     vector[0].iov_base = buf;
76     vector[0].iov_len = sizeof(buf);
77     vector[1].iov_base = buf;
78     vector[1].iov_len = sizeof(buf);
79     vector[2].iov_base = buf;
80     vector[2].iov_len = sizeof(buf);
81
82     i = writev( s, &vector[0], 3 );
83     fprintf( stdout, "i=%d, errno=%d\n", i, errno );
84     exit(0);
85 }