Hardware Reference
In-Depth Information
From here, it is possible to send and receive data using the client() object.
The server is only responsible for opening a port and accepting connections on
that port; data will be read from and written to the client object.
Servers spend most of their time waiting for connections and responding to
connections before waiting for another connection. As such, they are usually
in loop() waiting for a connection before acting. When an exchange has com-
pleted, close the connection using the stop() function.
client.stop();
To wait for connections, send data, and then close the connection, you can
use code that looks like this:
void loop()
{
EthernetClient client = server.available();
if (client == true)
{
// Client has connected, send data
client.println("Hello, client!");
client.stop();
}
}
Serving Web Pages
Web servers are the most visible ways of connecting to an Arduino over a net-
work to get data and also great fun! They can be seen on computers, tablets,
and mobile telephones and can easily be tweaked to produce some visually
stunning interfaces.
When a web browser connects to a web server, it expects some specii c infor-
mation. It not only just receives a web page, but also some headers that you do
not normally see. The server informs the web browser if the page is accessible
(remember those 404-error messages you see from time to time?), the sort of data
that is to be sent, and the connection status after the data has been delivered.
Additional headers can be added if needed.
A typical exchange might look like this:
HTTP/1.1 200 OK
Content-Type: text/html
Connection: close
The 200 return code means that the page was found and is available. The
content type of this page is HTML , sent as text data. Finally, the connection will
be closed after the page has been sent. If the web browser wants another page,
 
Search WWH ::




Custom Search