MCPcopy Create free account
hub / github.com/Tencent/libco / CreateTcpSocket

Function CreateTcpSocket

example_poll.cpp:84–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

82}
83
84static int CreateTcpSocket(const unsigned short shPort = 0 ,const char *pszIP = "*" ,bool bReuse = false )
85{
86 int fd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
87 if( fd >= 0 )
88 {
89 if(shPort != 0)
90 {
91 if(bReuse)
92 {
93 int nReuseAddr = 1;
94 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&nReuseAddr,sizeof(nReuseAddr));
95 }
96 struct sockaddr_in addr ;
97 SetAddr(pszIP,shPort,addr);
98 int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr));
99 if( ret != 0)
100 {
101 close(fd);
102 return -1;
103 }
104 }
105 }
106 return fd;
107}
108
109static void *poll_routine( void *arg )
110{

Callers 1

poll_routineFunction · 0.70

Calls 4

socketFunction · 0.85
setsockoptFunction · 0.85
closeFunction · 0.85
SetAddrFunction · 0.70

Tested by

no test coverage detected