Server/Client Confusion

I have a need to send largish amounts of data from a Photon to my computer, and I assumed that using TCP would be the way to go. I modified some code I found on an Arduino site that works fine, but I clearly have some misunderstanding of what a sever and a client are, since I don’t understand why the code works. I thought that it should be the server that would read the incoming URL request (from an OS X app), and the server that would then write out data to the computer. However, in the code below, you can see that it is the client that does all the reading and writing.

TCPServer server = TCPServer(80);
TCPClient client;
byte data[] = {131,260,37,41,152,163,77,18,99,100,214}; // dummy data for a test

void setup() {
  // start listening for clients
  while(!Serial.available()) Particle.process();

void loop() {
  // listen for incoming clients
  client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
        if (client.available()) {
            char c =;
            // if you've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so you can send a reply
            if (c == '\n' && currentLineIsBlank) {
                client.println("HTTP/1.1 200 OK");
                client.println("Connection: close");  // the connection will be closed after completion of the response
                for (int i=0; i< sizeof(data)/sizeof(data[0]); i++) {
                    client.printf("%d ", data[i]);
            if (c == '\n') {
                // you're starting a new line
                currentLineIsBlank = true;
            } else if (c != '\r') {
                // you've gotten a character on the current line
                currentLineIsBlank = false;

    Serial.println("client disconnected");

So why isn’t the server doing the reading and writing? What is the meaning of “client” that’s returned from server.client()? Also, a more general question; is using TCP the best way to send data from a Photon to my computer for data that will be in the 1000 byte range?

Hi @Ric, I think the confusion comes from the fact that the TCPClient is used in two roles: If your program is connecting to a remote TCP server, then you use the TCPClient to connect to the server and use it send data to the server and receive data from the server. If your program is the server then each incoming connection is represented by … a TCPClient. If you replace in your brain the term “TCPClient” with “TCPConnection” then I think things should be clearer. The TCPClient just represents an individual TCP connection. While the server is just accepting incoming connections and creating the necessary TCPClient (aka TCPConnection) object. Hope that removes confusion and does not add to it :smile: .

TCP is fine for the data.

Also, TCP is bidirectional. TCPClient means the Particle is making a connection to another server. TCPServer allows another computer to make a connection to the Photon. But once connected, data can be transferred in either direction, or both, depending on what your code does.

Thanks @Stevie , I think that gets me most of the way there. My mental construct was that the Photon was the server, and the computer was the client, so it made no sense for the client to be sending out data from the Photon. Thinking of the client as the connection makes a lot more sense. In OS X or iOS, there is an object called an NSURLConnection, which sends out requests and receives the result, so the notion that the client is a connection ties in with that concept I’m already very familiar with.

Okay, great to hear!