gpt4 book ai didi

c++ - 设置套接字超时?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:11:39 25 4
gpt4 key购买 nike

使用套接字,我不确定如何设置超时?

谢谢

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);

最佳答案

您需要使用 setsockopt设置 SO_SNDTIMEO 和/或 SO_RCVTIMEO 选项。

关于c++ - 设置套接字超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9847441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com