Byte to String Problem *not sure*

Hi there, I’ve been stuck with this problem where i can’t get the value sent by the client using TCP Protocol and pass it in a function or a variable. (UIPEthernet Library).

#include <UIPEthernet.h>

EthernetServer server = EthernetServer(1000);

void setup()
{
Serial.begin(9600);

uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
IPAddress myIP(192,168,0,6);

Ethernet.begin(mac,myIP);

server.begin();
}

void loop()
{
size_t size;

if (EthernetClient client = server.available())
{
while((size = client.available()) > 0)
{
uint8_t* msg = (uint8_t*)malloc(size);
size = client.read(msg,size);
Serial.write(msg,size); <--------- I need the value of this one.
free(msg);
}
client.println(“DATA from Server!”);
client.stop();
}
}

upon browsing the Arduino’s Documentation, i saw that

Function .write accepts this parameters:

Serial.write(buf, len)
where as:
buf: an array to send as a series of bytes
len: the length of the buffer

now, how can i convert those byte series to string?

To be honest, I’ve not done any client work at the Arduino level. However, if size is the number of bytes returned from client.read(), the bytes being read need to be in a buffer that is long enough to hold the null termination character for a C string (i.e., ‘\0’). This would mean changing the code to:

// other code...
      while((size = client.available()) > 0)
        {
          uint8_t* msg = (uint8_t*)malloc(size + 1);   // Allocate a byte for the null
          size = client.read(msg,size);
          msg[size] = '\0';                            // New statement to append null...
          Serial.write(msg,size);                       <--------- I need the value of this one.
          free(msg);
        }
// more code...

I don’t have the means to test the code, so be aware…

 if (EthernetClient client = server.available())

I rather doubt that this is valid for C/C++. What is the scope of this declaration going to be ? It looks more like java.

michinyon: if (EthernetClient client = server.available())

I rather doubt that this is valid for C/C++. What is the scope of this declaration going to be ? It looks more like java.

Nup, its perfectly fine.

Its scope is the next statement. (either a simple or compound statement. )