|
28 | 28 | byte mac[] = { |
29 | 29 | 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED |
30 | 30 | }; |
31 | | -IPAddress ip(192, 168, 1, 177); |
| 31 | +IPAddress ip(192, 168, 194, 177); |
32 | 32 | IPAddress myDns(192, 168, 1, 1); |
33 | 33 | IPAddress gateway(192, 168, 1, 1); |
34 | 34 | IPAddress subnet(255, 255, 0, 0); |
@@ -76,53 +76,43 @@ void setup() { |
76 | 76 | } |
77 | 77 |
|
78 | 78 | void loop() { |
79 | | - // wait for a new client: |
80 | | - EthernetClient client = server.available(); |
81 | | - |
82 | | - // when the client sends the first byte, say hello: |
83 | | - if (client) { |
84 | | - |
85 | | - boolean newClient = true; |
| 79 | + // check for any new client connecting, and say hello (before any incoming data) |
| 80 | + EthernetClient newClient = server.accept(); |
| 81 | + if (newClient) { |
86 | 82 | for (byte i=0; i < 8; i++) { |
87 | | - //check whether this client refers to the same socket as one of the existing instances: |
88 | | - if (clients[i] == client) { |
89 | | - newClient = false; |
| 83 | + if (!clients[i]) { |
| 84 | + Serial.print("We have a new client #"); |
| 85 | + Serial.println(i); |
| 86 | + newClient.print("Hello, client number: "); |
| 87 | + newClient.println(i); |
| 88 | + // Once we "accept", the client is no longer tracked by EthernetServer |
| 89 | + // so we must store it into our list of clients |
| 90 | + clients[i] = newClient; |
90 | 91 | break; |
91 | 92 | } |
92 | 93 | } |
| 94 | + } |
93 | 95 |
|
94 | | - if (newClient) { |
95 | | - //check which of the existing clients can be overridden: |
96 | | - for (byte i=0; i < 8; i++) { |
97 | | - if (!clients[i] && clients[i] != client) { |
98 | | - clients[i] = client; |
99 | | - // clear out the input buffer: |
100 | | - client.flush(); |
101 | | - Serial.println("We have a new client"); |
102 | | - client.print("Hello, client number: "); |
103 | | - client.print(i); |
104 | | - client.println(); |
105 | | - break; |
106 | | - } |
107 | | - } |
108 | | - } |
109 | | - |
110 | | - if (client.available() > 0) { |
111 | | - // read the bytes incoming from the client: |
112 | | - char thisChar = client.read(); |
113 | | - // echo the bytes back to all other connected clients: |
114 | | - for (byte i=0; i < 8; i++) { |
115 | | - if (clients[i] && (clients[i] != client)) { |
116 | | - clients[i].write(thisChar); |
| 96 | + // check for incoming data from all clients |
| 97 | + for (byte i=0; i < 8; i++) { |
| 98 | + if (clients[i] && clients[i].available() > 0) { |
| 99 | + // read bytes from a client |
| 100 | + byte buffer[80]; |
| 101 | + int count = clients[i].read(buffer, 80); |
| 102 | + // write the bytes to all other connected clients |
| 103 | + for (byte j=0; j < 8; j++) { |
| 104 | + if (j != i && clients[j]) { |
| 105 | + clients[j].write(buffer, count); |
117 | 106 | } |
118 | 107 | } |
119 | | - // echo the bytes to the server as well: |
120 | | - Serial.write(thisChar); |
121 | 108 | } |
122 | 109 | } |
123 | | - for (byte i=0; i < 8 ; i++) { |
124 | | - if (!(clients[i].connected())) { |
125 | | - // client.stop() invalidates the internal socket-descriptor, so next use of == will allways return false; |
| 110 | + |
| 111 | + // stop any clients which disconnect |
| 112 | + for (byte i=0; i < 8; i++) { |
| 113 | + if (clients[i] && !clients[i].connected()) { |
| 114 | + Serial.print("disconnect client #"); |
| 115 | + Serial.println(i); |
126 | 116 | clients[i].stop(); |
127 | 117 | } |
128 | 118 | } |
|
0 commit comments