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);
78 int Network_server(char * addr, int port)
80 return Network_server_listen(Network_server_bind(port, &port), addr);
83 int Network_client(const char * addr, int port, int timeout)
85 int sfd = socket(PF_INET, SOCK_STREAM, 0);
87 //log_print(2, "Network_client", "Created socket");
88 long arg = fcntl(sfd, F_GETFL, NULL);
90 fcntl(sfd, F_SETFL, arg);
94 error("Network_client", "Creating socket for address %s:%d : %s", addr, port, strerror(errno));
96 struct sockaddr_in server;
100 server.sin_family = AF_INET;
101 hp = gethostbyname(addr);
102 bcopy ( hp->h_addr, &(server.sin_addr.s_addr), hp->h_length);
103 server.sin_port = htons(port);
106 int res = connect(sfd, (struct sockaddr *) &server, sizeof(server));
109 if (res < 0 && errno == EINPROGRESS)
114 FD_SET(sfd, &writeSet);
121 tp = (timeout < 0) ? NULL : &tv;
123 int err = select(sfd+1, NULL, &writeSet, NULL, tp);
127 error("Network_client", "Timed out trying to connect to %s:%d after %d seconds", addr, port, timeout);
131 error("Network_client", "Connecting to %s:%d - Error in select(2) call : %s", addr, port, strerror(errno));
133 else if (FD_ISSET(sfd, &writeSet))
136 socklen_t len = sizeof so_error;
137 getsockopt(sfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
140 error("Network_client", "Connecting to %s:%d : %s", addr, port, strerror(so_error));
145 error("Network_client", "select(2) returned %d but the socket is not writable!?", err);
150 error("Network_client", "Connecting to %s:%d : %s", addr, port, strerror(errno));
153 arg = fcntl(sfd, F_GETFL, NULL);
154 arg &= (~O_NONBLOCK);
155 fcntl(sfd, F_SETFL, arg);
162 void Network_close(int sfd)
164 if (shutdown(sfd, 2) != 0)
166 error("Network_close", "Closing socket : %s", strerror(errno));