我正在尝试使用PIC24F构建服务器。
这是我要写的一段代码:
switch(TCPServerState) {
case SM_HOME:
// Allocate a socket for this server to listen and accept connections on
socket.Socket = TCPOpen(0, TCP_OPEN_SERVER, SERVER_PORT, TCP_PURPOSE_GENERIC_TCP_SERVER);
if(socket.Socket != INVALID_SOCKET) {
TCPServerState = SM_LISTENING;
}
break;
case SM_LISTENING:
// See if anyone is connected to us
//if(TCPIsConnected(socket.Socket)) {
if(!TCPWasReset(socket.Socket)){
if(socket.Connected == 0) {
socket.Connected = 1;
printf("Socket is CONNECTED: %d\n", socket.Socket);
}
uint16_t avaible = TCPIsGetReady(socket.Socket);
// Some stuff
}
else if(socket.Connected == 1){
printf("Socket RESET: %d\n", socket.Socket);
TCPServerState = SM_CLOSING;
}
break;
case SM_CLOSING:
// Close the socket connection.
socket.Connected = 0;
TCPClose(socket.Socket);
TCPServerState = SM_HOME;
printf("Socket is CLOSED: %d\n", socket.Socket);
break;
}
如果我正确关闭我的客户端套接字,所有工作正常,但如果我断开以太网电缆我无法检测到断开连接并且我的代码没有关闭套接字,因为TCPWasReset仍然为FALSE(或TCPIsConnected仍然为TRUE)。
那么我怎样才能检测到网线断开(没有添加软件keep_alive实现)?
谢谢
答案 0 :(得分:1)
检查几项:
为您的应用选择正确的TIMER1时钟源 - 内部时钟或外部时钟(T1CON.TCS)
否则,只需在TCP.C中的keepalive逻辑上使用调试器,在最新的Microchip Library for Applications TCP / IP堆栈5.42.08中默认为10秒。