WIZnet Developer Forum

TCP client error - hang-out at function ret = recv(sn,buf,size) ;

Hi everyone!
I use ioLibrary_Driver-master to study W5100.
I config w5100 mode TCP client:

[code] int32_t loopback_tcpc_ipwan(uint8_t sn, uint8_t* buf, uint8_t* destip, uint16_t destport)
{
int32_t ret;
uint16_t size = 0;
uint8_t i, index=0, en_getip=2;

         switch(getSn_SR(sn))
         { 

            case SOCK_ESTABLISHED :
               if(getSn_IR(sn) & Sn_IR_CON)
               {
                  printf("%d:Connected to %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
                  setSn_IR(sn,Sn_IR_CON);
               }  
                     strcpyf(buf,"GET /thaihoaicontrol/thaihoaicheckipwan.php HTTP/1.0\r\n");
                     strcatf(buf,"Host: www.thaihoaicenter.vn\r\n");
                     strcatf(buf,"Connection: close\r\n\r\n");   
                                    
                     if(send(sn,buf,strlen(buf))<=0){ 
                     en_tcpc_update = 0;                         
                     printf("\r\n[send-error]-try again!\r\n");
                       break;}   
                     printf("[SEND ok babe!]");                           
                                                                                    
                     if((size= getSn_RX_RSR(sn)) > 0){

                                if(size > DATA_BUF_SIZE) 
                                    size = DATA_BUF_SIZE; 
                                 
                               ret = recv(sn,buf,size);

                               if(ret <= 0){
                               printf("[ret <0 break]\r\n");       
                               return ret;}

                                printf("\r\nret = [%d]\r\nbuf = [%s]\r\n",ret,buf);
                               //Lay ipwan vao bien ipwan     
                               //xoa ipwan cu
                               for(i=0;i<16;i++)
                                    ipwan[i] = NULL;
                                                          
                               for(i=0;i< strlen(buf); i++){   //example ipwan = [192.168.1.1]
                                    if(buf[i] ==']'){
                                              en_getip = 0;        
                                              break;}
                                                        
                                    if(en_getip==1){
                                              ipwan[index] = buf[i]; 
                                              index++;}               
                                                          
                                    if(buf[i] =='['){
                                              en_getip=1;
                                              index = 0;}}
                                                
                               if(en_getip == 0)  
                               {
                                     en_tcp_client = 0;    
                                    printf("\r\n[IP-WAN-new] = [%s]\r\n",ipwan); 
                                    eeprom_read_string(78,lastipwan,EEPROM_read(77));  
                                    printf("\r\n[Lastipwan-old] = [%s]\r\n", lastipwan);              
                                              
                                    
                                    if(!strcmp(ipwan,lastipwan)){   
                                         printf("\r\n[IPWAN don't change]\r\n");   
                                         en_tcpc_update = 0;   
                                         en_loop_t1=0;    
                                         }              
                                    else{     
                                         printf("\r\n[IPWAN just change]\r\n");
                                         EEPROM_write(77,strlen(ipwan));    
                                         eeprom_write_string(78,ipwan);                                  
                                         en_tcpc_update = 1;}             
                                                                                                                                                                                                                                                       
                                    en_tcpc_ipwan=0;    
                                    close(sn);  
                                    printf("%d:Closed\r\n",sn);   
                                    break;} 

                                    disconnect(sn) ;}                 
                     else{
                                    printf("\r\n[Server don't respone]\r\n");   
                                    delay_us(10);}                                                                                                           
               break;   
            
            case SOCK_CLOSE_WAIT :
               if((ret=disconnect(sn)) != SOCK_OK) return ret;
               printf("%d:Closed\r\n",sn);
               break;

            case SOCK_INIT :
             //printf("%d:Try to connect to the %d.%d.%d.%d : %d \r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);    
               if( (ret =  connect(sn, destip, destport)) != SOCK_OK) return ret;
               break;
                                               
            case SOCK_CLOSED: 
                close(sn);           
               if((ret=socket(sn,Sn_MR_TCP,destport,0x00)) != sn)
                  return ret;  
                  printf("%d:TCP client Start\r\n",sn);
                  printf("%d:Opened\r\n",sn);
               break;   
               
            default:
               break;
               
         }      
                                        
         return 1;
      }

[/code]

When connected to my server, i send a command to get ip-wan,

[code]GET /thaihoaicontrol/thaihoaicheckipwan.php HTTP/1.0
Host: www.thaihoaicenter.vn
Connection: close

[/code]
and server returns data contain ip-wan:

[code]HTTP/1.1 200 OK
Date: Wed, 30 Dec 2015 10:11:16 GMT
Server: Apache/2
X-Powered-By: PHP/5.3.29
Vary: Accept-Encoding,User-Agent
Connection: close
Content-Type: text/html

[171.234.176.135][/code]

During data collection, not always also receive data from the server. After a few time received data, microcontroller was hangs at funciton [color=#FF0000]ret = recv(sn,buf,size);[/color] . Data is always sent and received normal by server in [color=#FF0000]mode TCP server[/color] so i think this function is not wrong.
I try to check bug, detection program couldn’t exit [color=#FF0000]while(getSn_CR(sn));[/color] stored in function [color=#FF0000]recv(sn,buf,size);[/color]

Please help me ,what is the problem ?
Thanks

Anh Tuan, please modify code a little to capture value of the getSn_CR(sn). Let’s say, display it in binary format on LED row or in any other way independent from W5100 communication channels.

If we will know the value W5100 gets stuck in, we may have an idea about what is going on.
In general under normal circumstances when processor issues command to W5100 and reads command register back, it should read non-zero value; when W5100 completes command it was instructed to perform, command register’s value becomes 0. In your case, for some reason, it does not happen.

Hello Eugeny, thanks for reply!
In case MCU was hang, the value of function [color=#FF0000]getSn_CR(sn);[/color] is 255, so it can not escape [color=#FF0000]while(getSn_CR(sn));[/color]
Could you give me some solution, thanks you!

IMHO it is not normal, W5100 gets stuck somewhere, and does not respond to queries.
Let’s check the process first: as I understand you use several HTTP sessions to communicate, thus one of the separate sessions hangs. Can you elaborate on which stage this connection hangs - on you opening connection (command 01), you establishing connection (command 04), you sending anything (command 20), on listening (02), on receiving (40)? In other words, which command you write to W5100 after which it starts responding with FF, and which commands precede this command?

Thanks for your help, Eugeny!
I’ll try carefully consider :cry:

Copyright © 2017 WIZnet Co., Ltd. All Rights Reserved.