I am trying to send a string from STM32H750B-DK with TCP and receive and print it in python. Here is C code for TCP server:
/*
* tcpserver.c
*
* Created on: Apr 20, 2022
* Author: controllerstech.com
*/
#include "lwip/opt.h"
#include "lwip/api.h"
#include "lwip/sys.h"
#include "tcpserver.h"
#include "string.h"
static struct netconn *conn, *newconn;
static struct netbuf *buf;
static ip_addr_t *addr;
static unsigned short port;
char msg[100];
char smsg[200];
char b[10] = {'0'};
/**** Send RESPONSE every time the client sends some data ******/
static void tcp_thread(void *arg)
{
err_t err, accept_err, recv_error;
/* Create a new connection identifier. */
conn = netconn_new(NETCONN_TCP);
if (conn!=NULL)
{
/* Bind connection to the port number 7. */
err = netconn_bind(conn, IP_ADDR_ANY, 146);
if (err == ERR_OK)
{
/* Tell connection to go into listening mode. */
netconn_listen(conn);
while (1)
{
/* Grab new connection. */
accept_err = netconn_accept(conn, &newconn);
/* Process the new connection. */
if (accept_err == ERR_OK)
{
/* receive the data from the client */
while (netconn_recv(newconn, &buf) == ERR_OK)
{
/* Extrct the address and port in case they are required */
addr = netbuf_fromaddr(buf); // get the address of the client
port = netbuf_fromport(buf); // get the Port of the client
/* If there is some data remaining to be sent, the following process will continue */
do
{
strncpy (msg, buf->p->payload, buf->p->len); // get the message from the client
if (msg[0]==103){
b[0] = '1';
}
else{
b[0] = 'h';
}
// Or modify the message received, so that we can send it back to the client
int len = sprintf (smsg, "\"%s\" was sent by the Server\n", b);
netconn_write(newconn, smsg, len, NETCONN_COPY); // send the message back to the client
memset (msg, '0円', 100); // clear the buffer
}
while (netbuf_next(buf) >0);
netbuf_delete(buf);
}
/* Close connection and discard connection identifier. */
netconn_close(newconn);
netconn_delete(newconn);
}
}
}
else
{
netconn_delete(conn);
}
}
}
void tcpserver_init(void)
{
sys_thread_new("tcp_thread", tcp_thread, NULL, DEFAULT_THREAD_STACKSIZE,osPriorityNormal);
}
And here is python code for TCP client I am using:
import socket
SERVER_IP = "0.0.0.0"
SERVER_PORT = 146
# Create a socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((SERVER_IP, SERVER_PORT))
server_socket.listen(1)
print("Server listening on", SERVER_IP, "port", SERVER_PORT)
while True:
client_socket, client_address = server_socket.accept()
print("Connection from:", client_address)
data = client_socket.recv(1024)
if data:
received_variable = int(data.decode())
print("Received variable:", received_variable)
client_socket.close()
When IP address is set to 0.0.0.0 it should listen on all available network interfaces so the program works, in console I get the message "Server listening on 0.0.0.0 port 146", but can't get the connection. When I set the IP to 169.254.211.100 (IP address of my STM32) I get this error:
line 43, in <module>
server_socket.bind((SERVER_IP, SERVER_PORT))
OSError: [WinError 10049] The requested address is not valid in its context
Otherwise I tried receiving the string in hercules and had no trouble connecting and receiving the string so I don-t understand why this doesn't work.
-
If you use socket.bind in Python too, you are creating a second tcp server. You have to use socket.connect. Take a look at: stackoverflow.com/questions/32445310/…Martin Scheuchenpflug– Martin Scheuchenpflug2023年09月27日 07:50:54 +00:00Commented Sep 27, 2023 at 7:50
-
1socket.listen + socket.accept creates a TCP server, not a TCP client. So you are basically have implement two server which wait for some client to connect - but no client. Use socket.connect instead with the IP and port of STM32 as destination. socket.bind is usually not needed for the client at all since a local IP address and port for the connection will be picked automatically when connecting.Steffen Ullrich– Steffen Ullrich2023年09月27日 07:56:21 +00:00Commented Sep 27, 2023 at 7:56
-
Thanks. I don't know how I missed that. It works fine now.AK16– AK162023年09月27日 08:01:01 +00:00Commented Sep 27, 2023 at 8:01