4 #include <sys/select.h>
7 #define h_addr h_addr_list[0]
13 int Network_get_port(int sfd)
15 static struct sockaddr_in sin;
16 static socklen_t len = sizeof(struct sockaddr_in);
18 if (getsockname(sfd, (struct sockaddr *)&sin, &len) != 0)
19 error("Network_port", "getsockname : %s", strerror(errno));
20 return ntohs(sin.sin_port);
23 int Network_server_bind(int port, int * bound)
25 int sfd = socket(PF_INET, SOCK_STREAM, 0);
28 error("Network_server", "Creating socket on port %d : %s", port, strerror(errno));
31 struct sockaddr_in name;
33 name.sin_family = AF_INET;
34 name.sin_addr.s_addr = htonl(INADDR_ANY);
35 name.sin_port = htons(port);
37 if (bind( sfd, (struct sockaddr *) &name, sizeof(name) ) < 0)
39 error("Network_server", "Binding socket on port %d : %s", port, strerror(errno));
43 *bound = Network_get_port(sfd);
47 int Network_server_listen(int sfd, char * addr)
49 int port = Network_get_port(sfd);
50 if (listen(sfd, 1) < 0)
52 error("Network_server", "Listening on port %d : %s", port, strerror(errno));
57 psd = accept(sfd, 0, 0);
60 struct sockaddr_in client;
63 client.sin_family = AF_INET;
64 hp = gethostbyname(addr);
65 bcopy ( hp->h_addr, &(client.sin_addr.s_addr), hp->h_length);
66 client.sin_port = htons(port);
67 socklen_t len = sizeof(client);
69 psd = accept(sfd, (struct sockaddr*)&client, &len);
71 //close(sfd); // don't close the bind socket here; we might want to reuse the port
76 int Network_server(char * addr, int port)
78 int bind = Network_server_bind(port, &port);
79 int sfd = Network_server_listen(bind, addr);
80 close(bind); // won't be able to reuse the port (it goes into TIME_WAIT)
84 int Network_client(const char * addr, int port, int timeout)
86 int sfd = socket(PF_INET, SOCK_STREAM, 0);
88 //log_print(2, "Network_client", "Created socket");
89 long arg = fcntl(sfd, F_GETFL, NULL);
91 fcntl(sfd, F_SETFL, arg);
95 error("Network_client", "Creating socket for address %s:%d : %s", addr, port, strerror(errno));
97 struct sockaddr_in server;
101 server.sin_family = AF_INET;
102 hp = gethostbyname(addr);
105 error("Network_client", "Can't get host by name %s", addr);
107 bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
108 server.sin_port = htons(port);
111 int res = connect(sfd, (struct sockaddr *) &server, sizeof(server));
114 if (res < 0 && errno == EINPROGRESS)
119 FD_SET(sfd, &writeSet);
126 tp = (timeout < 0) ? NULL : &tv;
128 int err = select(sfd+1, NULL, &writeSet, NULL, tp);
132 error("Network_client", "Timed out trying to connect to %s:%d after %d seconds", addr, port, timeout);
136 error("Network_client", "Connecting to %s:%d - Error in select(2) call : %s", addr, port, strerror(errno));
138 else if (FD_ISSET(sfd, &writeSet))
141 socklen_t len = sizeof so_error;
142 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
145 error("Network_client", "Connecting to %s:%d : %s", addr, port, strerror(so_error));
150 error("Network_client", "select(2) returned %d but the socket is not writable!?", err);
155 error("Network_client", "Connecting to %s:%d : %s", addr, port, strerror(errno));
158 arg = fcntl(sfd, F_GETFL, NULL);
159 arg &= (~O_NONBLOCK);
160 fcntl(sfd, F_SETFL, arg);
167 void Network_close(int sfd)
169 if (shutdown(sfd, 2) != 0)
171 error("Network_close", "Closing socket : %s", strerror(errno));