#include "ClearCore.h"
#define PORT_NUM 8888
#define MAX_PACKET_LENGTH 100
unsigned char packetReceived[MAX_PACKET_LENGTH];
char packetToSend[MAX_PACKET_LENGTH];
#define NUMBER_OF_CLIENTS 6
bool usingDhcp = true;
Connector *const outputLEDs[6] = {
};
int main(void) {
uint32_t timeout = 5000;
continue;
}
for(int i=0; i<6; i++){
outputLEDs[i]->Mode(Connector::OUTPUT_DIGITAL);
}
}
if (usingDhcp) {
if (dhcpSuccess) {
}
else {
while (true) {
continue;
}
}
} else {
IpAddress ip = IpAddress(192, 168, 0, 100);
}
EthernetTcpServer server = EthernetTcpServer(PORT_NUM);
EthernetTcpClient clients[NUMBER_OF_CLIENTS];
EthernetTcpClient tempClient;
bool newClient = 0;
server.Begin();
startTime = 0;
while(true){
tempClient = server.Accept();
if(tempClient.Connected()){
newClient = 1;
for(int i=0; i<NUMBER_OF_CLIENTS; i++){
if(!clients[i].Connected()){
clients[i].Close();
clients[i] = tempClient;
newClient = 0;
break;
}
}
if(newClient == 1){
newClient = 0;
tempClient.Send("This server has reached its max number of clients. Closing connection.");
sprintf(packetToSend, "This server has reached its max number of clients. Closing connection to (%s).", tempClient.RemoteIp().StringValue());
tempClient.Close();
}
}
for(int i=0; i<NUMBER_OF_CLIENTS; i++){
if (clients[i].Connected()) {
outputLEDs[i]->State(true);
if(clients[i].BytesAvailable()){
sprintf(packetToSend, "Read the following from the client(%s): ", clients[i].RemoteIp().StringValue());
while (clients[i].BytesAvailable()) {
clients[i].Read(packetReceived, MAX_PACKET_LENGTH);
for(int i=0; i<MAX_PACKET_LENGTH; i++){
packetReceived[i]=NULL;
}
}
sprintf(packetToSend, "Hello client %s",clients[i].RemoteIp().StringValue());
if(clients[i].Send(packetToSend) == 0){
clients[i].Close();
sprintf(packetToSend, "Client (%s) has been removed from client list. ", clients[i].RemoteIp().StringValue());
clients[i] = EthernetTcpClient();
outputLEDs[i]->State(false);
}
}
} else{
if(clients[i].RemoteIp() != IpAddress(0,0,0,0)){
sprintf(packetToSend, "Client (%s) has been removed from client list. ", clients[i].RemoteIp().StringValue());
clients[i].Close();
clients[i] = EthernetTcpClient();
outputLEDs[i]->State(false);
}
}
}
}
int delay = 5000;
for(int i=0; i<NUMBER_OF_CLIENTS; i++){
if(clients[i].RemoteIp() != IpAddress(0,0,0,0)){
sprintf(packetToSend, "Client %i = %s", i, clients[i].RemoteIp().StringValue());
}
}
}
}
}