Simple TCP communication over wifi
Simple TCP communication over wifi
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
If anyone has experience in such I'd appreciate help
Thank you for all help
Re: Simple TCP communication over wifi
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
Re: Simple TCP communication over wifi
Only client. Server maybe in future but for now just tiny client sending and receiving data
Re: Simple TCP communication over wifi
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;
}
Re: Simple TCP communication over wifi
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.
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
Re: Simple TCP communication over wifi
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
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
Re: Simple TCP communication over wifi
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
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
Re: Simple TCP communication over wifi
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
https://github.com/nkolban/esp32-snippe ... etClient.c
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32
Re: Simple TCP communication over wifi
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
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
Re: Simple TCP communication over wifi
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?
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: No registered users and 91 guests