[MUSIC] So we're talking about how an e Ethernet shield can act as client. Now let's talk about how it can act as a server. It can also act as a server. Remember that the goal of a server is to sit listen for connection from clients, for requests from clients. So it's gotta sit at some IP address, listen to a port, and when the request comes in on that port, it services the request, so it receives data, or whatever the request is, and then sends data satisfying the request. So, the Ethernet shield can act as a server, too, so let's look at how that works. First thing you want to do is create a server object, so remember with the client, we had the Ethernet client object, so, now we've got Ethernet server, we create a server object. And in order to make that server, you've gotta explicitly call EthernetServer. A function called EthernetServer which is its constructor. And you pass an argument called that is the port. So this creates a server. So, server is now equal to a server object that is listening on whatever port it is that you specified, right? So if you're making a web server you would listen on port 80 and so forth. To start listening, we have to create a client object. Now this sounds a little counter intuitive. You might say well look, I'm a server, why do I need to create a client object? But you need to create a client object because that client object is gonna represent the client that you're talking too, that you're communicating with. So you say ethernet client client = server.available(). So you're calling this function server.available() and that returns a client if a client is waiting, okay? If a client is waiting to talk to the server then server.available() will return that client and so now client will equal whatever server it is you want to talk to. The server data available will return false if there's no client available. But if there is a client available, then client's gonna equal [COUGH] a client object which refers to the client you are talking to, not yourself, but the one you're talking to. And then client.stop can be called from the server to cancel the communication with the clients, to end it. Now in order to send data to the client and receive data from the client you just call the same functions we called before, client.print and client.write. Client.print will send the data, client.write will send bytes and individual raw data. And you can do client.read to read data. Use client dot read to read data. So you can send and receive data, communicate with the client using client.print or client.read or also client.print line if you want to put a character in there or client.read does the reading. So here's a little example of a maybe a little silly piece of code but that's okay it fit on the slide. So we started off at the top. Make a server. Ethernet server, we create the server, we call the Ethernet server with an 80 as its argument. So we're creating a server that's gonna be listening to port 80. Now, in our setup, we call Ethernet.begin. You gotta call Ethernet.begin to begin to initialize the server, to initialize the Ethernet shield. Now notice that this time we pass it a MAC address, I don't know, I didn't define them in here. But suppose we had some MAC address. This time we give it an IP address, a gateway, and a subnet. The IP address is the most important here. We gotta give it an IP address because now we're acting as a server, right? And typically when you act as a server, you need your IP address to be static, to be known. Advertised, right? And using, so when somebody does a domain saver, DNS look up for your name, they've got this consistent IP address so they can find you. So, it is common when you're creating a server that you don't rely on DHCP to give you an IP address. You specify an IP address. Now I don't know what that IP address is but we could have defined it to be whatever we want. But we have to give it a MAC address and we also give it an IP address. Then we also give it the optional gateway and subnet, but that's not necessary. Then we call server.begin. server.begin actually evokes the server and starts it listening. Now [COUGH] then in the loop, we say, okay, let's see if there's a client that wants to connect to us. Right? So we call server.available to return an EthernetClient If a client is available. So server.available will return 0 if a client's not available. And then when we get to the next line, if client it will be false and we won't do anything. But if client is available then client's going to equal that client. Equal an object which refers to that client. So then we get to the line if client. We say okay, if there is a client that was ready to talk to us, then serial.print client.read. So we just read something from the client and print it to the serial monitor and that's that. So that's all this does is it connects to a client waiting and wherever a client sends it, it prints the serial monitor assuming the client is sending something. Now, by the way we could write this code, but to be safer, a better way to write this code is, and that, before we do the serial.print client.read, before we call client.read, we shouldn't call client.read before we know the date is available. So we should check for availability first before we call client.read. I didn't do it here, but probably should have. So it receives data on port 80 and just prints it. Thank you. [MUSIC]