【发布时间】:2011-07-10 22:12:34
【问题描述】:
我有一个小型客户端-服务器程序,我想在最大超时 10 秒后关闭与客户端的连接。我有一个警报,关闭连接的函数和警报处理程序如下所示:
void closeClient() {
int nr = close(conn);
if (nr == 0)
printf("Client connection closed.\n");
else {
printf("Error while closing client connection. Error code: %d\n",errno);
exit(1);
}
exit(0); // Process ends after serving the client
}
void time_out(int signal) {
printf("Time out.\n");
char* msg = "Time out.Connection to server is closed\n\0";
send(conn, msg, strlen(msg),0);
closeClient(conn);
exit(1);
}
问题在于客户端仅在尝试向服务器发送某些内容时(在连接已关闭之后)才打印消息(“超时。与服务器的连接已关闭”)。我不知道为什么。有什么建议吗?
【问题讨论】:
-
你永远不会显示调用time_out函数的代码。
-
是这样的:
codesignal(SIGALRM, time_out);警报(10); cod = recv(conn, &buf, sizeof(buf), 0); boolean isNumber = validateNumber(buf); printf("收到的字符串:%s\n", buf);警报(0); // 停止计时器 if(!isNumber) { //服务器没有收到一个有效的整数 errorMsg = "请输入一个有效的整数!\n\0"; printf("发送错误信息..\n");发送(连接,错误消息,strlen(错误消息)+1,0); } 其他..code