【米尔T113测评】开机点灯+TCP编程
-
米尔电子的 MYD-YT113X 板卡由核心板 MYC-YT113X 和底板 MYB-YT113X 组成,核心板与底板采用邮票孔焊接方式。此外 MYIR 提供了丰富的软件资源以及文档资料。开发板在出厂时已经烧录了镜像,只需要按照手册上电即可使用。
系统以文件的形式为 LED 设备提供操作接口。这些接口位于/sys/class/leds 目录下。在硬件资源列表中,可以通过命令读写 sysfs 的方式对 LED 进行测试。其中 0 表示 LED 点亮,1 表示 LED 熄灭, None表示关闭心跳灯。现在以心跳灯 led-blue 为例测试 LED:
最后附上开发板点灯图:
TCP协议是一种面向连接的、可靠的、基于字节流的传输层通信协议。通讯过程如下:
服务器端先初始化Socket,然后与端口绑定(bind),对端口进行监听(listen),调用accept阻塞,等待客户端连接。在这时如果有个客户端初始化一个Socket,然后连接服务器(connect),如果连接成功,这时客户端与服务器端的连接就建立了。客户端发送数据请求,服务器端接收请求并处理请求,然后把回应数据发送给客户端,客户端读取数据,最后关闭连接,一次交互结束。
服务端代码:
#include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/stat.h> #include <netinet/in.h> #include <sys/socket.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <sys/ipc.h> #include <errno.h> #include <sys/shm.h> #include <time.h> #include <pthread.h> #include <arpa/inet.h> #define BUFFER_SIZE 1024 // buf size #define LISTENQ 1 // the max quote #define SIZE_SHMADD 2048 int main(int argc, char *argv[]) { // check the arguments. if (argc != 2) { printf("Usage:\n"); printf(" %s <port number>\n", argv[0]); return -1; } struct sockaddr_in server_addr; struct sockaddr_in client_addr; char recv_buf[BUFFER_SIZE]; char send_buf[BUFFER_SIZE]; pid_t pid; int sock_fd, conn_fd = -1; int optval = 1; int port = atoi(argv[1]); if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); return -1; } // setting bind port crycled. if ((setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (void *)&optval, sizeof(int))) < 0) { perror("setsockopt"); goto err; } // initized address. bzero(&server_addr, sizeof(struct sockaddr_in)); server_addr.sin_family=AF_INET; server_addr.sin_addr.s_addr=htonl(INADDR_ANY); server_addr.sin_port = htons(port); // bind the port. if (bind(sock_fd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) { perror("bind"); goto err; } // transform the socket into listen socket. if (listen(sock_fd, LISTENQ) == -1) { perror("listen"); goto err; } printf("Waiting for client connectionn...\n"); socklen_t cli_len = sizeof(struct sockaddr_in); conn_fd = accept(sock_fd, (struct sockaddr *)(&client_addr), &cli_len); if (conn_fd == -1) { perror("accept"); close(sock_fd); return -1; } printf("Client accepted : %s:%d \n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); pid = fork(); if (pid > 0) { // The parent process is responsible for sending messages. while (1) { bzero(send_buf, sizeof(send_buf)); char *str = fgets(send_buf, sizeof(send_buf), stdin); if (str == NULL) { perror("fgets"); goto err; } if (send(conn_fd, send_buf, strlen(send_buf) - 1, 0) < 0) { perror("send"); goto err; } } } else if (pid == 0) { // The child process is responsible for receiving messages. while (1) { bzero(recv_buf, sizeof(recv_buf)); if ((recv(conn_fd, recv_buf, BUFFER_SIZE, 0)) <= 0) { perror("recv"); goto err; } printf("[Recv from client %s:%d] : %s\n", inet_ntoa(server_addr.sin_addr), htons(server_addr.sin_port), recv_buf); } } else { perror("fork"); goto err; } close(conn_fd); close(sock_fd); return 0; err: close(conn_fd); close(sock_fd); return -1; }
客户端代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <arpa/inet.h> #define BUFFER_SIZE 1024 int main(int argc, char *argv[]) { // check the arguments. if (argc != 3) { printf("Usage:\n"); printf(" %s <server addr> <port number>\n", argv[0]); return -1; } char *addr = argv[1]; short port = atoi(argv[2]); int sock_fd; int ret; char recv_buf[BUFFER_SIZE], send_buf[BUFFER_SIZE]; struct sockaddr_in server_addr; // initialize address/ bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = inet_addr(addr); // create socket fd. sock_fd = socket(AF_INET, SOCK_STREAM, 0); if (sock_fd == -1) { perror("socket"); return -1; } // connect. ret = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); if (ret == -1) { perror("connect"); goto err; } printf("connect server(IP:%s Port: %d).\n", addr, port); pid_t pid = fork(); if (pid > 0) { // The parent process is responsible for sending messages. while (1) { bzero(send_buf, sizeof(send_buf)); char *str = fgets(send_buf, sizeof(send_buf), stdin); if (str == NULL) { perror("fgets"); goto err; } if (send(sock_fd, send_buf, strlen(send_buf) - 1, 0) < 0) { perror("send"); goto err; } } } else if (pid == 0) { // The child process is responsible for receiving messages. while (1) { bzero(recv_buf, sizeof(recv_buf)); if (sock_fd > 0) { if ((recv(sock_fd, recv_buf, BUFFER_SIZE, 0)) <= 0) { perror("recv"); goto err; } printf("[Recv from server %s:%d] : %s\n", inet_ntoa(server_addr.sin_addr), htons(server_addr.sin_port), recv_buf); } } } else { perror("fork"); goto err; } close(sock_fd); return 0; err: close(sock_fd); return -1; }
编译后运行结果如下:
服务端启起来,等待客户端连接。 启动客户端,发送数据, 服务端接收数据。
-
udp --- 用户数据报协议,是一个无连接的简单的面向数据报的运输层协议。
udp不提供可靠性,它只是把应用程序传给IP层的数据报发送出去,但是并不能保证它们能到达目的地。
udp在传输数据报前不用在客户和服务器之间建立一个连接,且没有超时重发等机制,故而传输速度很快。
udp是一种面向无连接的协议,每个数据报都是一个独立的信息,包括完整的源地址或目的地址,它在网络上以任何可能的路径传往目的地,因此能否到达目的地,到达目的地的时间以及内容的正确性都是不能被保证的。udp通讯流程很简单,具体步骤如下:
udp服务端代码
#include <stdio.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdbool.h> #include <pthread.h> #define BUFFER_SIZE 1024 // buf size struct CONNECT_INFO { struct sockaddr_in client_addr; }; static int sock_fd; static volatile bool is_connect = false; bool udp_server_init(short port) { struct sockaddr_in server_addr; sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if (sock_fd == -1) { perror("socket"); return false; } // initialize address. bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // bind the local port. if ((bind(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in))) == -1) { perror("bind"); return false; } return true; } void *send_thread(void *arg) { struct CONNECT_INFO *info = (struct CONNECT_INFO *)arg; if (info == NULL) { printf("error connect info"); return (void*)-1; } while (1) { char send_buf[BUFFER_SIZE]; bzero(send_buf, sizeof(send_buf)); // input from keyboard. char *str = fgets(send_buf, sizeof(send_buf), stdin); if (str == NULL) { perror("fgets"); continue; } if (strlen(str) <= 1) { continue; } if(is_connect == false) { printf("No client are available!\n"); continue; } // send data. if ((sendto(sock_fd, send_buf, strlen(str) - 1, 0, (struct sockaddr *)&info->client_addr, sizeof(struct sockaddr_in))) < 0) { perror("sendto"); continue; } } return (void*)0; } int main(int argc, char *argv[]) { // check your input. if (argc != 2) { printf("Usage:\n"); printf(" %s <port number>\n", argv[0]); return -1; } short port = atoi(argv[1]); bool ret = udp_server_init(port); if (ret == false) { goto err; } pthread_t t_id; struct CONNECT_INFO info; socklen_t addr_len = sizeof(struct sockaddr_in); memset(&info, 0, sizeof(info)); pthread_create(&t_id, NULL, send_thread, (void *)&info); pthread_detach(t_id); printf("Waiting for client connectionn\n"); while (1) { // recive from client. char recv_buf[BUFFER_SIZE]; bzero(recv_buf, sizeof(recv_buf)); if ((recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&info.client_addr, &addr_len)) <= 0) { perror("recvfrom"); goto err; } printf("[Recv from client %s:%d] : %s\n", inet_ntoa(info.client_addr.sin_addr), htons(info.client_addr.sin_port), recv_buf); is_connect = true; } close(sock_fd); return 0; err: close(sock_fd); return -1; }
udp客户端代码:
#include <stdio.h> #include <string.h> #include <netinet/in.h> #include <sys/types.h> #include <sys/socket.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <arpa/inet.h> #define BUFFER_SIZE 1024 // buf size int main(int argc, char *argv[]) { // check the arguments. if (argc != 3) { printf("Usage:\n"); printf(" %s <server_addr> <portnumber>\n", argv[0]); return -1; } int sock_fd; char send_buf[BUFFER_SIZE]; char recv_buf[BUFFER_SIZE]; struct sockaddr_in server_addr; char *addr = argv[1]; short port = atoi(argv[2]); // initialize address. bzero(&server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = inet_addr(addr); server_addr.sin_port = htons(port); // create udp socket. sock_fd = socket(AF_INET, SOCK_DGRAM, 0); if (sock_fd == -1) { perror("socket"); return -1; } pid_t pid = fork(); if (pid > 0) { // The parent process is responsible for sending messages. while (1) { bzero(send_buf, sizeof(send_buf)); // read from stdin. char *str = fgets(send_buf, sizeof(send_buf), stdin); if (str == NULL) { perror("fgets"); goto err; } if (strlen(str) <= 1) { continue; } // send data. if ((sendto(sock_fd, send_buf, strlen(str) - 1, 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr_in))) < 0) { perror("sendto"); goto err; } } } else if (pid == 0) { // The child process is responsible for receiving messages. while (1) { bzero(recv_buf, sizeof(recv_buf)); socklen_t addr_len = sizeof(struct sockaddr_in); if ((recvfrom(sock_fd, recv_buf, sizeof(recv_buf), 0, (struct sockaddr *)&server_addr, &addr_len)) <= 0) { perror("recvfrom"); goto err; } printf("[Recv from server %s:%d] : %s\n", inet_ntoa(server_addr.sin_addr), htons(server_addr.sin_port), recv_buf); } } close(sock_fd); return 0; err: close(sock_fd); return -1; }
编译后运行结果:
Copyright © 2024 深圳全志在线有限公司 粤ICP备2021084185号 粤公网安备44030502007680号