[SOLVED]Arduino as a SERVER TCP. How to Send packets?

Hello

I have an Arduino DUE+ IoShield A (W5500 from Wiznet). IDE 1.6. Wiznet Ethernet library

I want the Arduino to behave as a TCP Server. This server waits for a client, and sends the client an info packet.

The code is divided in two files, one the server code, and other a .h with the struct of the packet

Server.

#include "packet.h"
#include <Ethernet.h>


 // the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0x00, 0x08, 0xDC, 0x4D, 0xD1, 0x55 };  
//the IP address for the shield:
byte ip[] = { 10, 1, 1, 3 };    
// the router's gateway address:
byte gateway[] = { 10, 1, 1, 1 };//? Esto no esun gateway, es un nodo.
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
int Port= 5000;

EthernetServer server = EthernetServer(Port);



void setup()
{
  // initialize the ethernet device
  Ethernet.begin(mac, ip, gateway, subnet);

  // start listening for clients
  server.begin();

  Info.PacketID= 0x0E;
  Info.ID_track=1;
  Info.Direction=0x00;
  Info.num=10;
  Info.stateA[0]=0x00;
  Info.stateA[1]=0x00;
  Info.stateA[2]=0x00;
  Info.stateA[3]=0x00;


void loop()
{
  // if an incoming client connects, there will be bytes available to read:
  EthernetClient client = server.available();
  if (client) {

    server.write(Info,sizeof(Info));
  }
  
}

packet.h

struct Packet{
  byte PacketID;
  byte ID_track;
  byte Direction;
  byte num;
  byte stateA[4];

};


struct Packet Info;

It gives error in server.write() , no matching function for call... I think that it is beacuse Info is not only an array of bytes but a struct... but I'm not sure

Any help about the server and how to send a data packet structure is welcome.
Thanks

It is client.write(), not server.write(). Something like this:

client.write(Packet, sizeof(Packet));

The Ethernet library included in the IDE is for a WizNet 5100 chip. I'm not sure if the WizNet 5500 is compatible enough to work with the unchanged library. As far as I know there is a Ethernet2 library for that chip.

Thanks for the answers,
I'm using Wiznet Ethernet Library.

Using client.write() returns the same error

Try using a override.

client.write((byte*)info, sizeof(info));

I tried using

client.write((byte*)info, sizeof(info));

The error now changes to: invalid cast from type ...

My bad. Use the "address of info" (ampersand).

client.write((byte*)&info, sizeof(info));

SurferTim:
My bad. Use the "address of info" (ampersand).

client.write((byte*)&info, sizeof(info));

I hate that error, I have to force myself to actually read my code aloud.

And say, "pointer to address of" or "pointer to value of"

Thank you! It worked!

1 Like