lwip-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lwip-users] lwip tcp hangup


From: vincent cui
Subject: Re: [lwip-users] lwip tcp hangup
Date: Tue, 30 Aug 2011 14:07:28 +0000

My code is following.. the lwIP_close must be called after http request is deal 
with 

void WebServer_Handler()
{
        int lSocket;
        int clientfd;
        struct sockaddr_in sLocalAddr;
    struct sockaddr_in client_addr;
    int addrlen=sizeof(client_addr);
    int nbytes;
        int8_t *ptr;
        int32_t arg, ret;
        fd_set readset;
        struct tm time;
        struct timeval timeout;        
        timeout.tv_sec = 3;        
        timeout.tv_usec = 0;
        
        
        lSocket = lwip_socket(AF_INET, SOCK_STREAM, 0);
        if (lSocket < 0) 
        {
                printf("webserver socket \r\n");
                return;
        }

        memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
        sLocalAddr.sin_family = AF_INET;
        sLocalAddr.sin_len = sizeof(sLocalAddr);
        sLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        sLocalAddr.sin_port = htons(80);

        if (lwip_bind(lSocket, (struct sockaddr *)&sLocalAddr, 
sizeof(sLocalAddr)) < 0) 
        {
                printf("webserver bind \r\n");
        lwip_close(lSocket);
        return;
        }

#if LWIP_TCP_KEEPALIVE
        lwip_setsockopt(lSocket, SOL_SOCKET, TCP_KEEPIDLE,  (void 
*)&tcp_keepIdle,              sizeof(tcp_keepIdle));
    lwip_setsockopt(lSocket, SOL_SOCKET, TCP_KEEPINTVL, (void *)&tcp_keepIntvl, 
        sizeof(tcp_keepIntvl));
    lwip_setsockopt(lSocket, SOL_SOCKET, TCP_KEEPCNT,   (void *)&tcp_keepCount, 
        sizeof(tcp_keepCount)); 
#endif

        if ( lwip_listen(lSocket, MEMP_NUM_TCP_PCB_LISTEN) != 0 )
        {
                printf("webserver listen \r\n");
        lwip_close(lSocket);
        return;
        }

        while (1) 
        {
                vTaskDelay(10);
        clientfd = lwip_accept(lSocket, (struct sockaddr*)&client_addr, 
&addrlen);
        if (clientfd > 0)
                {
                        lwip_setsockopt(clientfd, SOL_SOCKET, SO_RCVTIMEO, 
&web_rcvtimeo, sizeof(uint32_t));
                        FD_ZERO (&readset);
                        FD_SET (clientfd, &readset);
                        ret = lwip_select (clientfd + 1, &readset, 0, 0, 
&timeout);
                        if (ret <= 0)
                        {
                                lwip_close( clientfd );
                                continue;
                        }
                        if (FD_ISSET( clientfd, &readset))
                        {
                                nbytes=lwip_recv(clientfd, webpage, 
sizeof(WebSide),0);
                if (nbytes>0)
                                {       
                                        if( strstr( (void *)webpage, "GET" ) != 
NULL )
                                        {
                                                memcpy ( webpage , WebSide 
,sizeof(WebSide) ); 
                                                if( ( ptr = memstrExt( (void 
*)webpage,"AD8%",strlen("AD8%"),sizeof(webpage)) ) != NULL )
                                                {
                                                        time = 
Time_GetCalendarTime();
                                                        sprintf((void *)ptr, 
"%d-%d-%d   %02d:%02d:%02d", time.tm_year, \
                                                                        
time.tm_mon+1, time.tm_mday,\
                                                                        
time.tm_hour, time.tm_min, time.tm_sec);
                                              
                                                } 
                                                send(clientfd, "HTTP/1.1 200 
OK\r\nContent-type: text/html\r\n\r\n", strlen("HTTP/1.1 200 
OK\r\nContent-type: text/html\r\n\r\n"), 0);  //发送HTPP请求什么的
                                        send(clientfd,webpage,strlen(WebSide) 
,0); 
                                                lwip_close(clientfd);
                                                continue;
                                        }
                                }
                                else
                                {
                                        lwip_close(clientfd);
                                        continue;
                                }       
                        }  
                }
    } 
}

锘?Vincent Cui
Sr.Firmware Engineer
Mobile: +8613482482211
Tel: +86 21 34612525x6104
Fax: +86 21 34619770
E-Mail: address@hidden
Shanghai EnLogic Electric Technology Co., Ltd.
Address: 1104-1106, Building A, No.391, Guiping Road, Xuhui District, Shanghai, 
200233
http://www.enlogic.com

-----Original Message-----
From: address@hidden [mailto:address@hidden On Behalf Of Kieran Mansley
Sent: 2011年8月30日 22:05
To: Mailing list for lwIP users
Subject: Re: [lwip-users] lwip tcp hangup

On Tue, 2011-08-30 at 13:28 +0000, vincent cui wrote:
> Keiran:
> 
> Would you tell me the way to avoid the case of connection in CLOSE_WAIT ?

I haven't looked at the source for that application, so can't suggest a
precise fix, but fundamentally those connections are in that state
either because (i) the application has not called close(); or (ii) there
is a bug in lwIP.  The former seems most likely at the moment, and so to
solve the problem you need to know why the application hasn't called
close on those sockets.

Kieran


_______________________________________________
lwip-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/lwip-users

reply via email to

[Prev in Thread] Current Thread [Next in Thread]