client.print() vs. server.print()

I've read through the documentation on client.print and server.print and I can't determine when you'd want to use one over the other. What exactly is the difference between these two?

I drafted this bit of code to try and see what the difference would be, but when it's running I see both the client println and the server println show up in the browser window.

void loop() { EthernetClient client = server.available();

Serial.println("Beginning test sequence..."); client.println("This is a client print."); server.println("This is a server print.");

delay(5000); }

Can someone help clarify this for me please? Possibly with examples of when you'd use one instead of the other?

Please post the whole program.

EthernetClient client = server.available();

   Serial.println("Beginning test sequence...");
   client.println("This is a client print.");
   server.println("This is a server print.");

Get a client. Send some data to the serial port. Send some data to the client EVEN IF YOU DIDN'T GET A CLIENT. Send some data to the server. Even though the Arduino IS the server.

Can you explain what you think you are doing?

client.println() sends a message to only the specific client retrieved with server.available(). http://www.arduino.cc/en/Reference/ClientPrintln

server.println() sends a message to all clients connected to the server. http://www.arduino.cc/en/Reference/ServerPrintln

Here is the full program.

 #include <Ethernet.h>
 
 byte mac[] = {0x9F, 0xA0, 0xD1, 0xAD, 0x43, 0x19};
 IPAddress ip(192,168,1,255);
 
 EthernetServer server(80);
 byte data;
 
 void setup() {
   Ethernet.begin(mac, ip);
   server.begin();
   Serial.begin(9600);
   Serial.print("server is at ");
   Serial.println(Ethernet.localIP());
 }
 
 void loop()
 {
   EthernetClient client = server.available();

   Serial.println("Beginning test sequence...");
   client.println("This is a client print.");
   server.println("This is a server print.");
 
   delay(5000);  
 }

This code isn’t trying to do anything in particular - I was just trying to iron out the differences between the two commands. I’m going to be writing code for an arduino server to host a simple web page, and I was unclear if I should be using server.print or client.print.

Just to clarify - if I do a client.print, the arduino will be sending out strings or data regardless of if there’s a computer on the other end to see it?

This code isn't trying to do anything in particular

Then you needn't do anything in particular.

That depends on what you want it to do. If you want it to send a client a response to a message (request) the client has sent, use client.print.

If you want it to send a message to all clients connected to the server whether they have sent anything or not, use server.print. However, there is no way of telling if there are any clients connected at all unless one sends something, or you use more basic functions to determine if any clients are connected.

In your code above, server.available() returns a client that has sent a request.

Just to clarify - if I do a client.print, the arduino will be sending out strings or data regardless of if there's a computer on the other end to see it?

No. A client.print() is to send data to a client. If there is no client, there is no instance for the print() method to act on.

Excellent! Thanks for all of the replies, I think I’m starting to understand it now. :slight_smile:

Hello

I have a quiestion

client.print(), This command prints the serial monitor Arduino IDE.

I have used in the Intel Galileo gen 2 but do not print anything.

Somebody knows why not print?

I want to get the code php of a small website and watch this in the serial monitor Arduino IDE.

code:

#include <Ethernet.h>

byte mac = { 0x98, 0x4F, 0xEE, 0x01, 0x6A, 0xF3 }; // mac intel galileo
// change to your network settings
IPAddress ip(192,168,0,33); // ip intel galileo
IPAddress gateway(192, 168, 0, 1); // gateway lan
IPAddress subnet(255, 255, 255, 0);

IPAddress server(192,168,0,15); // ip webserver
//char serverName = “www.localhost.com”;
int serverPort = 80; //port
EthernetClient client;

void setup() {
Serial.begin(9600);
delay(2000);
Serial.println(“Starting ethernet…”);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(“Ready”);

if (client.connect(server, 80)) {
Serial.println(“connected”); // work ok
// Make a HTTP request:
client.println(“GET /Paginaweb.php”);
//client.println(“Get 192,168,0,15/Paginaweb.php”);
//client.println(“Host: www.google.com”);
//client.println(“Connection: close”);
//client.println();
}
else {
// kf you didn’t get a connection to the server:
Serial.println(“connection failed”);
}

}

void loop() {
Serial.println("…");
client.println(“GET /Paginaweb.php”);
delay(5000);

}

client.print(), This command prints the serial monitor Arduino IDE.

Normally you would use Serial.print() to output to the Serial monitor unless the Galileo is very different.