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

Function CreateTcpSocket

example_echosvr.cpp:175–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

173}
174
175static int CreateTcpSocket(const unsigned short shPort /* = 0 */,const char *pszIP /* = "*" */,bool bReuse /* = false */)
176{
177 int fd = socket(AF_INET,SOCK_STREAM, IPPROTO_TCP);
178 if( fd >= 0 )
179 {
180 if(shPort != 0)
181 {
182 if(bReuse)
183 {
184 int nReuseAddr = 1;
185 setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&nReuseAddr,sizeof(nReuseAddr));
186 }
187 struct sockaddr_in addr ;
188 SetAddr(pszIP,shPort,addr);
189 int ret = bind(fd,(struct sockaddr*)&addr,sizeof(addr));
190 if( ret != 0)
191 {
192 close(fd);
193 return -1;
194 }
195 }
196 }
197 return fd;
198}
199
200
201int main(int argc,char *argv[])

Callers 1

mainFunction · 0.70

Calls 4

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

Tested by

no test coverage detected