16,467
社区成员
发帖
与我相关
我的任务
分享
SOCKET g_socket;
///
CSocket m_clientSocket;
///
g_socket = m_clientSocket.Detach();
///其它线程
CSocket socket;
socket.Attach(g_socket);
////
int TCP_Connect(const char *serverIP, int port, int iTimeOut)
{
struct timeval tv;
int error;
fd_set writefds;
struct sockaddr_in servaddr;
int ret;
u_long ul = 1;//设置非阻塞方式连接
BOOL bNodelay = TRUE;
//1.socket
int fd = socket(PF_INET,SOCK_STREAM, 0);
if(fd <=0 )
{
LogError("create socket failed.%s", strerror(errno));
fd = 0;
return -1;
}
//2.fcntl
#ifdef LINUX_PLATFORM
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
#else
if(ioctlsocket(fd, FIONBIO, (unsigned long*)&ul) != 0)
{
LogError("ioctlsocket socket failed:%ld", GetLastError());
}
#endif
//3.connect
//bzero(&servaddr, sizeof(servaddr));
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
#ifdef LINUX_PLATFORM
inet_aton((const char*)serverIP, &servaddr.sin_addr);
#else
servaddr.sin_addr.S_un.S_addr = inet_addr(serverIP);
#endif
servaddr.sin_port = htons(port);
if(iTimeOut <= 0)
{
iTimeOut = 5;
}
if(connect(fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
{ //if(connect failed)
#ifdef LINUX_PLATFORM
if (errno == EINPROGRESS)
#else
ret = GetLastError();
if(ret == WSAEINPROGRESS || ret == WSAEWOULDBLOCK)
#endif
{// if it is in the connect process
while(1)
{
tv.tv_sec = iTimeOut;
tv.tv_usec = 0;
FD_ZERO(&writefds);
FD_SET((u_long)fd, &writefds);
if( select(fd + 1, NULL, &writefds, NULL, &tv) > 0 )
{
int len = sizeof(int);
//skip the firewall setting.
getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&error, &len);
if(error == 0)
ret = 0;
else
ret = -1;
break;
}
else
{
#ifdef LINUX_PLATEFORM
if(errno == EINTR || errno == EAGAIN )
continue;
#endif
LogError("connect time out.");
ret = -1;//timeout or error happen
break;//
}
}
}//end if it is in the connect process
else
{
ret = -1;
}
}//end if(connect failed)
else ret = 0;
if(setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char*)&bNodelay, sizeof(bNodelay) ) != 0)
{
LogError("setsockopt socket failed.%s", strerror(errno));
return -3;
}
if(ret < 0)
{
#ifdef LINUX_PLATFORM
close(fd);
#else
closesocket(fd);
#endif
fd = 0;
LogError("connect %s failed, errno:%d.", serverIP, errno);
return ret;
}
return fd;
}
BOOL SetTimeOut(SOCKET s, int nTime, BOOL bRecv)
{
return setsockopt(s, SOL_SOCKET, bRecv ? SO_RCVTIMEO : SO_SNDTIMEO, (char*)&nTime, sizeof(nTime));
}