Arduino网络编程实战-Ethernet篇-TCP服务器

TCP服务器

Arduino Ethernet Shield V1 允许 Arduino 板连接到互联网。 它基于 Wiznet W5100ethernet 芯片(数据表)。 Wiznet W5100 提供支持 TCP 和 UDP 的网络 (IP) 堆栈。 它最多支持四个同时套接字连接。

本次实例将演示如何实现一个简单的TCP服务器

在这里插入图片描述

1、硬件准备

  • Arduino Mega 2560
  • Arduino Ethernet Shield
  • 路由器(推荐可以上网、开启DHCP)
  • 网线一条
  • 电脑一台

2、软件准备

  • Arduino IDE
  • 网络调试软件(推荐使用通信猫)

3、代码实现

1)添加头文件

1
2
3
#include <SPI.h>
#include <Ethernet.h>

2)设置物理地址(MAC)

1
2
3
4
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

3)定义配置TCP服务器

1
2
EthernetServer server(8090); // 服务器的端口为8090

4)启动网卡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Ethernet.begin(mac); // 通过设置MAC,进行DHCP方式获取IP

if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1);
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
while(true){
delay(1);
}
}

5)输出分配到的IP

1
2
3
4
5
6
7
8
9
Serial.print("IP:");
Serial.println(Ethernet.localIP()); // 查询分配IP
Serial.print("Subnet Mask:");
Serial.println(Ethernet.subnetMask()); // 查询子网掩码
Serial.print("Gateway:");
Serial.println(Ethernet.gatewayIP()); // 查询网关IP
Serial.print("DNS Server:");
Serial.println(Ethernet.dnsServerIP()); // 查询DNS服务器IP

6)启动TCP服务器

1
2
3
server.begin();
Serial.println("TCP Server Started");

7)监听客户端连接及数据接收与发送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
EthernetClient client = server.available(); // 监听客户端连接
if(client){
Serial.print("client@");
Serial.print(client.remoteIP());
Serial.print(":");
Serial.print(client.remotePort());
Serial.println(" connected");
if(client.available() > 0){
// 读取客户端数据
char thisChar = client.read();
// 将数据发送回客户端
server.write(thisChar);
// 串口输出
Serial.write(thisChar);
}
}

8)完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <SPI.h>
#include <Ethernet.h>

#define USE\_STATIC 0

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
#if USE\_STATIC
IPAddress ip(192, 168, 0, 177);
#endif

EthernetServer server(8090);

void setup() {

Serial.begin(9600);
Serial.println("Ethernet TCP Server Example");

#if USE\_STATIC
Ethernet.begin(mac,ip);
#else
Ethernet.begin(mac);
#endif

if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1);
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
while(true){
delay(1);
}
}

Serial.print("IP:");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask:");
Serial.println(Ethernet.subnetMask());
Serial.print("Gateway:");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server:");
Serial.println(Ethernet.dnsServerIP());

server.begin();
Serial.println("TCP Server Started");
}

void loop() {
EthernetClient client = server.available();
if(client){
Serial.print("client@");
Serial.print(client.remoteIP());
Serial.print(":");
Serial.print(client.remotePort());
Serial.println(" connected");
if(client.available() > 0){
// 读取客户端数据
char thisChar = client.read();
// 将数据发送回客户端
server.write(thisChar);
// 串口输出
Serial.write(thisChar);
}
}
}

4、运行调试结果

在这里插入图片描述
在这里插入图片描述

文章来源: https://iotsmart.blog.csdn.net/article/details/122677438