Hi,
I have two Megas, each with an ethernet shield and each acting as a server to receive data from a client (a LabVIEW VI). (See minimal sketch below.)
When I send packets to only one of the servers (by disabling one of the clients in the VI) everything works fine (as seen both by the Megas subsequent performance and by monitoring the packets via Wireshark).
However, if I try to send data to both servers I am only able to do so once. Re-sending any data causes the client to time out. Examining the packets with Wireshark, I see that on the second attempt the client sends out a SYN request, but the server doesn't respond with a SYN ACK. (The client resends the SYN until it times out, unless the TCP socket (terminology???) close and the IP address must be relocated, in which case everything works as it should for that round of data).
I'm fairly convinced this is not a problem with the Arduino sketch (or the LabVIEW code), but something deeper in the TCP protocol. However, I'm hoping someone more knowledgeable will have some advice!
Thanks in advance,
-A.
/******************************************************************************
* This sketch listens for a TCP packet from LabView containing the parameters*
* for programming the DDS's used in the experiment. *
******************************************************************************/
#include <SPI.h> //needed for Arduino versions later than 0018
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90, 0xA2, 0xDA, 0x0E, 0xCE, 0x7A }; //MAC adress
IPAddress ip(10, 10, 6, 102); //IP address must be compatible with local network
unsigned int localPort = 23; //local port to listen on
//Create a server listening on localPort
EthernetServer server(localPort);
//Define a buffer to hold the TCP packet
int maxSize=500;
int i = 0;
char packetBuffer[120];
bool gotPacket;
void setup(){
//start Ethernet and initalize the server
Ethernet.begin(mac, ip);
server.begin();
//start serial connection for debugging
Serial.begin(9600);
}
void loop() {
//if there is data available, read a packet
EthernetClient client = server.available();
i = 0;
gotPacket = false;
if(client){
while(client.connected()){
//Serial.println("Client connected.");
while(client.available()){
char c = client.read();
packetBuffer[i] = c;
i++;
gotPacket = true;
} //end client.available while-loop
//Read the packetBuffer
if(gotPacket){
//Print packetBuffer for debugging
Serial.print("Received packet of size ");
Serial.println(i);
gotPacket = false;
} //end gotClient if-statement
} //end client.connected while-loop
client.stop();
} //end client if-statement
delay(50);
} //end loop()