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 CCS Technical Support

TCP Connection not established

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



Joined: 10 Sep 2009
Posts: 22

View user's profile Send private message

TCP Connection not established
PostPosted: Wed Oct 07, 2009 2:57 pm     Reply with quote

Hi,

I am using the pic18f97j60 with the ccs tcs/ip stack. I was able to get ethernet connection to a laptop and can see data on a webpage. So I created my own function to be able to talk to a device using the tcp protocol, but TCPIsConnected(HTTPSocket) returns false and I don't see any problem. It is basically stuck in the case HTTP_ST_WAIT_CONNECT.

The below is the function code:
Code:

void mytask(void)
{
   static enum {HTTP_ST_CONNECT=0, HTTP_ST_WAIT_CONNECT=1, HTTP_ST_GET=2,
      HTTP_ST_PUT=3, HTTP_ST_DISCONNECT=4} state=0;
   static TICKTYPE timeout_counter;
   static char lc, lc2;
   char c;
   char content[250];
   int8 a0;
  #if !STACK_USE_CCS_PICENS
   int8 a1;
  #endif

   if (HTTPSocket==INVALID_SOCKET)
      state=HTTP_ST_CONNECT;
   else if (!TCPIsConnected(HTTPSocket))
      state=HTTP_ST_WAIT_CONNECT;
   else if (TickGetDiff(TickGet(), timeout_counter) > TICKS_PER_SECOND*60)
      state=HTTP_ST_DISCONNECT;

   switch(state)
   {
      case HTTP_ST_CONNECT:

         //SHOUTCAST_PORT  = 80;
        /* remoteNode.IPAddr.v[0] = 192;
            remoteNode.IPAddr.v[1] = 168;   
            remoteNode.IPAddr.v[2] = 0;   
            remoteNode.IPAddr.v[3] = 1; 
           
            remoteNode.MACAddr.v[0] = 0;
            remoteNode.MACAddr.v[1] = 128;   
            remoteNode.MACAddr.v[2] = 174;   
            remoteNode.MACAddr.v[3] = 169;
            remoteNode.MACAddr.v[4] = 236;   
            remoteNode.MACAddr.v[5] = 8;*/
            HTTPSocket = TCPConnect(&remoteNode,SHOUTCAST_PORT);
         if (HTTPSocket!=INVALID_SOCKET)
         {
            printf("\r\nHTTP: Listening");
            state=HTTP_ST_WAIT_CONNECT;
            timeout_counter=TickGet();
         }
         else
            state = HTTP_ST_CONNECT;
         break;

      case HTTP_ST_WAIT_CONNECT:
         timeout_counter=TickGet();
         if (TCPIsConnected(HTTPSocket))
         {
            state=HTTP_ST_GET;
            printf("\r\nHTTP: Connected");
            write_lcd_char("u");
         }
         else
         {
            state=HTTP_ST_WAIT_CONNECT;
            write_lcd_char("f");
         }
         break;

      case HTTP_ST_GET:
         if (TCPIsGetReady(HTTPSocket))
         {
            while (TCPGet(HTTPSocket, &c))
            {
               if ( (c=='\n') && (lc2=='\n') )
               {
                  state=HTTP_ST_PUT;
                  printf("\r\nHTTP: Got Page Request");                 
               }
               lc2=lc;
               lc=c; 
               
            }
         }
         break;

      case HTTP_ST_PUT:
         if (TCPIsPutReady(HTTPSocket)) {
            printf("\r\nHTTP: Putting Page Response");
            a0=read_adc();
           #if STACK_USE_CCS_PICNET
            set_adc_channel(1);
            delay_us(20);
            a1=read_adc();
            set_adc_channel(0);
           #endif
          //  sprintf(content, "<HTML><HEAD><TITLE>PICNET</TITLE></HEAD><BODY><H1>HELLiO</H1>");
            sprintf(content,st_HostName);
            sprintf(&content[0]+strlen(content),st_Message);
            //sprintf(&content[0]+strlen(content),"<P>%s = 0x%X",STANDARD_ADC_STRING,a0);
        //   #if STACK_USE_CCS_PICNET
       //     sprintf(&content[0]+strlen(content),"<BR>AN1 = 0x%X",a1);
       //    #endif
       //     sprintf(&content[0]+strlen(content),"</BODY></HTML>");
            //write_lcd_char(c);
            printf(HTTPPut, "HTTP/1.1 200 OK\r\n");
            printf(HTTPPut, "Content-Type: text/html\r\n");
            printf(HTTPPut, "Content-Length: %u\r\n",strlen(content));
            printf(HTTPPut, "\r\n");
            printf(HTTPPut, "%s", content);
            printf("\r\nHTTP: Flushing...");
            TCPFlush(HTTPSocket);
            printf("\r\nHTTP: Flushed!");
            state=HTTP_ST_GET;
         }
         break;

      case HTTP_ST_DISCONNECT:
         if (TCPIsPutReady(HTTPSocket)) {
            printf("\r\nHTTP: Force Disconnect");
            TCPDisconnect(HTTPSocket);
            state=HTTP_ST_WAIT_CONNECT;
         }
         break;
   }
}


The code for TCPIsConnected() is:
Code:

BOOL TCPIsConnected(TCP_SOCKET s)
{
    return ( TCB[s].smState == TCP_ESTABLISHED );
}

Any help is very much appreciated.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Wed Oct 07, 2009 6:21 pm     Reply with quote

You're using this as a client, not a server, right?

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
mazen82



Joined: 10 Sep 2009
Posts: 22

View user's profile Send private message

PostPosted: Thu Nov 05, 2009 7:20 am     Reply with quote

Yes, that is my intention.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Nov 05, 2009 2:53 pm     Reply with quote

ok. So TCPConnect returns something valid... and then you spin inside your wait for connection. You need to find out if the packet got out.. and a reply came back.

Do you have WireShark?

It might be worthwhile to connect a hub or managed switch with a monitor port so you can monitor the network traffic between your PIC and the remote port.

I would say it's almost a requirement for this kind of development environment.

I've done HTTP server side stuff with the PIC, but not client side.

Get that set up and I'm sure when you see the traffic, (or don't see it) that things will become clear and move a lot faster.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
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