1

I am doing a sketch using an ESP8266 as a WiFi server, I need to send a character string to the Client, for this I am using this piece of code:

  char sbuf[] = "Hello world!\n\r";
  size_t len = strlen(sbuf);
  for (i = 0; i < MAXCLIENTS; i++) {
  if (serverClients[i] && serverClients[i].connected()) {
     serverClients[i].write((char*) &sbuf, len);
     delay(1);
   }
 }

It works OK, the client can receive the characters.

But now I want to make a function to call it the times I need it, this is the function code:

void sendDataToClient( char *sbuf[]) {
size_t len = strlen(sbuf);
for (i = 0; i < MAXCLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
  serverClients[i].write((char*) &sbuf, len);
  delay(1);
  }
 }
}

That's I'm calling the function:

char bufferMSGtoClient[] = "Hello world!\n\r";

sendDataToClient(bufferMSGtoClient)

But it doesn't work, the client does not receive anything, someone can tell me what is my error?

1 Answer 1

4

sizeof doesn’t do what you think it does here. You’re getting 4 because that’s the size of a pointer. Use strlen instead. It will tell you how long up to the \n. Add two if you want the \r too.

void sendDataToClient( char *sbuf[]) {

This line is also not doing what you think. It is passing an array of pointers. You either want a pointer OR an array, but certainly not an array of pointers.

void sendDataToClient( char *sbuf) {

and

serverClients[i].write((char*) &sbuf, len);

should be

serverClients[i].write(sbuf, len);

I'm betting.

Sign up to request clarification or add additional context in comments.

2 Comments

Delta, I have made the change, I still get the value of len 4 and the client does not receive anything
You're also passing in an array of pointers. Lose the brackets on the argument sbuf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.