Hello,
I'm working on a basic RS422 to Ethernet converter using an ATmega2560 and a Wiznet W5500 module. My goal is to create two TCP servers on a single ATmega2560, each listening on a different port. These servers are intended to handle only one client connection at a time and should not allow multiple clients to connect simultaneously.
However, I'm encountering an issue: when one client is connected to a server, another client is able to connect to the same server, but it cannot send or receive any messages. To test this, I'm using the Hercules tool on Windows to establish multiple sessions.
The problem becomes more apparent when the previously connected client disconnects; all the messages that the new client attempted to send are suddenly sent to the serial port.
I've shared the complete project on GitHub: link
I've also included the function responsible for managing TCP connections below for reference. Can anyone offer suggestions on what might be causing this issue?
Any insights or guidance would be greatly appreciated.
Thank you.
void checkTCPdata()
{
static uint32_t timeSinceLastBroadcast = millis();
static uint32_t timeSinceLastModeSent = millis();
uint32_t currentTime = millis();
char ipAddressStr[16]; // Adjust size as needed
char rc = 0;
EthernetClient tempClient;
if (!g_localDataclient.connected())
{
tempClient = g_dataServer_local_cnx->accept(); // accept incoming connections
if (tempClient)
{
g_localDataclient.stop();
g_localDataclient = tempClient;
}
}
if (g_localDataclient.connected())
{
while (g_localDataclient.available() > 0) // if any data is received on TCP, write to serial
{
rc = g_localDataclient.read();
Serial.write(rc);
}
}
if (!g_remoteDataclient.connected())
{
tempClient = g_dataServer_remote_cnx->accept(); // accept incoming connections
if (tempClient)
{
g_remoteDataclient.stop();
g_remoteDataclient = tempClient;
}
}
if (g_remoteDataclient.connected())
{
if (!g_localDataclient.connected())
{
while (g_remoteDataclient.available() > 0) // if any data is received on TCP, write to serial
{
rc = g_remoteDataclient.read();
Serial.write(rc);
}
}
else
{
while (g_remoteDataclient.available() > 0) // if any data is received on TCP, write to serial
{
rc = g_remoteDataclient.read();
}
}
}
if ((currentTime - timeSinceLastBroadcast) >= 20000) // if there has not been any client connection broadcast own IP on port 9999
{
if (!g_localDataclient.connected() && !g_remoteDataclient.connected())
{
ipAddressToString(g_ip, ipAddressStr, sizeof(ipAddressStr));
timeSinceLastBroadcast = millis();
Udp.beginPacket(g_broadcastIP, 9999);
Udp.write(ipAddressStr);
Udp.endPacket();
}
}
if ((currentTime - timeSinceLastModeSent) >= 15000) // if there has not been any client connection broadcast own IP on port 9999
{
timeSinceLastModeSent = millis();
if (g_localDataclient.connected() && g_remoteDataclient.connected())
{
g_localDataclient.write("L&R\r\n");
g_remoteDataclient.write("L&R\r\n");
}
else if (g_localDataclient.connected() && !g_remoteDataclient.connected())
{
g_localDataclient.write("L\r\n");
}
else if (!g_localDataclient.connected() && g_remoteDataclient.connected())
{
g_remoteDataclient.write("R\r\n");
}
}
}