Socket.h
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*******************************************************************/
/* */
/* File: Socket.h */
/* Author: Helmut Schmid */
/* Purpose: */
/* Created: Fri Aug 15 14:19:19 2008 */
/* Modified: Fri Aug 15 14:33:41 2008 (schmid) */
/* */
/*******************************************************************/
/***************** class Socket **********************************/
class Socket {
int portno; /* port address */
int sockfd;
struct sockaddr_in serv_addr;
struct sockaddr cli_addr;
socklen_t clilen;
public:
int next_client() {
return accept( sockfd, (struct sockaddr *)&serv_addr, &clilen);
}
Socket( int port=7070 ): portno( port ) {
/* create a socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
fprintf(stderr, "ERROR opening socket\n");
exit(1);
}
/* initialise serv_addr with zeros */
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
/* convert portno to network byte order and store it in serv_addr.sin_port */
serv_addr.sin_port = htons(portno);
/* set the host IP address (available in INADDR_ANY) */
serv_addr.sin_addr.s_addr = INADDR_ANY;
/* bind the socket to a host and port */
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
fprintf(stderr, "ERROR on binding\n");
exit(1);
}
/* Listen to the socket; up to 5 connections at a time */
listen(sockfd, 5);
clilen = sizeof(cli_addr);
}
};