Discussion:
[lwip-users] TCP server netconn API
Saket Chawla
2012-02-05 13:08:45 UTC
Permalink
I am trying to build a simple TCP echoserver and client for same using
netconn API.
I am sure some might already have tried but i couldn't find any source on
internet


My system is running ubuntu 10.10

Directory structure to refer for compile instruction
/contrib
/lwip
/server.c
/lwipopts.h


File content

*lwipopts.h [i think there might me more required here that i don't
know of]*

#define LWIP_SOCKET 0


*server.c [took the basic code from *Design and Implementation of the
lwIPTCP/IP Stack*]*

#include <stdio.h>
#include <lwip/api.h>

int main()
{
struct netconn *conn, *newconn;
err_t err;

/* create a connection structure */
conn = netconn_new(NETCONN_TCP);

/* bind the connection to port 2000 on any local IP address */
*netconn_bind(conn, NULL, 7);*

printf("Now listening\n");
/* tell the connection to listen for incoming connection requests */
netconn_listen(conn);

/* Grab new connection. */
err = netconn_accept(conn, &newconn);
printf("accepted new connection %p\n", newconn);

/* Process the new connection. */
if (err == ERR_OK)
{
struct netbuf *buf;
void *data;
u16_t len;

while ((err = netconn_recv(newconn, &buf)) == ERR_OK)
{
printf("Received data\n");
do{
netbuf_data(buf, &data, &len);
err = netconn_write(newconn, data, len, NETCONN_COPY);
if (err != ERR_OK)
printf("tcpecho: netconn_write: error \"%s\"\n", lwip_strerr(err));
}while (netbuf_next(buf) >= 0);
netbuf_delete(buf);
}

/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}

}


compile instruction

gcc -I. -I lwip/src/include/ -I contrib/ports/unix/include/ -I
lwip/src/include/ipv4/ lwip/src/api/*.c lwip/src/core/*.c
lwip/src/core/ipv4/*.c contrib/ports/unix/sys_arch.c
lwip/src/netif/etharp.c -lpthread server.c


Problem
All files are included and no errors / warnings occur for compile
But in netconn_bind I receive an error

*Assertion "netconn_bind: invalid conn" failed at line 172 in
lwip/src/api/api_lib.c*
*Aborted*

Please let me know if i am missing something obvious
Also i did try to understand the server implementation from simhost but it
is too complicated for a basic echoserver application
So any reference manual or if someone has already build such application
for a start then a link to it is deeply appreciated.
--
Thanks
Regards
Saket Chawla
Kieran Mansley
2012-02-06 19:19:16 UTC
Permalink
Post by Saket Chawla
All files are included and no errors / warnings occur for compile
But in netconn_bind I receive an error
Assertion "netconn_bind: invalid conn" failed at line 172 in lwip/src/api/api_lib.c
Aborted
Please let me know if i am missing something obvious
What value did netconn_new() return? You're immediately passing this to netconn_bind() without checking that it succeeded.
Post by Saket Chawla
Also i did try to understand the server implementation from simhost but it is too complicated for a basic echoserver application
So any reference manual or if someone has already build such application for a start then a link to it is deeply appreciated.
The lwIP wiki is the most up to date source for information like this.

Kieran
Saket Chawla
2012-02-07 03:10:23 UTC
Permalink
Conn value is NULL

i tested it and also that is the only reason why i can get the error when
calling netconn_bind()
So netconn_new() is the call where something is not working.
--
Thanks
Regards
Saket Chawla
Saket Chawla
2012-02-07 07:38:11 UTC
Permalink
The code fails assertion at this pont

*LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return
NULL;);*
*
*
The type at fail point is MEMP_NETCONN
Post by Saket Chawla
Conn value is NULL
i tested it and also that is the only reason why i can get the error when
calling netconn_bind()
So netconn_new() is the call where something is not working.
--
Thanks
Regards
Saket Chawla
--
Thanks
Regards
Saket Chawla
Simon Goldschmidt
2012-02-07 07:45:11 UTC
Permalink
Post by Saket Chawla
The code fails assertion at this pont
*LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return
NULL;);*
*
*
The type at fail point is MEMP_NETCONN
That's strange, as MEMP_NETCONN isn't even the one with the highest number. I would suppose you have either defined LWIP_ERROR to something strange or you have mixed up include paths so that MEMP_NETCONN evaluates to something different in your application file than in memp.c. You can try to rule that out by printing MEMP_NETCONN from both files.

Simon
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
Kieran Mansley
2012-02-07 13:19:43 UTC
Permalink
Post by Simon Goldschmidt
Post by Saket Chawla
The code fails assertion at this pont
*LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return
NULL;);*
*
*
The type at fail point is MEMP_NETCONN
That's strange, as MEMP_NETCONN isn't even the one with the highest
number. I would suppose you have either defined LWIP_ERROR to
something strange or you have mixed up include paths so that
MEMP_NETCONN evaluates to something different in your application file
than in memp.c. You can try to rule that out by printing MEMP_NETCONN
from both files.
Or perhaps lwipopts.h doesn't have support for netconn API turned on? I
would expect compile errors then though.

Kieran

Loading...