lwip-users
[Top][All Lists]
Advanced

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

[lwip-users] questions on lwip (two TCP/IP sockets)


From: shogun
Subject: [lwip-users] questions on lwip (two TCP/IP sockets)
Date: Wed, 11 Aug 2010 10:02:40 -0400

I have some questions about using lwip but first some background.  I am using a Stellaris 9b92 eval board and want to create two sockets (servers) to listen on two different ports and each port can send and receive different data.  The ports are 5058 and 5059.  I stated with a example application and modified it to do what I wanted to evaluate the hardware and software.  I can get one port to work at a time but when I try both ports together is depends on what connection to what port I make first if the data gets sent to the right port.  Below is the header file followed by the source file.  I have three initialization functions, one for each port, and one that should init both ports “void newSocsInit()” at once and I intend to use the one that does both at once.  (I use the other two sometimes that do one port at a time for test).

 

My questions are:

 

From reading the documentation, rawapi.txt  “The tcp_listen() function returns a new connection identifier, and the one passed as an argument to the function will be deallocated. The reason for this behavior is that less memory is needed for a connection that is listening, so tcp_listen() will  reclaim the memory needed for the original connection andallocate a new smaller memory block for the listening connection.”  It would appear I no longer need the original pointer that the tcp_new(); returns but I find I need to keep  a copy of the pointer from new to use with the send functions. 

 

Can someone shed some light on this and help me understand this better and why when I try to use both sockets at once the connect order matters?

 

Thanks,

Doug

 

 

#ifndef SOCKETS_H_

#define SOCKETS_H_

 

extern int socket2Connected, socketNewConnected;

 

void newSocInit();

err_t socket_new_accept(void *arg, struct tcp_pcb *pcb, err_t err);

static err_t socket_new_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err );

static err_t socket_new_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 ); //basic callback

int send_socket_new_Data(char * data, int size);

static err_t socket_new_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 );

 

 

void newSoc2Init();

err_t socket2_new_accept(void *arg, struct tcp_pcb *pcb, err_t err);

static err_t socket2_new_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err );

static err_t socket2_new_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 ); //basic callback

int send_socket2_new_Data(char * data, int size);

static err_t socket2_new_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 );

 

#endif /*SOCKETS_H_*/

 

#include "inc/hw_ints.h"

#include "inc/hw_memmap.h"

#include "inc/hw_nvic.h"

#include "inc/hw_types.h"

#include "driverlib/ethernet.h"

#include "driverlib/flash.h"

#include "driverlib/gpio.h"

#include "driverlib/interrupt.h"

#include "driverlib/rom.h"

#include "driverlib/sysctl.h"

#include "driverlib/systick.h"

#include "utils/locator.h"

#include "utils/lwiplib.h"

#include "utils/uartstdio.h"

#include "httpserver_raw/httpd.h"

#include <string.h>

#include <stdio.h>

#include <stdlib.h>

#include "sockets.h"

 

#define  SOCKET_ONE_PORT 5058

#define  SOCKET_TWO_PORT 5059

 

 

 

struct tcp_pcb *newSoc_pcb, *newSoc_pcb_save;

struct tcp_pcb *newSoc2_pcb, *newSoc2_pcb_save;

 

int socket2Connected, socketNewConnected;

 

/***************************** start soc new ********************************/

//This  is just for test and not typically used

 

void newSocInit()

{

                err_t ert = 0;

               

                socketNewConnected = 0;

               

                newSoc_pcb = tcp_new();

                newSoc_pcb_save = newSoc_pcb;

                ert = tcp_bind(newSoc_pcb, IP_ADDR_ANY, SOCKET_ONE_PORT);

                if(ert)

                {

                    UARTprintf(" error (%d) binding\n", ert);

                }

                tcp_arg(newSoc_pcb, NULL);

                newSoc_pcb = tcp_listen(newSoc_pcb);

               

//newSoc_pcb_save = newSoc_pcb;

               

                tcp_accept(newSoc_pcb, socket_new_accept); //..,

               

                //tcp_sent(newSoc_pcb, socket_new_sent);

}

 

err_t socket_new_accept(void *arg, struct tcp_pcb *pcb, err_t err)

{

     //tcp_accepted(pcb);

                 UARTprintf("\nsocket new port %d accepted...\n", SOCKET_ONE_PORT);

                 tcp_recv(pcb, socket_new_recv); //use this  connect_callback

                 pcb->flags |= TF_NODELAY; //turn off Nagle

                 socketNewConnected = 1;

 

                return ERR_OK;

}

static err_t socket_new_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err )

{

                char *rq;

                static char debugStr[265];

                err_t ert = 0;

                if(p != NULL)

                {

                                rq = p->payload;

                                sprintf(debugStr, "socket %d received >*%s*<\n", SOCKET_ONE_PORT, rq);

                                //tcp_recved(pcb, p->tot_len);

                                tcp_recved(pcb, p->tot_len);//need to tell stack data received

 

                                UARTprintf(debugStr);

                 

                                ert = pbuf_free(p);

                                while(p->next) //see if next contains a non NULL pointer

                                {

                                                ert = pbuf_free(p->next);

                                                if(ert)

                                                {

                                                                UARTprintf(" error (%d) freeing data at %x\n", ert, p->next);

                                                }

                                                p = p->next;

                                }//end clean up memory 

 

 

//                            tcp_sent(newSoc_pcb_save, socket_new_sent);

//                            ert = tcp_write(newSoc_pcb_save, debugStr, 255, 0);

//                            if(ert)

//                            {

//                                            UARTprintf("\nsocket_new_recv() error %X sending data\n", ert);

//                            }

 

                                ert = pbuf_free(p);

                                if(ert)

                                {

                                                UARTprintf("debug socket error (%d) freeing data\n", ert);

                                }

                               

                                send_socket_new_Data(debugStr, sizeof(debugStr));

                               

                }

                else //pointer is null

                {

                                UARTprintf("socket %d disconnected...\n", SOCKET_ONE_PORT);

                                socketNewConnected = 0;

                }

                return ERR_OK;

} // static err_t socket_new_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err )

 

 

static err_t socket_new_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 )

{

                UARTprintf("port %d sent callback done\n", SOCKET_ONE_PORT);

                return ERR_OK; 

}

 

 

int send_socket_new_Data(char * data, int size)

{

                 err_t ert = 0;

                 int memAvai;

                 

                 memAvai = tcp_sndbuf(newSoc_pcb_save);

                 UARTprintf("\nMemory on port %d to send = %X\n", SOCKET_ONE_PORT, memAvai);

                 

                               

                 //tcp_sent(newSoc_pcb, socket_new_sent);

                 tcp_sent(newSoc_pcb_save, socket_new_sent);

                 ert = tcp_write(newSoc_pcb_save, data, size, 0); 

                 if(ert)

                 {

                                UARTprintf("\nerror %X sending data\n", ert);

                 }

                 tcp_output(newSoc_pcb);  //flush after the send

                 return 0;

}

 

/***************************** end soc new ********************************/

 

 

 

 

 

/***************************** start soc 2 ********************************/

 

//This  is just for test and not typically used

void newSoc2Init()

{

                err_t ert = 0;

               

                socket2Connected = 0;

               

                newSoc2_pcb = tcp_new();

                newSoc2_pcb_save = newSoc2_pcb;

                ert = tcp_bind(newSoc2_pcb, IP_ADDR_ANY, SOCKET_TWO_PORT);

                if(ert)

                {

                    UARTprintf(" error (%d) binding\n", ert);

                }

                tcp_arg(newSoc2_pcb, NULL);

                newSoc2_pcb = tcp_listen(newSoc2_pcb);

               

//newSoc2_pcb_save = newSoc2_pcb;

               

                tcp_accept(newSoc2_pcb, socket2_new_accept); //..,

               

                //tcp_sent(newSoc_pcb, socket_new_sent);

}

 

err_t socket2_new_accept(void *arg, struct tcp_pcb *pcb, err_t err)

{

     //tcp_accepted(pcb);

                 UARTprintf("\nsocket port %d accepted...\n", SOCKET_TWO_PORT);

                 tcp_recv(pcb, socket2_new_recv); //use this  connect_callback

                 pcb->flags |= TF_NODELAY; //turn off Nagle

                 socket2Connected = 1;

 

                return ERR_OK;

}

static err_t socket2_new_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err )

{

                char *rq;

                static char debugStr[265];

                err_t ert = 0;

                if(p != NULL)

                {

                                rq = p->payload;

                                sprintf(debugStr, "socket %d received >*%s*<\n", SOCKET_TWO_PORT, rq);

                                //tcp_recved(pcb, p->tot_len);

                                tcp_recved(pcb, p->tot_len);//need to tell stack data received

 

                                UARTprintf(debugStr);

                 

                                ert = pbuf_free(p);

                                while(p->next) //see if next contains a non NULL pointer

                                {

                                                ert = pbuf_free(p->next);

                                                if(ert)

                                                {

                                                                UARTprintf(" error (%d) freeing data at %x\n", ert, p->next);

                                                }

                                                p = p->next;

                                }//end clean up memory 

 

                                ert = pbuf_free(p);

                                if(ert)

                                {

                                                UARTprintf("socket %d error (%d) freeing data\n", SOCKET_TWO_PORT, ert);

                                }

                               

                                send_socket2_new_Data(debugStr, sizeof(debugStr));

                               

                }

                else //pointer is null

                {

                                UARTprintf("socket %d disconnected...\n", SOCKET_TWO_PORT);

                                socket2Connected = 0;

                }

                return ERR_OK;

} // static err_t socket_new_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err )

 

 

static err_t socket2_new_sent(void *arg, struct tcp_pcb *pcb, u16_t var1 )

{

                UARTprintf("port %d sent callback done\n", SOCKET_TWO_PORT);

                return ERR_OK; 

}

 

 

int send_socket2_new_Data(char * data, int size)

{

                 err_t ert = 0;

                 int memAvai;

                 

                 memAvai = tcp_sndbuf(newSoc2_pcb_save);

                 UARTprintf("\nMemory port %d to send = %X\n", SOCKET_TWO_PORT, memAvai);

                               

                 //tcp_sent(newSoc_pcb, socket_new_sent);

                 tcp_sent(newSoc2_pcb_save, socket2_new_sent);

                 ert = tcp_write(newSoc2_pcb_save, data, size, 0); 

                 if(ert)

                 {

                                UARTprintf("\nerror %X sending data\n", ert);

                 }

                 tcp_output(newSoc2_pcb);  //flush after the send

                 return 0;

}

 

 

/***************************** end soc 2 ********************************/

void newSocsInit()

{

                err_t ert = 0;

               

                socketNewConnected = 0;

                socket2Connected = 0;

               

                newSoc_pcb = tcp_new();

                newSoc_pcb_save = newSoc_pcb;

               

                newSoc2_pcb = tcp_new();

                newSoc2_pcb_save = newSoc2_pcb;

               

                ert = tcp_bind(newSoc_pcb, IP_ADDR_ANY, SOCKET_ONE_PORT);

                if(ert)

                {

                    UARTprintf(" error (%d) binding\n", ert);

                }

                tcp_arg(newSoc_pcb, NULL);

                newSoc_pcb = tcp_listen(newSoc_pcb);

               

                ert = tcp_bind(newSoc2_pcb, IP_ADDR_ANY, SOCKET_TWO_PORT);

                if(ert)

                {

                    UARTprintf(" error (%d) binding\n", ert);

                }

                tcp_arg(newSoc2_pcb, NULL);

                newSoc2_pcb = tcp_listen(newSoc2_pcb);

               

//newSoc_pcb_save = newSoc_pcb;

               

                tcp_accept(newSoc_pcb, socket_new_accept); //..

                tcp_accept(newSoc2_pcb, socket2_new_accept);

               

                //tcp_sent(newSoc_pcb, socket_new_sent);

} // void newSocsInit()

 

 

 

               

 

 

 

 


reply via email to

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