3 #include <sys/socket.h>
4 #include <netinet/in.h>
16 struct sockaddr_in baddr
;
17 struct sockaddr_in addr
;
18 socklen_t baddrsize
= sizeof(baddr
);
21 s
= DO( socket(PF_INET
, SOCK_STREAM
, 0) );
23 (void) DO( setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(int)) );
24 memset(&addr
, 0, sizeof(addr
));
25 addr
.sin_family
= AF_INET
;
26 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
27 addr
.sin_port
= htons(12321);
29 (void) DO( bind(s
, (struct sockaddr
*)&addr
, sizeof(addr
)) );
31 (void) DO( listen(s
, 5) );
33 memset(&baddr
, 0, sizeof(baddr
));
34 x
= DO( accept(s
, (struct sockaddr
*)&baddr
, &baddrsize
) );
36 (void) DO( write(x
, "hello", 6) );
41 int s
, count
= 0, ret
;
42 struct sockaddr_in addr
;
45 addr
.sin_family
= AF_INET
;
46 addr
.sin_addr
.s_addr
= inet_addr("127.0.0.1");
47 addr
.sin_port
= htons(12321);
51 s
= DO( socket(PF_INET
, SOCK_STREAM
, 0) );
52 ret
= connect(s
, (struct sockaddr
*)&addr
, sizeof(addr
));
54 // If the connect() failed, we close the socket and reopen it before
55 // trying again. This isn't necessary on Linux, but it is on Darwin.
56 (void) DO( close(s
) );
59 } while (count
< 10 && ret
== -1);
66 (void) DO( read(s
, buf
, sizeof(buf
)) );
72 int main (int argc
, char **argv
)
78 if ((pid
= fork()) == 0) {