Simple TCP communication over wifi

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Simple TCP communication over wifi

Postby Deouss » Mon Jun 04, 2018 11:39 am

I just started a new project that requires simple tcp communication between esp and pc and looking for a code example particularly for RTOS esp-idf. Not sure if there are network socket libraries for the simplest tcp ip. Non blocking would be awesome.
If anyone has experience in such I'd appreciate help

Thank you for all help

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

Re: Simple TCP communication over wifi

Postby kolban » Mon Jun 04, 2018 2:31 pm

Is your ESP32 going to be a TCP/IP server accepting incoming client connection requests or will it be a client attempting to connect to an external TCP/IP server?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Simple TCP communication over wifi

Postby Deouss » Mon Jun 04, 2018 5:07 pm

Only client. Server maybe in future but for now just tiny client sending and receiving data

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

Re: Simple TCP communication over wifi

Postby fly135 » Mon Jun 04, 2018 5:13 pm

Deouss wrote:Only client. Server maybe in future but for now just tiny client sending and receiving data

Code: Select all

int create_ipv4_socket()
{
  struct addrinfo hints;
  struct addrinfo *res;
  struct in_addr *addr;

  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  
  int err = getaddrinfo(UDP_IPV4_ADDR, TCP_PORT, &hints, &res);

  if(err != 0 || res == NULL) {
    printf("DNS lookup failed err=%d res=%p\n", err, res);
    return -1;
  }

  /* Code to print the resolved IP.

     Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */
  addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr;
  printf("DNS lookup succeeded. IP=%s\n", inet_ntoa(*addr));

  l_sock = socket(res->ai_family, res->ai_socktype, 0);
  if(l_sock < 0) {
    printf("... Failed to allocate socket.\n");
    freeaddrinfo(res);
    return -1;
  }

  struct timeval to;
  to.tv_sec = 2;
  to.tv_usec = 0;
  setsockopt(l_sock,SOL_SOCKET,SO_SNDTIMEO,&to,sizeof(to));
  
  if(connect(l_sock, res->ai_addr, res->ai_addrlen) != 0) {
    printf("... socket connect failed errno=%d\n", errno);
    close(l_sock);
    freeaddrinfo(res);
    return -1;
  }

  printf("... connected\n");
  freeaddrinfo(res);

  // All set, socket is configured for sending and receiving
  return l_sock;
}


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

Re: Simple TCP communication over wifi

Postby kolban » Mon Jun 04, 2018 6:17 pm

As Mr fly135 wonderfully illustrates, there is nothing "special" in ESP32 TCP/IP programming. Now, of course, as soon as I said that ... the idea of "special" is subjective ... so let me explain. For many decades there has been a programming API available in C for working with TCP/IP networking. That API is called "sockets".

See:

https://en.wikipedia.org/wiki/Berkeley_sockets

... and where I am going with this is that if you find a book/article/videos on "sockets in general" then the vast majority of it will stand you well in the ESP32 world. If I were in your shoes, I'd grab a gallon of coffee, make a list of some good books and other materials on sockets ... (generically) and go read ... once you understand sockets concepts such as:

* socket
* bind
* accept
* listen
* send
* recv
* close

you will then find you can apply those identically in an ESP32 environment. You will also be able to apply the same ideas in other applications / platforms.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Simple TCP communication over wifi

Postby Deouss » Mon Jun 04, 2018 9:49 pm

Thank you very much guys!
I can build anything out of sockets)
If you know more examples for client server communication I'd be glad to see them.
Thanks again

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Simple TCP communication over wifi

Postby Deouss » Thu Jul 12, 2018 2:35 pm

I actually have a question about TCP client.

What are the correct steps to:

1) connect to desired WiFI SSID to use TCP transfers
2) initialize client tcp socket
3) connect socket to tcp server address
4) receive data from server

Thanks

PS: that example code above gives many errors related to undefined functions.
I do't know what header files I need for network app

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

Re: Simple TCP communication over wifi

Postby kolban » Thu Jul 12, 2018 8:24 pm

Here is a sample I keep as a reference for making a client connection to an external socket server. This assumes we already have a good WiFi connection to the network:

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

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

Re: Simple TCP communication over wifi

Postby fly135 » Thu Jul 12, 2018 10:17 pm

The examples directory in the ESP32 SDK is full of examples of connecting to wifi. Look in the $IDF_PATH/examples/protocols directory for examples. In those examples you will see the necessary include files.

I recommend a text editor that has a "find in files" option like notepad++. If you don't know the include file of a definition you can typically do a "find in files" on that definition in the $IDF_PATH/components directory using "*.h" as a filter.

John A

Deouss
Posts: 425
Joined: Tue Mar 20, 2018 11:36 am

Re: Simple TCP communication over wifi

Postby Deouss » Fri Jul 13, 2018 8:44 am

I am getting error during connection to simple console Windows program listening on port
error 'Connection reset by peer'

Soket family is InterNetwork (on console) - esp uses AF_INET - I guess it is same?
I tested all with iperf example so maybe I need to use other socket or something.
Firewall might be issue too but it is off. What could be wrong?

Who is online

Users browsing this forum: Ask Jeeves [Bot] and 105 guests