繁体   English   中英

Arduino:打开和关闭 ethe.net 服务器可用性

[英]Arduino: turn on and off ethernet server availability

我需要设置一个必须根据 Arduino UNO 上的某些条件打开和关闭的 ethe.net(网络)服务器。

我在Ethe.net中阅读了Server class 的文档,似乎一旦启动就没有机会停止服务器,即没有Ethe.netServer.begin()对应项。

然后我想在setup部分设置服务器并根据给定条件为传入连接提供服务:

EthernetServer server = EthernetServer(80);

void setup() {
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop() {

  if (condition) {

    EthernetClient client = server.available();
    if (client == true) {
      // serve the client...
    }

  } else {
    // do something else
  }

}

这确实有效,但客户没有被正确拒绝:它只是悬而未决。 在浏览器中,可以看到 web 页面无限加载,如果条件变为true ,客户端最终将为条件为false时发出的请求提供服务。

我看不到拒绝请求的方法(没有对应的Ethe.netServer.available() )。 我唯一想到的就是执行一个

server.available().stop();

在 else 块的开头。 这可以防止在条件为false时发出请求,但不会阻止客户端和服务器之间的连接发生(这就像打开连接并立即关闭它一样)。

当条件为false时,我怎样才能完全避免建立连接?

我猜这里是因为我没有方便的Arduino集合,但是从内存和阅读参考中,您可以尝试类似

void loop() 
{
    EthernetClient client = server.available();
    if ( !condition )
    {
        client.stop(); // break connection and do something else
    }
    else
    {
        // serve the client...
    }
}

希望至少可以在正确的方向上为您提供帮助。

干杯,

要禁用服务器时,是否可以仅返回404标头?

if(!condition)
{
     client.println("HTTP/1.1 404 OK");
     client.println("Content-Type: text/html");
     client.println("Connnection: close");
     client.println();
     client.println("<!DOCTYPE HTML>");
     client.println("<html><body>404</body></html>");
}
else
{
     // serve client
}

我在这里写下这个答案,因为它是唯一仍然活跃或尚未关闭的关于该主题的帖子。 尽管有无数关于能够随意打开或关闭 Ethe.netServer 的研究,但这是不可能的。 您唯一可以做的就是使用一些在 Ethe.net/W5100/W5200/W5500 库的类中定义为“公共”的函数。 我注意到实际影响网卡可靠性的功能是:

#include <Ethernet.h>
#include <utility/w5100.h>
W5100.setRetransmissionTime(milliseconds);
W5100.setRetransmissionCount(number);

(有助于缩短Wi.net W5100/W5200/W5500.网卡超时时的等待时间)

EthernetClient::setConnectionTimeout(CONNECTION_TIMEOUT);
EthernetClient::setTimeout(CONNECTION_INPUT_STREAMING_TIMEOUT);

(它们有助于缩短连接到 Ethe.netServer 的客户端超时时的等待时间)

更多提示:

  • Ethe.netServer::available()返回false时考虑使用Ethe.netServer::flush()刷新服务器缓冲区;
  • 使用Ethe.netClient::write()时也使用Ethe.netClient::flush()以确保所有数据已发送;
  • 在死机/无用客户端上使用Ethe.netClient::close()轻松释放 sockets。

考虑实现一个 function 到 force-close.network sockets,使用以下代码:

#include <SPI.h>
#include <utility/w5100.h>
void closeAllSockets()
{
     for (int i = 0; i < MAX_SOCK_NUM; i++)
     {
         SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
         W5100.execCmdSn(i, Sock_CLOSE);
         SPI.endTransaction();
     }
}

void printAllSockets()
{
     for (int i = 0; i < MAX_SOCK_NUM; i++)
     {
         Serial.print(F("Socket #"));
         Serial.print(i);
         uint8_ts = W5100.readSnSR(i);
         Serial.print(F(": 0x"));
         Serial.print(s, 16);
         Serial.print(F(" "));
         Serial.print(W5100.readSnPORT(i));
         Serial.print(F(" D:"));
         uint8_t dip[4];
         W5100.readSnDIPR(i, dip);
         for (int j = 0; j < 4; j++)
         {
             Serial.print(dip[j], 10);
             if (j < 3)
                 Serial.print(".");
         }
         Serial.print(F("("));
         Serial.print(W5100.readSnDPORT(i));
         Serial.println(F(")"));
     }
}

MAX_SOCK_NUM根据网卡变化,Wi.net W5100最大为4个sockets,W5200和W5500最大为8个sockets。

希望这对某人有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM