Example of simple TCP server?

newsettler_AI
Posts: 121
Joined: Wed Apr 05, 2017 12:49 pm

Example of simple TCP server?

Postby newsettler_AI » Fri Apr 27, 2018 5:11 pm

Hi,

I'm looking for examples of TCP server.
I need to read-write data over tcp: esp32 starts as wifi softap, then I connect from android phone and start some android tcp client.
Then I can exchanging data.

I have done wifi section and make some tcp connection.
Problem is that tcp server droping connection each time I send data from phone.

Here is my code:

Code: Select all

// handle connection
static void netconn_handler(struct netconn *conn)
{
	struct netbuf *inbuf;
	char *buf;
	uint16_t buflen;
	err_t err;

	err = netconn_recv(conn, &inbuf);
    printf("net connection received\n");

	if(err == ERR_OK)
	{
		netbuf_data(inbuf, (void**)&buf, &buflen);

		memcpy(MyData.TCP_Buf, buf, buflen);
		MyData.TCP_conn = true;
		MyData.TCP_len = buflen;
		MyData.TCP_got_packet = true;

		ESP_LOGI(TCP_TAG, "TCP_to_UART_write len:%d\n data:%s", MyData.TCP_len, (uint8_t *)MyData.TCP_Buf);
		RS485_send_data(E_TCP_BUFFER);
		printf("RS485_send_data sent\n");

        if (MyData.UART_got_packet == true)
        {
        	MyData.UART_got_packet = false;
        	netconn_write(conn, MyData.TCP_Buf, MyData.TCP_len, NETCONN_NOCOPY);
        	ESP_LOGI(TCP_TAG, "TCP_to_UART_write len:%d\n data:%s", MyData.TCP_len, (uint8_t *)MyData.TCP_Buf);
        }
	}
	// close the connection and free the buffer
	netconn_close(conn);
	MyData.TCP_conn = false;
	printf("connection in conn handler closed\n");
	netbuf_delete(inbuf);
}

//tcp server
static void tcp_server_task(void *pvParameters)
{
	struct netconn *conn, *newconn;
	err_t err;
	conn = netconn_new(NETCONN_TCP);
	netconn_bind(conn, NULL, TCP_PORT);
	netconn_listen(conn);
	printf("TCP Server listening port:%d\n",TCP_PORT);
	do
	{
		err = netconn_accept(conn, &newconn);
		printf("New client connected\n");
		if (err == ERR_OK) {
			printf("Process connection\n");
			netconn_handler(newconn);
			FlowMeterData.TCP_conn = false;
			printf("Process connection end\n");
			netconn_delete(newconn);
			printf("net connection deleted\n");
		}
		vTaskDelay(1); //allows task to be pre-empted
	} while(err == ERR_OK);
	printf("Closing tcp\n");
	netconn_close(conn);
	netconn_delete(conn);
	printf("\n");
}

I tried not to closing connection, but then I cant exchange data.

User avatar
fly135
Posts: 606
Joined: Wed Jan 03, 2018 8:33 pm
Location: Orlando, FL

Re: Example of simple TCP server?

Postby fly135 » Fri Apr 27, 2018 5:20 pm

Just curious. Why not use the regular run of the mill socket api? I have no problem with running a TCP server with it.

John A

newsettler_AI
Posts: 121
Joined: Wed Apr 05, 2017 12:49 pm

Re: Example of simple TCP server?

Postby newsettler_AI » Wed May 02, 2018 10:51 am

Are there some examples of this api in esp idf?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: Example of simple TCP server?

Postby kolban » Wed May 02, 2018 2:00 pm

Here is a socket server for ESP-IDF for TCP sample that was working back in 2016.

https://github.com/nkolban/esp32-snippe ... t_server.c
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Who is online

Users browsing this forum: No registered users and 94 guests