-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateTCPServerSocket.h
34 lines (24 loc) · 1.24 KB
/
CreateTCPServerSocket.h
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
#include <sys/socket.h> /* for socket(), bind() and connect() */
#include <arpa/inet.h> /* for sockadddr_in and inet_ntoa */
#include <string.h> /* for memset() */
#define MAXPENDING 5 /* Maximum outstanding connection requests */
void DieWithError(char *errorMessage); /* Error handling function */
int CreateTCPServerSocket(unsigned short port){
int sock; /* Socket to create */
struct sockaddr_in servAddr ; /* Local address */
/* Create socket for incoming connections */
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
DieWithError("socket() failed");
/* Construct local address structure */
memset(&servAddr, 0 ,sizeof(servAddr)); /* Zero out structure */
servAddr.sin_family = AF_INET; /* Internet address family */
servAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
servAddr.sin_port = htons(port); /* Given port */
/* Bind to the local address */
if(bind(sock, (struct sockaddr *)&servAddr, sizeof(servAddr)) < 0)
DieWithError("bind() failed");
/* Mark the socket so it will listen for incoming connections */
if(listen(sock, MAXPENDING) < 0)
DieWithError("listen() failed");
return sock;
}