I wrote a TCP client in C and a TCP server in Python. The client runs on a ESP32S2 board while the server runs on my PC (virtual Linux OS) and both the board and PC are connected to the same Wi-Fi. However, even though the same client code works as expected on my PC, it is not working when the code is loaded into the ESP32S2. The connect() function returns errno 113. I was wondering what could be the underlying issues.
Here is the client code (code handling Wi-Fi connection is omitted for simplicity):
#define SERVER_IP AF_INET
#define SERVER_ADDR "192.168.1.157"
#define SERVER_PORT 5566
static int client_fd;
static void client_init(struct sockaddr_in addr) {
client_fd = socket(SERVER_IP, SOCK_STREAM, 0);
addr.sin_family = SERVER_IP;
addr.sin_port = htons(SERVER_PORT);
if (inet_pton(SERVER_IP, SERVER_ADDR, &(addr.sin_addr)) < 0) {
ESP_LOGE(TAG, "Invalid address or protocol (errno: %d)", errno);
}
bzero(&(addr.sin_zero), 8);
if (connect(client_fd, (struct sockaddr *)(&addr), sizeof(addr)) == -1) {
ESP_LOGE(TAG, "connect error (errno: %d)", errno);
}
}
void app_main() {
wifi_sta_init();
struct sockaddr_in server_addr;
client_init(server_addr);
}
Here is the server code:
import socket
import sys
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_addr = ('0.0.0.0', 5566)
server_sock.bind(server_addr)
server_sock.listen(1)
while True:
print("waiting for a connection")
connection, client_addr = server_sock.accept()
try:
print(f"connection from {client_addr}")
while True:
data = connection.recv(1024)
with open('test.txt', 'wb') as file:
file.write(data)
print(f"received {data}")
if data:
break
else:
print("no more data from {client_address}")
break
finally:
connection.close()
Update: The issue is resolved after I set the network adapter setting of the virtual machine to bridged.