使用this库我尝试连接静态IP,但它没有连接到互联网。如果在路由器上启用了DHCP并删除它func createLocalConnection(port string) *net.TCPConn {
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:"+port)
if err != nil {
panic(err)
}
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
panic(err)
}
return conn
}
func proxyConnection(conn *net.TCPConn) {
defer conn.Close()
data := make([]byte, 1)
n, err := conn.Read(data)
if err != nil {
fmt.Println(err)
return
}
var proxyConn *net.TCPConn
if data[0] == 0x03 { // RTMP first byte.
proxyConn = createLocalConnection(RTMPPort)
} else {
proxyConn = createLocalConnection(HTTPPort)
}
proxyConn.Write(data[:n])
defer proxyConn.Close()
// Request loop
go func() {
for {
data := make([]byte, 1024*1024)
n, err := conn.Read(data)
if err != nil {
break
}
proxyConn.Write(data[:n])
}
}()
// Response loop
for {
data := make([]byte, 1024*1024)
n, err := proxyConn.Read(data)
if err != nil {
break
}
conn.Write(data[:n])
}
}
func main() {
listener, err := net.ListenTCP("tcp", addr)
if err != nil {
panic(err)
}
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
continue
}
go server.ProxyConnection(conn)
}
}
它的工作原理。我使用arduino MEGA。静态IP经过测试并可在其他设备上运行。
问题:如何正确设置静态IP?
注意:我在这个社区发布此问题,因为它是关于代码而不是电子产品。
连接:
ether.staticSetup(myip, gwip, dnsip, netmask);
代码:
VCC - 3.3V
GND - GND
SCK - Pin 52
SO - Pin 50
SI - Pin 51
CS - Pin 53 # Selectable with the ether.begin() function
输出:
// This is a demo of the RBBB running as webserver with the Ether Card
// 2010-05-28 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <EtherCard.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = { xxx, xxx, 216, 203 };
static byte gwip[] = { xxx, xxx, 216, 126 };
static byte dnsip[] = { 8, 8, 8, 8};
static byte netmask[] = { 255, 255, 255, 0};
byte Ethernet::buffer[500];
BufferFiller bfill;
void setup () {
Serial.begin(9600);
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)
Serial.println(F("Failed to access Ethernet controller"));
ether.staticSetup(myip, gwip, dnsip, netmask);
while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("\nGateway found");
Serial.print("ether.clientWaitingGw=");// just to test
Serial.print(ether.clientWaitingGw());
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos) { // check if valid tcp data is received
ether.httpServerReply(homePage());// send web page data
Serial.println("\nSend web data");
}
delay(1000);
}
static word homePage() {
long t = millis() / 1000;
word h = t / 3600;
byte m = (t / 60) % 60;
byte s = t % 60;
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\n"
"Pragma: no-cache\r\n"
"\r\n"
"<meta http-equiv='refresh' content='1'/>"
"<title>RBBB server</title>"
"<h1>$D$D:$D$D:$D$D</h1>"),
h / 10, h % 10, m / 10, m % 10, s / 10, s % 10);
return bfill.position();
}
答案 0 :(得分:1)
经过研究,循环功能的延迟导致请求超时。删除delay(1000);
即可使用
使用static ip修正代码:
#include <EtherCard.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };
static byte myip[] = { xxx, xxx, 216, 203 };
static byte gwip[] = { xxx, xxx, 216, 126 };
static byte dnsip[] = { 8, 8, 8, 8};
static byte netmask[] = { 255, 255, 255, 0};
byte Ethernet::buffer[500];
BufferFiller bfill;
void setup () {
Serial.begin(9600);
if (ether.begin(sizeof Ethernet::buffer, mymac, 53) == 0)// 53 only on MEGA
Serial.println(F("Failed to access Ethernet controller"));
ether.staticSetup(myip, gwip, dnsip, netmask);
while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());
Serial.println("\nGateway found");
Serial.print("ether.clientWaitingGw=");// just to test
Serial.print(ether.clientWaitingGw());
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos) { // check if valid tcp data is received
ether.httpServerReply(homePage());// send web page data
Serial.println("\nSend web data");
}