Skip to main content

Ethernet 编程

前言

YY3588 支持 1 个 千兆网口(Ethernet0) ,一个 2.5Gbps 网口(Ethernet1)。
yy3588网口图片.jpg

2 连通性测试

2.1 查看网卡节点

linaro@linaro-alip:~$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.14 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::80ef:1567:63c9:4c3c prefixlen 64 scopeid 0x20<link>
ether ae:66:6d:e6:89:eb txqueuelen 1000 (Ethernet)
RX packets 226057 bytes 102260286 (97.5 MiB)
RX errors 0 dropped 20330 overruns 0 frame 0
TX packets 14258 bytes 1453498 (1.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 72

eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.16 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::8404:6967:3fe1:2517 prefixlen 64 scopeid 0x20<link>
ether 9a:df:d7:1f:52:58 txqueuelen 1000 (Ethernet)
RX packets 192 bytes 17570 (17.1 KiB)
RX errors 0 dropped 20 overruns 0 frame 0
TX packets 16 bytes 1937 (1.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 67 bytes 4219 (4.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 67 bytes 4219 (4.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

2.2 上网测试

# 使用 eth0 上网
root@linaro-alip:/# ping -I eth0 -c 8 www.baidu.com
PING www.wshifen.com (103.235.46.96) from 192.168.1.14 eth0: 56(84) bytes of data.
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=1 ttl=45 time=277 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=2 ttl=45 time=270 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=3 ttl=45 time=225 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=4 ttl=45 time=272 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=5 ttl=45 time=275 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=6 ttl=45 time=255 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=7 ttl=45 time=221 ms
64 bytes from 103.235.46.96 (103.235.46.96): icmp_seq=8 ttl=45 time=281 ms

--- www.wshifen.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7008ms
rtt min/avg/max/mdev = 220.731/259.456/280.557/22.381 ms
# 使用 eth1 上网
参考 eth0

3 设置静态IP地址

3.1 修改配置文件

  • 以网关192.168.1.1为例
cat /etc/network/interfaces

auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1

auto eth1
iface eth1 inet static
address 192.168.1.101
netmask 255.255.255.0
gateway 192.168.1.1

3.2 重启网络服务

sudo systemctl restart networking
# 或
reboot

4 简单的UDP DEMO程序

4.1 源代码

  • 服务器端源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>

int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert(sockfd != -1);

struct sockaddr_in saddr, caddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(6000);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int res = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
assert(res != -1);

while (1)
{
int len = sizeof(caddr);
char buff[128] = {0};
recvfrom(sockfd, buff, sizeof(buff) - 1, 0, (struct sockaddr *)&caddr, (socklen_t *)&len);
printf("buff=%s\n", buff);
if (strncmp(buff, "end", 3) == 0)
{
break;
}
sendto(sockfd, "ok", 2, 0, (struct sockaddr *)&caddr, sizeof(caddr));
}
close(sockfd);
exit(0);
}
  • 客户端源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>

int main()
{
int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
assert(sockfd != -1);

struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(6000);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

while (1)
{
char buff[128] = {0};
printf("input\n");
fgets(buff, sizeof(buff), stdin);
if (strncmp(buff, "end", 3) == 0)
{
break;
}
sendto(sockfd, buff, strlen(buff), 0, (struct sockaddr *)&saddr, sizeof(saddr));
memset(buff, 0, sizeof(buff));
int len = sizeof(saddr);
recvfrom(sockfd, buff, sizeof(buff) - 1, 0, (struct sockaddr *)&saddr, (socklen_t *)&len);
printf("recv: %s\n", buff);
}
close(sockfd);
exit(0);
}

现象演示动图

debian12_udp_test.gif

5 简单的TCP DEMO程序

5.1 源代码

  • 服务器端源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>

int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd != -1);

struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(6000);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int res = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
assert(res != -1);

res = listen(sockfd, 5);
assert(res != -1);

while (1) {
struct sockaddr_in caddr;
socklen_t len = sizeof(caddr);
int clientfd = accept(sockfd, (struct sockaddr *)&caddr, &len);
assert(clientfd != -1);

char buff[128] = {0};
recv(clientfd, buff, sizeof(buff) - 1, 0);
printf("Received: %s\n", buff);
if (strncmp(buff, "end", 3) == 0) {
break;
}
send(clientfd, "Message received", 16, 0);
close(clientfd);
}
close(sockfd);
exit(0);
}
  • 客户端源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>

int main() {
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
assert(sockfd != -1);

struct sockaddr_in saddr;
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(6000);
saddr.sin_addr.s_addr = inet_addr("127.0.0.1");

int res = connect(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
assert(res != -1);

while (1) {
char buff[128] = {0};
printf("input\n");
fgets(buff, sizeof(buff), stdin);
if (strncmp(buff, "end", 3) == 0) {
break;
}
send(sockfd, buff, strlen(buff), 0);
memset(buff, 0, sizeof(buff));
recv(sockfd, buff, sizeof(buff) - 1, 0);
printf("recv: %s\n", buff);
}
close(sockfd);
exit(0);
}

5.2 现象演示动图

debian12_tcp_test.gif