Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. /*
  2. Advanced Chat Server
  3.  
  4. A more advanced server that distributes any incoming messages
  5. to all connected clients but the client the message comes from.
  6. To use, telnet to your device's IP address and type.
  7. You can see the client's input in the serial monitor as well.
  8. Using an Arduino Wiznet Ethernet shield.
  9.  
  10. Circuit:
  11. * Ethernet shield attached to pins 10, 11, 12, 13
  12.  
  13. created 18 Dec 2009
  14. by David A. Mellis
  15. modified 9 Apr 2012
  16. by Tom Igoe
  17. redesigned to make use of operator== 25 Nov 2013
  18. by Norbert Truchsess
  19.  
  20. */
  21.  
  22. #include <SPI.h>
  23. #include <Ethernet.h>
  24.  
  25. // Enter a MAC address and IP address for your controller below.
  26. // The IP address will be dependent on your local network.
  27. // gateway and subnet are optional:
  28. byte mac[] = {
  29. 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  30. };
  31. IPAddress ip(192, 168, 1, 177);
  32. IPAddress myDns(192, 168, 1, 1);
  33. IPAddress gateway(192, 168, 1, 1);
  34. IPAddress subnet(255, 255, 0, 0);
  35.  
  36.  
  37. // telnet defaults to port 23
  38. EthernetServer server(23);
  39.  
  40. EthernetClient clients[8];
  41.  
  42. void setup() {
  43. // You can use Ethernet.init(pin) to configure the CS pin
  44. //Ethernet.init(10); // Most Arduino shields
  45. //Ethernet.init(5); // MKR ETH shield
  46. //Ethernet.init(0); // Teensy 2.0
  47. //Ethernet.init(20); // Teensy++ 2.0
  48. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  49. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  50.  
  51. // initialize the Ethernet device
  52. Ethernet.begin(mac, ip, myDns, gateway, subnet);
  53.  
  54. // Open serial communications and wait for port to open:
  55. Serial.begin(9600);
  56. while (!Serial) {
  57. ; // wait for serial port to connect. Needed for native USB port only
  58. }
  59.  
  60. // Check for Ethernet hardware present
  61. if (Ethernet.hardwareStatus() == EthernetNoHardware) {
  62. Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
  63. while (true) {
  64. delay(1); // do nothing, no point running without Ethernet hardware
  65. }
  66. }
  67. if (Ethernet.linkStatus() == LinkOFF) {
  68. Serial.println("Ethernet cable is not connected.");
  69. }
  70.  
  71. // start listening for clients
  72. server.begin();
  73.  
  74. Serial.print("Chat server address:");
  75. Serial.println(Ethernet.localIP());
  76. }
  77.  
  78. void loop() {
  79. // check for any new client connecting, and say hello (before any incoming data)
  80. EthernetClient newClient = server.accept();
  81. if (newClient) {
  82. for (byte i=0; i < 8; i++) {
  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;
  91. break;
  92. }
  93. }
  94. }
  95.  
  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].connected()) {
  105. clients[j].write(buffer, count);
  106. }
  107. }
  108. }
  109. }
  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);
  116. clients[i].stop();
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement