1 /* Copyright (c) 2003 Tungsten Graphics, Inc.
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files ("the
5 * Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions: The above copyright notice, the Tungsten
10 * Graphics splash screen, and this permission notice shall be included
11 * in all copies or substantial portions of the Software. THE SOFTWARE
12 * IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
15 * SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
18 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <netinet/tcp.h>
33 #include <arpa/inet.h>
36 #include <sys/socket.h>
39 #if defined(IRIX) || defined(irix)
40 typedef int socklen_t
;
45 #define DEFAULT_MASTER_PORT 7011
49 * Return my hostname in <nameOut>.
50 * Return 1 for success, 0 for error.
53 MyHostName(char *nameOut
, int maxNameLength
)
55 int k
= gethostname(nameOut
, maxNameLength
);
61 * Create a socket attached to a port. Later, we can call AcceptConnection
62 * on the socket returned from this function.
63 * Return the new socket number or -1 if error.
69 struct sockaddr_in servaddr
;
76 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
79 /* get my host name */
80 k
= gethostname(hostname
, 1000);
83 /* get hostent info */
84 hp
= gethostbyname(hostname
);
87 /* initialize the servaddr struct */
88 memset(&servaddr
, 0, sizeof(servaddr
) );
89 servaddr
.sin_family
= AF_INET
;
90 servaddr
.sin_port
= htons((unsigned short) (*port
));
91 memcpy((char *) &servaddr
.sin_addr
, hp
->h_addr
,
92 sizeof(servaddr
.sin_addr
));
94 /* deallocate when we exit */
95 k
= setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
,
96 (char *) &so_reuseaddr
, sizeof(so_reuseaddr
));
99 /* send packets immediately */
101 k
= setsockopt(sock
, IPPROTO_TCP
, TCP_NODELAY
,
102 (char *) &tcp_nodelay
, sizeof(tcp_nodelay
));
107 *port
= DEFAULT_MASTER_PORT
;
110 while (k
&& (*port
< 65534)) {
111 /* bind our address to the socket */
112 servaddr
.sin_port
= htons((unsigned short) (*port
));
113 k
= bind(sock
, (struct sockaddr
*) &servaddr
, sizeof(servaddr
));
119 printf("###### Real Port: %d\n", *port
);
122 /* listen for connections */
123 k
= listen(sock
, 100);
131 * Accept a connection on the named socket.
132 * Return a new socket for the new connection, or -1 if error.
135 AcceptConnection(int socket
)
137 struct sockaddr addr
;
141 addrLen
= sizeof(addr
);
142 newSock
= accept(socket
, &addr
, &addrLen
);
151 * Contact the server running on the given host on the named port.
152 * Return socket number or -1 if error.
155 Connect(const char *hostname
, int port
)
157 struct sockaddr_in servaddr
;
164 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
167 hp
= gethostbyname(hostname
);
170 memset(&servaddr
, 0, sizeof(servaddr
));
171 servaddr
.sin_family
= AF_INET
;
172 servaddr
.sin_port
= htons((unsigned short) port
);
173 memcpy((char *) &servaddr
.sin_addr
, hp
->h_addr
, sizeof(servaddr
.sin_addr
));
175 k
= connect(sock
, (struct sockaddr
*) &servaddr
, sizeof(servaddr
));
182 /* send packets immediately */
183 k
= setsockopt(sock
, IPPROTO_TCP
, TCP_NODELAY
,
184 (char *) &tcp_nodelay
, sizeof(tcp_nodelay
));
193 CloseSocket(int socket
)
200 SendData(int socket
, const void *data
, int bytes
)
205 while (sent
< bytes
) {
206 b
= write(socket
, (char *) data
+ sent
, bytes
- sent
);
208 return -1; /* something broke */
216 ReceiveData(int socket
, void *data
, int bytes
)
220 while (received
< bytes
) {
221 b
= read(socket
, (char *) data
+ received
, bytes
- received
);
231 SendString(int socket
, const char *str
)
233 const int len
= strlen(str
);
236 /* first, send a 4-byte length indicator */
237 b
= write(socket
, &len
, sizeof(len
));
241 sent
= SendData(socket
, str
, len
);
248 ReceiveString(int socket
, char *str
, int maxLen
)
250 int len
, received
, b
;
252 /* first, read 4 bytes to see how long of string to receive */
253 b
= read(socket
, &len
, sizeof(len
));
257 assert(len
<= maxLen
); /* XXX fix someday */
259 received
= ReceiveData(socket
, str
, len
);
260 assert(received
!= -1);
261 assert(received
== len
);