Hi all,
I'm trying to send multiple messages from my Arduino to a server.
In the main loop I have a for cycle that tries to send four (identical) messages, but only the first is sended because the client.connect() method returns false after the first message.
Note that I wrote my code using the example given in a previous post:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1254745295
The Arduino code:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x20, 0x45 };
byte serverIp[] = { 192, 168, 33, 17 };
byte clientIp[] = { 192, 168, 33, 16 };
Server server(10001);
Client client2(clientIp, 10002);
void setup()
{
Serial.begin(57600);
Ethernet.begin(mac, serverIp);
server.begin();
Serial.println("Server started");
}
void loop()
{
// listen for incoming clients
Client client = server.available();
if (client) {
Serial.println("Client available");
int i = 0, len = 0;
byte hbuf[22];
byte pbuf[256];
byte respHeader[] = { 0x32, 0x4F, 0x44, 0x32, 0x10, 0x9, 0x3E,
0x7E, 0x0, 0x0, 0x0, 0x30, 0x20, 0x10,
0x40, 0x50, 0x21, 0x0, 0x0, 0x51, 0x22,
0x32
};
byte respPayload[] = { 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
0x49, 0x50, 0x20, 0x21, 0x22, 0x23, 0x24,
0x25, 0x26, 0x27, 0x28, 0x29, 0x30, 0x9,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, 0x1, 0x2, 0x3, 0x4,
0x5, 0x6, 0x7, 0x8, 0x20, 0x21, 0x22,
0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
0x30
};
// lettura header
Serial.println("Reading header");
while (client.connected() && i < 22) {
if (client.available()) {
hbuf[i] = client.read();
i++;
}
}
if (i != 22) {
Serial.println("Insufficient bytes for parsing");
}
Serial.print("Header bytes: ");
for (i = 0; i < 22; i++) {
Serial.print(hbuf[i], HEX);
Serial.print(" ");
}
Serial.println();
i = 0;
len = hbuf[0]; // first header's byte
Serial.println("Reading payload");
while (client.connected() && i < len) {
if (client.available()) {
pbuf[i] = client.read();
i++;
}
}
if (i != len) {
Serial.println("Insufficient bytes for parsing");
}
Serial.print("Payload bytes: ");
for (i = 0; i < len; i++) {
Serial.print(pbuf[i], HEX);
Serial.print(" ");
}
Serial.println();
for (i = 0; i < 4; i++) {
sendResponseMessage(respHeader, respPayload);
}
delay(1);
client.stop();
}
}
void sendResponseMessage (byte *respHeader, byte *respPayload)
{
int i, len = 0;
if (client2.connect()) {
Serial.println("Connected");
Serial.println("Sending response message...");
if (respHeader) {
Serial.println("Sending the message header...");
for (i = 0; i < 22; i++) {
client2.write(* (respHeader + i));
}
Serial.println("Header sended");
}
len = respHeader[0];
if (respPayload) {
Serial.println("Sending the message payload...");
for (i = 0; i < len; i++) {
client2.write(*(respPayload + i));
}
Serial.println("Payload sended");
}
} else { // connection failed
Serial.println("Connection failed");
}
delay(300);
client2.flush();
if (!client2.connected()) { // no more connected
Serial.println();
Serial.println("Disconnecting.");
client2.stop();
// for(;;)
// ;
}
else { // disconnect anyway
client2.stop();
}
}
And this is the output:
Server started
Client available
Reading header
Header bytes: 32 4F 44 32 10 9 3E 7E 23 38 11 30 20 10 40 50 21 31 41 51 22 32
Reading payload
Payload bytes: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
Connected
Sending response message...
Sending the message header...
Header sended
Sending the message payload...
Payload sended
disconnecting.
Connection failed
disconnecting.
Connection failed
disconnecting.
Connection failed
disconnecting.