Go Down

Topic: Ethernet shield server.write vs server.print (Read 2049 times) previous topic - next topic

tomroth

What is the difference between server.write() and server.print()?

This is pretty clearly documented.

http://arduino.cc/en/Reference/ServerWrite

write()
server.write(data)
data: the value to write (byte or char)

http://arduino.cc/en/Reference/ServerPrint

print()
server.print(data)
server.print(data, BASE)
data: the data to print (char, byte, int, long, or string)

tomroth

The documentation is clear as mud, in this instance (really good overall!!!). The only difference I see is that you can use the optional 'BASE' in server.print. Also, using server.print, it seems I have more flexibility as to what 'DATA' can hold. I've read the ethernet reference, several times. The reason I asked the question is because server.write and server.print seem to be almost interchangeable. Am I missing something, or is that pretty much correct?

Thanks.

zoomkat

Quote
Print data to all the clients connected to a server. Prints numbers as a sequence of digits, each an ASCII character (e.g. the number 123 is sent as the three characters '1', '2', '3').


Sounds like the print function can take a single byte and convert it into various multi character formats.
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Just always use print, and it will do what you expect.  They should have hidden write().  If you call print(32), you will get '32'.  If you call write(32) you will get a space (' ') and be confused.

PaulS

Quote
They should have hidden write()

Since the Client class derives from Stream, which derives from Print, and the Server class derives from Print, this would have been impossible, since both print() and write() are public methods in the Print class. Serial also derives from Print, and in Serial you most definitely want to be able to send both ASCII ("32") and binary (32) data.

It would have taken a little bit of work, but definitely possible.  Server could have derived privately from Print and then thoughtfully exposed only the interface that's needed to users.

Go Up