Requan
Joined: 11 May 2008 Posts: 74
|
TCP/IP stack (ex13b.c) after break connection |
Posted: Mon Oct 29, 2012 8:39 am |
|
|
Hi,
I used ex13b in PIC18F67j60.
It works great but is one problem: when i try connect to PIC when network cable is unplugged, and next i plug cable etc after several times i can't connect any more. Ping's are accept by PIC but i can't connect, only reset PIC helps or when timeout force disconnection state.
This situation is happen in live, when connection is lost by switch and PC software try to connect to PIC.
What is better for tcp\ip?:
1) connect, send commands and wait for to send next commands without disconnect?
2) connect, send command, disconnect etc?
Code: |
void MyTCPTask() {
static TICKTYPE lastTick[NUM_LISTEN_SOCKETS];
static TCP_SOCKET socket[NUM_LISTEN_SOCKETS]={INVALID_SOCKET,INVALID_SOCKET};
static enum {
MYTCP_STATE_NEW=0, MYTCP_STATE_LISTENING=1,
MYTCP_STATE_CONNECTED=2, MYTCP_STATE_DISCONNECT=3,
MYTCP_STATE_FORCE_DISCONNECT=4
} state[NUM_LISTEN_SOCKETS]={0};
static NODE_INFO remote[NUM_LISTEN_SOCKETS];
TICKTYPE currTick;
int8 dis;
int8 i;
currTick=TickGet();
for (i=0;i<NUM_LISTEN_SOCKETS;i++) {
switch (state[i])
{
case MYTCP_STATE_NEW:
if (socket[i] == INVALID_SOCKET)
socket[i]=TCPListen(TCP_PORT);
if (socket[i]!=INVALID_SOCKET) {
state[i]=MYTCP_STATE_LISTENING;
//printf(lcd_putc, "\fLISTENING ON\n");
//lcd_display_ip(&MY_IP);
}
else
{
printf(lcd_putc,"\fSOCKET ERROR");
}
StatusConnected = FALSE;
break;
case MYTCP_STATE_LISTENING:
if (TCPIsConnected(socket[i]))
{
state[i]=MYTCP_STATE_CONNECTED;
// printf(lcd_putc, "\fCONNECTED!");
lastTick[i]=currTick;
StatusConnected = TRUE;
output_high(LedET);
}
break;
case MYTCP_STATE_CONNECTED:
if (TCPIsConnected(socket[i]))
{
if (TickGetDiff(currTick,lastTick[i]) > ((int16)TICKS_PER_SECOND * 10))
{
state[i]=MYTCP_STATE_DISCONNECT;
printf(lcd_putc, "\fTIMEOUT\nDISCONNECTING");
lastTick[i]=currTick;
StatusConnected = FALSE;
}
else
{
dis=TCPConnectedTask(socket[i],i);
if (dis)
{
// printf(lcd_putc, "\fDISCONNECTING");
state[i]=MYTCP_STATE_DISCONNECT;
lastTick[i]=currTick;
StatusConnected = FALSE;
output_low(LedET);
}
}
}
else {
//printf(lcd_putc, "\fDISCONNECTED");
state[i]=MYTCP_STATE_FORCE_DISCONNECT;
StatusConnected = FALSE;
output_low(LedET);
}
break;
case MYTCP_STATE_DISCONNECT:
if (TCPIsPutReady(socket[i])) {
state[i]=MYTCP_STATE_FORCE_DISCONNECT;
}
else if (TickGetDiff(currTick, lastTick[i]) > (TICKS_PER_SECOND * 10)) {
state[i]=MYTCP_STATE_FORCE_DISCONNECT;
}
break;
case MYTCP_STATE_FORCE_DISCONNECT:
TCPDisconnect(socket[i]);
state[i]=MYTCP_STATE_NEW;
break;
}
}
}
|
Best Regards,
Martin |
|