C++ 设置套接字超时?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9847441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting Socket Timeout?
提问by jdl
Using sockets, I am not sure how to set the timeout?
使用套接字,我不确定如何设置超时?
thanks
谢谢
int sock, connected, bytes_recieved;
char send_data [128] , recv_data[128];
SOCKADDR_IN server_addr,client_addr;
int sin_size;
int j = 0;
::socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(4000);
server_addr.sin_addr.s_addr = INADDR_ANY;
::bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
::listen(sock, 5);
::fflush(stdout);
while(1)
{
sin_size = sizeof(struct sockaddr_in);
connected = ::accept(sock, (struct sockaddr *)&client_addr, &sin_size);
while (1)
{
j++;
::send(connected, send_data, strlen(send_data), 0);
//dealing with lost communication ?
//and reastablishing communication
//set timeout and reset on timeout error
}
}
::closesocket(sock);
回答by Jon
You need to use setsockopt
to set the SO_SNDTIMEO
and/or SO_RCVTIMEO
options.
您需要使用setsockopt
来设置SO_SNDTIMEO
和/或SO_RCVTIMEO
选项。
回答by Remy Lebeau
A socket is in blocking mode by default. If you switch it to non-blocking mode using ioctlsocket(FIONBIO)
, you can use select()
to manage timeouts:
默认情况下,套接字处于阻塞模式。如果使用 将其切换到非阻塞模式ioctlsocket(FIONBIO)
,则可以用于select()
管理超时:
SOCKET sock, connected;
int bytes_recieved;
char send_data [128] , recv_data[128];
SOCKADDR_IN server_addr,client_addr;
int sin_size;
int j = 0, ret;
fd_set fd;
timeval tv;
sock = ::socket(AF_INET, SOCK_STREAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(4000);
server_addr.sin_addr.s_addr = INADDR_ANY;
::bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
::listen(sock, 1);
::fflush(stdout);
u_long nbio = 1;
::ioctlsocket(sock, FIONBIO, &nbio);
while(1)
{
FD_ZERO(&fd);
FD_SET(sock, &fd);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(0, &fd, NULL, NULL, &tv) > 0)
{
sin_size = sizeof(struct sockaddr_in);
connected = ::accept(sock, (struct sockaddr *)&client_addr, &sin_size);
nbio = 1;
::ioctlsocket(connected, FIONBIO, &nbio);
while (1)
{
j++;
if (::send(connected, send_data, strlen(send_data), 0) < 0)
{
//dealing with lost communication ?
//and reastablishing communication
//set timeout and reset on timeout error
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
FD_ZERO(&fd);
FD_SET(connected, &fd);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(0, NULL, &fd, NULL, &tv) > 0)
continue;
}
break;
}
}
closesocket(connected);
}
}
回答by The Bird
you can use:
您可以使用:
fd_set fd;
timeval tv;
FD_ZERO(&fd);
FD_SET(sock, &fd);
tv.tv_sec = time_out(second);
tv.tv_usec = 0;
to set timeout for sending,receiving data.
设置发送、接收数据的超时时间。