Send byte array over ethernet with Ethernet Shield

Hello,

I today started playing around with a Arduino Mega + Ethernet Shield (Hacked) and I am now trying to send a byte array to a server. This server is a gaming interface that runs on my computer. My problem now is the following:

I need to send a string in Intel byte order (reverse) to this server. The byte array is as follows:
00 01 01 02 08 46 61 68 72 70 75 6C 74
however when I try to send the array using client.println() and monitor it using Serial.write() I get the following output: tluprhaF which doesn't correspond with the byte array I'm trying to send. It doesn't send the last byte:

With Wireshark i get for the data only 12 bytes: 74:6c:75:70:72:68:61:46:08:02:01:01 it misses the 0x00.

Any help would be much appriciated. :slight_smile:

Below the code I am using:

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 45 }; // ip
byte server[] = { 192, 168, 1, 100 }; // the server
char helloString2[] = {0x74,0x6C,0x75,0x70,0x72,0x68,0x61,0x46,0x08,0x02,0x01,0x01,0x00}; // the array I try to send
Client client(server, 1435);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);

Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
client.write(helloString2);
Serial.write(helloString2);
} else {
Serial.println("connection failed");
}
}

void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}

The client::print and Serial::print functions for printing strings stop transmitting the bytes when they encounter a NULL. After all, the definition of a string is a NULL-terminated array of characters.

An array of characters with an embedded null, which you have, is going to need to be output one byte at a time, using a for loop rather than explicitly stopping on the null.

Just a hunch, but does it think that the 0x00 is the NULL terminator, and therefor stops transmitting at the last 0x01? Try changing it to 0x05, and try to add a 0x05 after the 0x00. Experimenting around like that will help you develop a theory as to why it doesn't work.

Or dive into the libraries to see exactly the inner workings of the routines, and figure it out that way.

Good luck!

EDIT: PaulS beat me to it :slight_smile:

Thanks for all the information, I think I've come a little further now. :slight_smile:

Although now I run into the following problem, im trying to send a byte array of 45 bytes but the write function only sends the first 16, I have no idea as to why. The code is now as follows (the array to send is helloNieuw:

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 45 }; // Change this parameters to reflect your network values
byte server[] = { 192, 168, 1, 100 }; // Google

byte helloNieuw[45] = {0x29,0x00,0x00,0x00,0x00,0x01,0x02,0x06,0x24,0x5a,0x44,0x20,0x42,0x75,0x73,0x2d,0x47,0x65,0x72,0x61,0x65,0x74,0x20,0x32,0x2e,0x33,0x2e,0x36,0x2e,0x33,0x34,0x36,0x34,0x34,0x20,0x28,0x49,0x50,0x76,0x34,0x2b,0x54,0x43,0x50,0x29};
Client client(server, 1435);

void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);

Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
client.write(helloNieuw,HEX);
} else {
Serial.println("connection failed");
}
}

void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}

Change:

client.write(helloNieuw,HEX);

to

client.write(helloNieuw);

Just a guess... :slight_smile:

Hello, thanks for the replay! I tried it without the HEX behind the variable but when I do that I get a compiler error:

 In function 'void setup()':
error: call of overloaded 'write(byte [45])' is ambiguousC:\arduino-0018\libraries\Ethernet/Client.h:17: note: candidates are: virtual void Client::write(uint8_t) <near match>

C:\arduino-0018\libraries\Ethernet/Client.h:18: note:                 virtual void Client::write(const char*) <near match>

I have no idea as to what the compiler means with "ambiguous" :-/.

The Client::write function has several definitions. The compiler is telling you that there are at least two of the write methods that could be invoked by the argument type that you have provided, and that it doesn't know which one you want to call.

You need to help it out by making it explicit which one you want to use.

The argument type you have supplied to the call is an array of bytes. There is no overloaded method that is defined with this argument type. There are implicit casts that the compiler can make, when there is no loss of accuracy. Any of the other argument types that are the same size as byte could be used - uint_8 or char.

If you change the byte type to uint_8, the compiler will know which method to actually call, since there is one that does not require an implicit cast.

We have cracked the problem, Like you suggested I dived into print.h file and saw that define HEX was 16 so instead of using client.write(helloNieuw,HEX); I typed client.write(helloNieuw,45); and the server responded with an acknoledge command :slight_smile:
So I needed to specify the array length as second argument (Didn't know at first what the HEX meant):

uint8_t helloNieuw[] = {0x29,0x00,0x00,0x00,0x00,0x01,0x02,0x06,0x24,0x5a,0x44,0x20,0x42,0x75,0x73,0x2d,0x47,0x65,0x72,0x61,0x65,0x74,0x20,0x32,0x2e,0x33,0x2e,0x36,0x2e,0x33,0x34,0x36,0x34,0x34,0x20,0x28,0x49,0x50,0x76,0x34,0x2b,0x54,0x43,0x50,0x29};

client.write(helloNieuw,45);

Thanx for the support!

Oh that works! Sorry, I should have told you that HEX was 16, which is why I suspected it. Thought you might want to have a dynamic routine there so taking out a limiting value (the HEX) was my first attempt :wink:

This ethernet stuff looks really straight forward, once you get around these little problems. I just wish the Ethernet Shield wasn't so cost prohibitive.