66 sock = socket(AF_INET, SOCK_STREAM, 0);
71 struct sockaddr_in s_in;
72 memset(&s_in, 0,
sizeof(s_in));
73 s_in.sin_family = AF_INET;
74 s_in.sin_addr.s_addr = htonl(INADDR_ANY);
75 s_in.sin_port = htons(0);
76 if (::bind(sock, (
const struct sockaddr *) &s_in,
81 if (listen(sock, 10) != 0) {
85 socklen_t
len =
sizeof(s_in);
86 if (getsockname(sock, (
struct sockaddr *) &s_in, &
len) != 0) {
90 *port = ntohs(s_in.sin_port);
96 struct sockaddr_in s_in;
97 memset(&s_in, 0,
sizeof(s_in));
98 s_in.sin_family = AF_INET;
99 s_in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
100 s_in.sin_port = htons(local_port);
101 fd_t fd = socket(AF_INET, SOCK_STREAM, 0);
109 socklen_t old_timeout_len =
sizeof old_timeout;
110 if (getsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
reinterpret_cast<sockopt_t>(&old_timeout), &old_timeout_len) != 0) {
113 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
reinterpret_cast<sockopt_t>(&timeout),
sizeof timeout) != 0) {
116 ret = connect(fd, (
struct sockaddr *) &s_in,
123 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO,
reinterpret_cast<sockopt_t>(&old_timeout), old_timeout_len) != 0) {
162 int err = WSAStartup(MAKEWORD(2, 2), &wsaData);
164 fprintf(stderr,
"WSAStartup failed with error %d.", err);
167 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
168 fprintf(stderr,
"Counld not found usable version of Winsock.\n");
172 err = WSAStartup(MAKEWORD(2, 2), &wsaData);
174 fprintf(stderr,
"WSAStartup failed with error %d.", err);
178 if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
179 fprintf(stderr,
"Counld not found usable version of Winsock.\n");
189 perror(
"open_socket");
195 socklen_t old_timeout_len =
sizeof old_timeout;
196 if (getsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<sockopt_t>(&old_timeout), &old_timeout_len) != 0) {
199 if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<sockopt_t>(&timeout),
sizeof timeout) != 0) {
205 p[0] = accept(sock, NULL, NULL);
207 perror(
"pipe accept");
213 if (setsockopt(p[0], SOL_SOCKET, SO_RCVTIMEO,
reinterpret_cast<sockopt_t>(&old_timeout), old_timeout_len) != 0) {