CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

TCP/IP stack (ex13b.c) after break connection

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Requan



Joined: 11 May 2008
Posts: 74

View user's profile Send private message

TCP/IP stack (ex13b.c) after break connection
PostPosted: Mon Oct 29, 2012 8:39 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group