Is it possible to have more than one client on a server chat if so, how?


hello everyone,

in server chat example code wifi shield, description shows how client can communicate chat server shield creates on telnet. however, when added second client on server second client not able connect. used following code example , modified have 2 clients.

code: [select]

#include <spi.h>
#include <wifi.h>

char ssid[] = "myqwest2629"; //  network ssid (name) sa
char pass[] = "****************";    // network password (use wpa, or use key wep)

int keyindex = 0;            // network key index number (needed wep)

int status = wl_idle_status;

wifiserver server(23);

boolean alreadyconnected = false; // whether or not client connected previously

void setup() {
  //initialize serial , wait port open:
  serial.begin(9600);
  while (!serial) {
    ; // wait serial port connect. needed leonardo only
  }
 
  // check presence of shield:
  if (wifi.status() == wl_no_shield) {
    serial.println("wifi shield not present");
    // don't continue:
    while(true);
  } 
 
  // attempt connect wifi network:
  while (status != wl_connected) {
    serial.print("attempting connect ssid: ");
    serial.println(ssid);
    // connect wpa/wpa2 network. change line if using open or wep network:   
    status = wifi.begin(ssid, pass);

    // wait 10 seconds connection:
    delay(10000);
  }
  // start server:
  server.begin();
  // you're connected now, print out status:
  printwifistatus();
 }


void loop() {
  // first client:
  wificlient client = server.available();
 
  //second client:
  wificlient client_2 = server.available();


  // when client sends first byte, hello:
  if (client) {
    if (!alreadyconnected) {
      // clead out input buffer:
      client.flush();   
      serial.println("we have new client");
      client.println("hello, client!");
      alreadyconnected = true;
    }

    if (client.available() > 0) {
      // read bytes incoming client:
      char thischar = client.read();
      // echo bytes client:
      server.write(thischar);
      // echo bytes server well:
      serial.write(thischar);
    }
  }
 
  // second client
  if (client_2) {
    if (!alreadyconnected) {
      // clead out input buffer:
      client_2.flush();   
      serial.println("we have new client");
      client_2.println("hello, client!");
      alreadyconnected = true;
    }

    if (client_2.available() > 0) {
      // read bytes incoming client:
      char thischar_1 = client_2.read();
      // echo bytes client:
      server.write(thischar_1);
      // echo bytes server well:
      serial.write(thischar_1);
    }
  }
}


void printwifistatus() {
  // print ssid of network you're attached to:
  serial.print("ssid: ");
  serial.println(wifi.ssid());

  // print wifi shield's ip address:
  ipaddress ip = wifi.localip();
  serial.print("ip address: ");
  serial.println(ip);

  // print received signal strength:
  long rssi = wifi.rssi();
  serial.print("signal strength (rssi):");
  serial.print(rssi);
  serial.println(" dbm");
}


i'm not sure if code written incorrectly or physically not possible. suggestions help.

thank you!!!

the standard chat server example should able support more 1 client (up 4), although never tested it. code doesn't make sense second client (client_2) active in rare event both clients connect @ same time.


Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Is it possible to have more than one client on a server chat if so, how?


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code