Недавно начал ковырять lwip на stm32. Задачка сделать сервер, получать от клиента данные и отправлять ему что-то в ответ.
Во всяких примерах сделано просто - дождались коннекта, прочитали "GET /", выслали строку и закрыли соединение. Вот код примера, он работает.
CODE
conn = netconn_new(NETCONN_TCP);
if (conn!= NULL)
{
/* Bind to port 80 (HTTP) with default IP address */
err = netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
{
/* Put the connection into LISTEN state */
netconn_listen(conn);
while(1)
{
/* accept any icoming connection */
if(newconn->state != NETCONN_CONNECT) err = netconn_accept(conn, &newconn);
if(err == ERR_OK)
{
res = netconn_recv(newconn, &inbuf);
if (res == ERR_OK)
{
netbuf_data(inbuf, (void**)&buf, &buflen);
if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
{
sprintf(data, "Hello %d times!", call_times++);
len = strlen(data);
netconn_write(newconn, (const unsigned char*)(data), (size_t)len, NETCONN_NOCOPY);
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(newconn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
/* delete connection */
netconn_delete(newconn);
}
}
}
else
{
printf("can not bind netconn");
}
}
else
{
printf("can not create netconn");
}
if (conn!= NULL)
{
/* Bind to port 80 (HTTP) with default IP address */
err = netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
{
/* Put the connection into LISTEN state */
netconn_listen(conn);
while(1)
{
/* accept any icoming connection */
if(newconn->state != NETCONN_CONNECT) err = netconn_accept(conn, &newconn);
if(err == ERR_OK)
{
res = netconn_recv(newconn, &inbuf);
if (res == ERR_OK)
{
netbuf_data(inbuf, (void**)&buf, &buflen);
if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
{
sprintf(data, "Hello %d times!", call_times++);
len = strlen(data);
netconn_write(newconn, (const unsigned char*)(data), (size_t)len, NETCONN_NOCOPY);
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(newconn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
/* delete connection */
netconn_delete(newconn);
}
}
}
else
{
printf("can not bind netconn");
}
}
else
{
printf("can not create netconn");
}
Мне по-сути надо то-же самое, но соединение не закрывать, а ждать дальнейших данных. Добавил цикл в чтение с выходом по ошибке
CODE
conn = netconn_new(NETCONN_TCP);
if (conn!= NULL)
{
/* Bind to port 80 (HTTP) with default IP address */
err = netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
{
/* Put the connection into LISTEN state */
netconn_listen(conn);
while(1)
{
/* accept any icoming connection */
if(newconn->state != NETCONN_CONNECT) err = netconn_accept(conn, &newconn);
if(err == ERR_OK)
{
do
{
res = netconn_recv(newconn, &inbuf);
if (res == ERR_OK)
{
netbuf_data(inbuf, (void**)&buf, &buflen);
if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
{
sprintf(data, "Hello 80 %d times!", call_times++);
len = strlen(data);
netconn_write(newconn, (const unsigned char*)(data), (size_t)len, NETCONN_NOCOPY);
}
}
}
while (res == ERR_OK);
/* Close the connection (server closes in HTTP) */
netconn_close(newconn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
/* delete connection */
netconn_delete(newconn);
}
}
}
else
{
printf("can not bind netconn");
}
}
else
{
printf("can not create netconn");
}
if (conn!= NULL)
{
/* Bind to port 80 (HTTP) with default IP address */
err = netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
{
/* Put the connection into LISTEN state */
netconn_listen(conn);
while(1)
{
/* accept any icoming connection */
if(newconn->state != NETCONN_CONNECT) err = netconn_accept(conn, &newconn);
if(err == ERR_OK)
{
do
{
res = netconn_recv(newconn, &inbuf);
if (res == ERR_OK)
{
netbuf_data(inbuf, (void**)&buf, &buflen);
if ((buflen >=5) && (strncmp(buf, "GET /", 5) == 0))
{
sprintf(data, "Hello 80 %d times!", call_times++);
len = strlen(data);
netconn_write(newconn, (const unsigned char*)(data), (size_t)len, NETCONN_NOCOPY);
}
}
}
while (res == ERR_OK);
/* Close the connection (server closes in HTTP) */
netconn_close(newconn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
/* delete connection */
netconn_delete(newconn);
}
}
}
else
{
printf("can not bind netconn");
}
}
else
{
printf("can not create netconn");
}
По задумке на каждый запрос "GET /" должен возвращать строку и ждать следующую посылку. В реальности же работает один раз и дальше вылетает из netconn_recv с ошибкой -1. Даже реконнект не помогает. Что я делаю не так?
И дальше вопрос: как определить, отконнектился клиент или еще висит? netconn_state всегда NONE, оттуда состояние коннекта не вынуть. Неужели только не коллбеках все это можно нормально сделать?