|
芯驰D9开发板上有路以太网接口,测试下两个以太网接口通信。1、开发板接口
2、将两个**接口都连接到交换机,通过命令配置**连接
root@myd-jd9x:~# ifconfig eth0 192.168.1.100 netmask 255.255.255.0 up
root@myd-jd9x:~# ifconfig eth1 192.168.1.101 netmask 255.255.255.0 up
3、查看**的IP地址
4、使用ubuntu虚拟机ping两个**
**连接正常
5、两个**同时登录SSH
6、测试程序
udp_server.c
- <font size="3">
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <net/if.h>
- #include <linux/sockios.h>
- #include <sys/ioctl.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <net/if.h>
- #include <netinet/ether.h>
- #include <netinet/in.h>
- #include <netpacket/packet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
-
- #define BUF_SIZE 2048
- #define INTERFAXENAME "udp"
- int sock;
- int main(int argc, char *argv[])
- {
- int ret = 0;
- char buf[1024];
- memset(buf,1,1024);
- if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
- {
- perror("socket");
- exit(1);
- }
- struct sockaddr_in server_addr;
- memset(&server_addr, 0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(5050);
- server_addr.sin_addr.s_addr = inet_addr("192.168.1.100");
-
- ret = bind(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
- if (ret < 0)
- {
- printf("%s: bind 192.168.1.100 failed\n", __FUNCTION__);
- close(sock);
- return 0;
- }
- else
- printf("bind 192.168.1.100 success \r\n");
-
- struct sockaddr_in client_addr;
- socklen_t len;
- len = sizeof(client_addr);
- while(1)
- {
- ret = recvfrom(sock, buf, 1024, 0,(struct sockaddr *)&client_addr, &len);
- printf("recvfrom 192.168.1.101 ret:%d\n", ret);
- }
-
- close(sock);
- return 0;
- }
- </font>
复制代码
udp_client.c
- <font size="3">
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <net/if.h>
- #include <linux/sockios.h>
- #include <sys/ioctl.h>
- #include <arpa/inet.h>
- #include <errno.h>
- #include <net/if.h>
- #include <netinet/ether.h>
- #include <netinet/in.h>
- #include <netpacket/packet.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <time.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
-
- #define BUF_SIZE 2048
- #define INTERFAXENAME "udp"
- int sock;
- int main(int argc, char *argv[])
- {
- int js=0;
- int ret = 0;
- char buf[1024];
- memset(buf,1,1024);
-
- if ( (sock=socket(AF_INET, SOCK_DGRAM, 0)) <0)
- {
- perror("socket");
- exit(1);
- }
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(5050);
- addr.sin_addr.s_addr = inet_addr("192.168.1.101");
- ret = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
- if (ret < 0) {
- printf("%s: bind 192.168.1.101 failed\n", __FUNCTION__);
- close(sock);
- return 0;
- }
- else
- printf("bind 192.168.1.101 success \r\n");
-
- struct sockaddr_in server_addr;
- memset(&server_addr, 0, sizeof(server_addr));
- server_addr.sin_family = AF_INET;
- server_addr.sin_port = htons(5050);
- server_addr.sin_addr.s_addr = inet_addr("192.168.1.100");
-
- while(1)
- {
- ret = sendto(sock, buf, 1024, 0,(struct sockaddr *)&server_addr, sizeof(server_addr));
- printf("sendto 192.168.1.100 ret:%d js:%d\n", ret,js++);
- usleep(200000);
- }
-
- close(sock);
- return 0;
- }
- </font>
复制代码
7、编译
ubuntu:/opt/myd-jd9# aarch64-linux-gnu-gcc -o udp_server udp_server.c
ubuntu:/opt/myd-jd9# aarch64-linux-gnu-gcc -o udp_client udp_client.c
ip:192.168.1.100 运行udp_server
ip:192.168.1.101 运行udp_client
8、两路网口收发测试
|
|