MCPcopy Index your code
hub / github.com/antirez/smallchat / createTCPServer

Function createTCPServer

chatlib.c:38–57  ·  view source on GitHub ↗

Create a TCP socket listening to 'port' ready to accept connections. */

Source from the content-addressed store, hash-verified

36
37/* Create a TCP socket listening to 'port' ready to accept connections. */
38int createTCPServer(int port) {
39 int s, yes = 1;
40 struct sockaddr_in sa;
41
42 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) return -1;
43 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); // Best effort.
44
45 memset(&sa,0,sizeof(sa));
46 sa.sin_family = AF_INET;
47 sa.sin_port = htons(port);
48 sa.sin_addr.s_addr = htonl(INADDR_ANY);
49
50 if (bind(s,(struct sockaddr*)&sa,sizeof(sa)) == -1 ||
51 listen(s, 511) == -1)
52 {
53 close(s);
54 return -1;
55 }
56 return s;
57}
58
59/* Create a TCP socket and connect it to the specified address.
60 * On success the socket descriptor is returned, otherwise -1.

Callers 1

initChatFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected