lwip-users
[Top][All Lists]
Advanced

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

[lwip-users] Re: advice on recovery on a socket send error using


From: shogun
Subject: [lwip-users] Re: advice on recovery on a socket send error using
Date: Thu, 19 Aug 2010 17:36:37 -0400

<SNIP>
This all seems a little confused.  You're trying to close a data PCB but
then to re-open it you're creating a listening PCB.  Do you not still
have the bound listening PCB from when you first accepted the
connection?  You should be able to just call tcp_accept() on that to
re-open the data socket (although it will have to wait for the remote
end reconnect).

It might be clearer if we could see your actual code with function
arguments etc.

> 
> 2) My other question is what would be the likely cause of the tcp_write()
> returning 0xFF?

0xff is an odd value for tcp_write() to return.  It would normally
return an error value from the set defined in src/include/lwip/err.h
The error number is signed and the value will be negative so perhaps
that is causing the confusion.

Kieran

<SNIP>

I have some extra stuff I more or less removed in the structure
"mainNetInfoStruct" but ignore that.  mainNetInfoStruct does however contain
the pointer to the PCB in the accept callback to be used later for sending.
The question is about what to do after a send error and if I "reset" the
socket correctly after the error.  The goal is to have the socket close and
allow the client to reconnect.  Below is the header and the source code more
or less.  Also, thanks for helping me out with this! I am sort of new to
this and appreciate the help and advice.


#ifndef MAIN_SOCKET_H_
#define MAIN_SOCKET_H_

#define MAIN_SOCKET_PORT        5058


extern netInfoPort mainNetInfoStruct;

/*******************************************************/
void main_socket_init( void);
static err_t main_accept(void *arg, struct tcp_pcb *pcb, err_t err);
static err_t main_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t
err );
static err_t main_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 ); 
static err_t main_sent_callbackClear(void *arg, struct tcp_pcb *pcb, u16_t
var1 );
static void socket_error(void *arg, err_t err);
static void main_client_close(struct tcp_pcb *pcb);
static err_t main_recvData(void *arg, struct tcp_pcb *pcb, struct pbuf *p,
err_t err );
int main_sendData(char * data);
int main_sendDataBytes(char * data, int id, int size);
#endif /*MAIN_SOCKET_H_*/



//start source file

netInfoPort mainNetInfoStruct;

void main_socket_init( void)
{
        struct tcp_pcb *pcb;
        
        UARTprintf("\nstarting main socket on 5059...\n");
        pcb = tcp_new(); //create TCPIP Connection
        tcp_bind(pcb, IP_ADDR_ANY, MAIN_SOCKET_PORT);
        pcb = tcp_listen(pcb);
        tcp_accept(pcb, main_accept);
} 

static err_t main_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
        UARTprintf("\nmain port connection on %d accepted...\n",
MAIN_SOCKET_PORT);
        tcp_err(pcb, socket_error);              
        mainNetInfoStruct.pcb = pcb;    //need this to send data later
        tcp_recv(pcb, main_recv);               
        pcb->flags |= TF_NODELAY;               //turn off Nagle
        mainNetInfoStruct.portConnected = TRUE;
        mainNetInfoStruct.sending = FALSE;      
        return ERR_OK;
} 



static err_t main_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t
err )
{
        char *rq;
        static char debugStr[265];
        if(p != NULL)
        { 
                rq = p->payload;
                EventHandler( rq );     // command parser / handler
                tcp_recved(pcb, p->tot_len);            
                pbuf_free(p); // pbuf_free does not return an error code, it
returns the number of pbuf structures that were deallocated.
        }
        else //pointer is null
        {
                main_client_close(pcb); 
                UARTprintf("\nmain port disconnected...\n");
        }
        return ERR_OK;
}




static err_t main_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 )
{
        mainNetInfoStruct.sending = FALSE;
        UARTprintf("\nmain sent callback done\n");
        return ERR_OK;  
} 

static void main_client_close(struct tcp_pcb *pcb)
{
        tcp_arg(pcb, NULL);     
        tcp_sent(pcb, NULL);
        tcp_abort(pcb); // aborts the connection
        tcp_close(pcb);
} //end main_client_close()

int main_sendData(char * data)
{
        err_t ert = 0;
        
        if(mainNetInfoStruct.portConnected)
        {
                tcp_sent(mainNetInfoStruct.pcb, main_sent);
                mainNetInfoStruct.sending = TRUE;
                ert = tcp_write(mainNetInfoStruct.pcb, data, 256, 0);
                if(ert)// on error, close the socket
                {
                        UARTprintf("main_sendData() error (%d) sending
data\n", ert);
                        mainNetInfoStruct.portConnected = FALSE;
                        //pbuf_free(p);
                        main_client_close(mainNetInfoStruct.pcb);

                }
                return 0;
        }
        else
        {
                return 1;
        }
}//end int main_sendData(char * data)



int main_sendDataBytes(char * data, int id, int size)
{
        err_t ert = 0;
        int timeout = 0;
        
        GPIO_G_set_testpoints(0x4, 0x4);
        
        mainNetInfoStruct.pcb->flags |= TF_NODELAY;     //turn off Nagle
        
        tcp_sent(mainNetInfoStruct.pcb, main_sent_callbackClear);
        mainNetInfoStruct.sending = TRUE;
        mainNetInfoStruct.id = id;
        mainNetInfoStruct.messagesRequested++;
        ert = tcp_write(mainNetInfoStruct.pcb, data, size, 0);  
        if(ert)// on error, close the socket
        {
                UARTprintf("main port main_sendDataBytes() error (%d)
sending data\n", ert);
                mainNetInfoStruct.portConnected = FALSE;
                main_client_close(mainNetInfoStruct.pcb);

        }
        tcp_output(mainNetInfoStruct.pcb);  //flush after the send
        return 0;
}//end int main_sendDataBytes(char * data, int id, int size)

//this gets called 2X for some reason (1 for each packet???)
static err_t main_sent_callbackClear(void *arg, struct tcp_pcb *pcb, u16_t
var1 )
{
        GPIO_G_set_testpoints(0x4, 0);
        return ERR_OK;  
} //end main_sent_callbackClear()



static void socket_error(void *arg, err_t err)
{
        UARTprintf("TCP Error Function....Err[%d]\n", err);    
}




















reply via email to

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