LINUX C 进行TCP网络连接,怎样设置连接超时时间

LINUX C 进行TCP网络连接,怎样设置连接超时时间,第1张

如果你确定,真的不需要等这么久,或者用户希望可以随时中上连接过程,那么一般是用 非阻塞模式来做的. 看看我的这段连接代码(节选),可以作为TCP连接的典范:

bool CRemoteLink::Connect()

{

OnDisconnected() // 如果已经连接,则断开

if(!m_bUseProxy)

{

m_iConnStatus = SS_CONNECTING // 正在连接状态

GNTRACE ("开始连接到远程服务器[%s][%ld]...\n", m_strip.c_str(), m_port)

// 建立套接字, 准备连接到服务器

m_socket = ::socket(AF_INET, SOCK_STREAM, 0)

if (socket <0) {

if(m_pCallBack)

m_pCallBack->OnSocketError(SE_CREATE, MSG_SE_CREATE)

return false

}

// 设为异步 *** 作方式

unsigned long on = 1

if (::ioctlsocket(m_socket, FIONBIO, &on) <0) {

::closesocket(m_socket)

if(m_pCallBack)

m_pCallBack->OnSocketError(SE_CREATE, MSG_SE_CREATE)

return false

}

sockaddr_in addr

memset(&addr, 0, sizeof(addr))

addr.sin_family = AF_INET

addr.sin_addr.s_addr = inet_addr(m_strip.c_str())

addr.sin_port = htons(m_port)

int rt

rt = ::connect(m_socket, (sockaddr *) &addr, sizeof(addr))

if (rt == 0) {

OnConnected()

return true

}

// ==================================================================

timeval to

// 首先建立连接

fd_set wfds

fd_set efds

FD_ZERO(&wfds)

FD_ZERO(&efds)

// test shutdown event each 100ms.

to.tv_sec = 0

// CONNECT_TIMEOUT

to.tv_usec = 100000

int it = 0

while(!m_meShutdown.Wait(0) &&!m_meConnStop.Wait(0))

{

FD_SET(m_socket, &wfds)

FD_SET(m_socket, &efds)

int n = select(m_socket + 1, NULL, &wfds, &efds, &to)

if (n >0) {

if(FD_ISSET(m_socket, &wfds))

{

OnConnected()

return true

}

else

{

//int err = ::WSAGetLastError()

//const char* msg = GetLastErrorMessage(err)

GNTRACE ("CRemoteLink::Connect : connection attempt failed!\n")

if(m_pCallBack)

m_pCallBack->OnSocketError(SE_CONN, MSG_SE_CONN)

break

}

} else if (n <0) { // Select Error

int err = ::WSAGetLastError()

const char* msg = GetLastErrorMessage(err)

GNTRACE ("CRemoteLink::Connect : Select Error.[%d] - %s\n", err, msg)

if(m_pCallBack)

m_pCallBack->OnSocketError(err, msg)

break

}

else

{

it += 100

if(it >30000) // 连接超时 -- (30S)

{

GNTRACE ("CRemoteLink::Connect : Time out.\n")

if(m_pCallBack)

m_pCallBack->OnSocketError(SE_TIMEOUT, MSG_SE_TIMEOUT)

break

}

}

}

if(m_meConnStop.Wait(0))

{

GNTRACE("连接过程进行时被取消。\n")

}

}

else

{

// 通过代理服务器连接

给你一段我写的client代码

C/C++ code

/*客户端*/ #include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/socket.h>#include<sys/types.h>#include<netdb.h>#include<netinet/in.h>#include<unistd.h>#include<sys/time.h>#include <errno.h>#define SERVERPORT 5556 int main(int argc,char *argv[]) { int sockfd,connectfd=0,recvfd,sendfd struct sockaddr_in serv_addrstruct hostent *hostFILE *fpchar ch[1024]int byte_send=0//int a=MSG_DONTWAIT//int opt=MSG_DONTWAITfd_set rfdsstruct timeval tvint retval, maxfdif(argc!=3) { printf("format: %s IP file_name\n",argv[0])return 0} if((host=(gethostbyname(argv[1])))==NULL) { printf("error in gethostby name. \n")return 0} if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1) { printf("error in socket create.\n")return 0} printf("创建socket成功,sockfd=%d\n",sockfd)//printf("运行至此1\n")serv_addr.sin_family=AF_INETserv_addr.sin_port =htons(SERVERPORT)serv_addr.sin_addr=*((struct in_addr*)host->h_addr)bzero(&(serv_addr.sin_zero),sizeof(serv_addr.sin_zero))//printf("运行至此2\n")while(1) { if((connectfd=connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(struct sockaddr )))==-1) { printf("error in connect.\n")printf("连接失败:connectfd=%d\n",connectfd)sleep(5)continue} printf("连接成功:connectfd=%d\n",connectfd)break} //printf("运行至此3\n")fp=fopen(*(argv+2),"rb")memset(ch,0,sizeof(ch))while(1) { memset(ch,0,sizeof(ch))sendfd=fread(ch,sizeof(char),1024,fp)if(sendfd<0) breaksleep(1)//printf("sendfd=%d\n",sendfd)byte_send=send(sockfd,ch,sendfd,0)//printf("byte_send=%d,%s\n",byte_send,strerror(errno))//memset(&ch,0,sizeof(ch))//printf("发送内容:%s\n",ch)sleep(3)while(1) { //void FD_ZERO (fd_set *fdset)// 初始化文件描述集合 //void FD_SET (int fd, fd_set *fdset)// 将描述符加入到集合中 //void FD_CLR (int fd, fd_set *fdset)// 将描述符中集合中删除 //int FD_ISSET (int fd, fd_set *fdset)// 检查描述符集合中指定的文件描述符是否可读写 /*把可读文件描述符的集合清空*/ FD_ZERO(&rfds)/*把标准输入的文件描述符加入到集合中*/ FD_SET(0, &rfds)maxfd = 0/*把当前连接的文件描述符加入到集合中*/ FD_SET(sockfd, &rfds)/*找出文件描述符集合中最大的文件描述符*/ if(maxfd <sockfd) maxfd = sockfd/*设置超时时间*/ tv.tv_sec = 5tv.tv_usec = 0/*等待聊天*/ retval = select(maxfd+1, &rfds, NULL, NULL, &tv)if(retval == -1) { printf("select出错,客户端程序退出\n")break} else if(retval == 0) { //printf("客户端没有任何输入信息,并且服务器也没有信息到来,waiting...\n")//continueprintf("客户端输出信息完毕,等待下一次输出\n")break} else { /*服务器发来了消息*/ if(FD_ISSET(sockfd,&rfds))// 检查描述符集合中指定的文件描述符是否可读写 { /******接收消息*******/ memset(ch,0,sizeof(ch))recvfd=recv(sockfd,ch,sizeof(ch),0)if(recvfd==-1) { //printf("运行至此7\n")printf("error in recv.\n")continue} //printf("recvfd=%d\n",recvfd)printf("%s\n",ch)} } } } printf("传送结束!\n")fclose(fp)close(connectfd)return 0}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8191916.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存