2024-05-02 09:47 AM
Hi,
I built a telnet server using lwip on FreeRTOS. I based my code on the LwIP_HTTP_Server_Netconn_RTOS example.
Now, I want to use the connection to relay/send data to the client from different places in my program but for this, I need to know the state of the connection (if aclient is connected or not). I've tried the following but turns out, conn-> state always stays NETCONN_NONE even when a client is connected, i.e. that's not the way to do it:
void relay_str_to_socket(char *pch, int sz)
{
struct netconn *conn = {0};
conn = curr_con_get();
if (conn->state != NETCONN_NONE) {
if (netconn_err(conn) == ERR_OK) {
netconn_write(conn, pch, sz, NETCONN_NOCOPY);
}
}
}
void telnet_server_serve(struct netconn *conn) {
struct netbuf *inbuf;
err_t recv_err;
char* buf;
u16_t buflen;
char *resp_ok = "OK, data parsed successfully!\n";
char *resp_err = "ERR, data could not be parsed\n is first char '{'?\n";
// Keep the connection open to continuously handle data
while (1) {
// Read the data from the port, blocking if nothing yet there
recv_err = netconn_recv(conn, &inbuf);
if (recv_err == ERR_OK) {
if (netconn_err(conn) == ERR_OK) {
curr_con_set(conn);
netbuf_data(inbuf, (void**)&buf, &buflen);
if (buflen > 0 && buf[0] == '{') {
// Extract data from JSON string
int rv = ethdataDecode(&m_dat, buf);
need_conf_set(1); // Set signal that new command needs processing
if (rv == OK) {
relay_str_to_socket(resp_ok, strlen(resp_ok));
}
} else {
relay_str_to_socket(resp_err, strlen(resp_err));
}
}
netbuf_delete(inbuf); // Clean up the buffer
} else if (recv_err == ERR_CLSD || recv_err == ERR_CONN) {
// Close the connection if the client disconnected
break;
}
}
If I send data to the socketr if it's not connected, it appears to get stuck and never returns...
Solved! Go to Solution.
2024-05-02 10:26 AM
Turns out I was abvle to resolve this by setting a manual "connected" flag that isset after netconn_accept() and reset after netconn_delete() in my server_netconn_thread().
2024-05-02 10:26 AM
Turns out I was abvle to resolve this by setting a manual "connected" flag that isset after netconn_accept() and reset after netconn_delete() in my server_netconn_thread().