PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

AdvancedChatServer.ino 3.4KB

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Advanced Chat Server
  3. A more advanced server that distributes any incoming messages
  4. to all connected clients but the client the message comes from.
  5. To use, telnet to your device's IP address and type.
  6. You can see the client's input in the serial monitor as well.
  7. Using an Arduino Wiznet Ethernet shield.
  8. Circuit:
  9. * Ethernet shield attached to pins 10, 11, 12, 13
  10. created 18 Dec 2009
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe
  14. redesigned to make use of operator== 25 Nov 2013
  15. by Norbert Truchsess
  16. */
  17. #include <SPI.h>
  18. #include <NativeEthernet.h>
  19. // Enter a MAC address and IP address for your controller below.
  20. // The IP address will be dependent on your local network.
  21. // gateway and subnet are optional:
  22. byte mac[] = {
  23. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  24. };
  25. IPAddress ip(192, 168, 1, 177);
  26. IPAddress myDns(192, 168, 1, 1);
  27. IPAddress gateway(192, 168, 1, 1);
  28. IPAddress subnet(255, 255, 0, 0);
  29. // telnet defaults to port 23
  30. EthernetServer server(23);
  31. EthernetClient clients[8];
  32. void setup() {
  33. // You can use Ethernet.init(pin) to configure the CS pin
  34. //Ethernet.init(10); // Most Arduino shields
  35. //Ethernet.init(5); // MKR ETH shield
  36. //Ethernet.init(0); // Teensy 2.0
  37. //Ethernet.init(20); // Teensy++ 2.0
  38. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  39. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  40. // initialize the Ethernet device
  41. Ethernet.begin(mac, ip, myDns, gateway, subnet);
  42. // Open serial communications and wait for port to open:
  43. Serial.begin(9600);
  44. while (!Serial) {
  45. ; // wait for serial port to connect. Needed for native USB port only
  46. }
  47. // Check for Ethernet hardware present
  48. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  49. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  50. while (true) {
  51. delay(1); // do nothing, no point running without Ethernet hardware
  52. }
  53. }
  54. if (Ethernet.linkStatus() == LinkOFF) {
  55. Serial.println("Ethernet cable is not connected.");
  56. }
  57. // start listening for clients
  58. server.begin();
  59. Serial.print("Chat server address:");
  60. Serial.println(Ethernet.localIP());
  61. }
  62. void loop() {
  63. // check for any new client connecting, and say hello (before any incoming data)
  64. EthernetClient newClient = server.accept();
  65. if (newClient) {
  66. for (byte i=0; i < 8; i++) {
  67. if (!clients[i]) {
  68. Serial.print("We have a new client #");
  69. Serial.println(i);
  70. newClient.print("Hello, client number: ");
  71. newClient.println(i);
  72. // Once we "accept", the client is no longer tracked by EthernetServer
  73. // so we must store it into our list of clients
  74. clients[i] = newClient;
  75. break;
  76. }
  77. }
  78. }
  79. // check for incoming data from all clients
  80. for (byte i=0; i < 8; i++) {
  81. if (clients[i] && clients[i].available() > 0) {
  82. // read bytes from a client
  83. byte buffer[80];
  84. int count = clients[i].read(buffer, 80);
  85. // write the bytes to all other connected clients
  86. for (byte j=0; j < 8; j++) {
  87. if (j != i && clients[j].connected()) {
  88. clients[j].write(buffer, count);
  89. }
  90. }
  91. }
  92. }
  93. // stop any clients which disconnect
  94. for (byte i=0; i < 8; i++) {
  95. if (clients[i] && !clients[i].connected()) {
  96. Serial.print("disconnect client #");
  97. Serial.println(i);
  98. clients[i].stop();
  99. }
  100. }
  101. }