Pages: [1]   Go Down
Author Topic: Ethernet shield server.write vs server.print  (Read 1809 times)
0 Members and 1 Guest are viewing this topic.
Howell, Mi
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged


Howell, Mi
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9541
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49040
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA
Offline Offline
God Member
*****
Karma: 11
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: