设为首页收藏本站
查看: 667|回复: 2

【米尔瑞萨RZ/G2L开发板-试用体验】TCP服务器

[复制链接]

33

主题

15

回帖

837

积分

管理员

积分
837
米尔小助手1 发表于 2023-6-12 18:03:10 | 显示全部楼层 |阅读模式
创建tcp服务
在虚拟机上新建tcp_server.c:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/fcntl.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/in.h>
  10. #include <sys/socket.h>
  11. #include <sys/wait.h>
  12. #include <pthread.h>

  13. #define SERVER_PORT 3861
  14. #define LISENT_NUM 10

  15. int main(int argc, char * argv[])
  16. {
  17.     int sfd, cfd;
  18.     struct sockaddr_in clientaddr;
  19.     struct sockaddr_in serverAddr;
  20.     char buff[1024];
  21.     int size = sizeof(struct sockaddr);
  22.     pthread_t client_thread[LISENT_NUM];

  23.     if((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  24.     {
  25.         perror("socket");
  26.         exit(-1);
  27.     }

  28.     memset(&serverAddr, 0, sizeof(struct sockaddr));
  29.     serverAddr.sin_family = AF_INET;
  30.     serverAddr.sin_addr.s_addr = INADDR_ANY;
  31.     serverAddr.sin_port = htons(SERVER_PORT);

  32.     if (bind(sfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)
  33.         {
  34.                 perror("bind");
  35.                 close(sfd);
  36.                 exit(-1);
  37.     }
  38.     if(listen(sfd, LISENT_NUM) == -1)
  39.     {
  40.         perror("listen");
  41.         close(sfd);
  42.         exit(-1);
  43.     }

  44.     printf("#@ listen SERVER_PORT %d\n", SERVER_PORT);
  45.     printf("main: server waiting connect...\n");

  46.     if ((cfd = accept(sfd, (struct sockaddr *)&clientaddr, (socklen_t*)&size)) == -1)
  47.         {
  48.                 perror("accept");
  49.                 close(sfd);
  50.                 return0;
  51.         }
  52.     printf("client (ip = %s : SERVER_PORT = %d) connect success\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));

  53.     while (1)
  54.     {
  55.         usleep(1000*10);
  56.         if (send(cfd, "hello MYD/YG2L", 6, MSG_NOSIGNAL) == -1)
  57.         {
  58.             perror("send");
  59.             exit(-1);
  60.         }
  61.         printf("send: hello MYD/YG2L\n");
  62.         usleep(1000*10);
  63.         if(recv(cfd, buff, sizeof(buff), 0) == -1)
  64.         {
  65.             perror("recv");
  66.             exit(-1);
  67.         }
  68.         printf("receive: %s\n", buff);
  69.     }
  70.     return0;

  71. }
复制代码

  • 加载sdk:source /opt/yg2lx/environment-setup-aarch64-poky-linux
  • 编译:

  1. lugl@lugl-virtual-machine:~/MYDG2L$ source /opt/yg2lx/environment-setup-aarch64-poky-linux
  2. lugl@lugl-virtual-machine:~/MYDG2L$ $CC tcp_server.c -o tcp_server
  3. In file included from /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/bits/libc-header-start.h:33,
  4.                  from /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/stdio.h:27,
  5.                  from tcp_server.c:1:
  6. /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/features.h:397:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
  7. #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
  8.     ^~~~~~~
  9. lugl@lugl-virtual-machine:~/MYDG2L$ ls
  10. tcp_server  tcp_server.c
  11. lugl@lugl-virtual-machine:~/MYDG2L$ file tcp_server
  12. tcp_server: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-aarch64.so.1, for GNU/Linux 3.14.0, BuildID[sha1]=c81069580d81fc0f4ac9bb00ecdd7ddf822e251f, with debug_info, not stripped
复制代码


  • 上传开发板,修改执行权限后运行:

  1. root@myir-yg2lx:~# chmod 777 tcp_server
  2. root@myir-yg2lx:~# ./tcp_server
  3. #@ listen SERVER_PORT 3861
  4. main: server waiting connect...
  5. client (ip = 192.168.3.166 : SERVER_PORT = 53634) connect success
  6. send: hello MYD/YG2L
  7. receive: hello
  8. send: hello MYD/YG2L
  9. receive: hello
  10. send: hello MYD/YG2L
  11. receive: hello MYD
  12. send: hello MYD/YG2L
复制代码


  • 打开TCP调试工具,连上服务器:




发送数据后,服务器打印出接收到的数据,客户端也接收到数据。

回复

使用道具 举报

0

主题

5

回帖

14

积分

新手上路

积分
14
李俊 发表于 2023-6-13 14:09:38 | 显示全部楼层
这个比较好玩
回复 支持 反对

使用道具 举报

2

主题

5

回帖

88

积分

注册会员

积分
88
bin 发表于 2023-8-3 11:25:59 | 显示全部楼层
已验证,send: hello MYD/YG2L,只能收到hello,send(cfd, "hello MYD/YG2L", 6, MSG_NOSIGNAL),6要改下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录

本版积分规则

Archiver|手机版|小黑屋|米尔科技论坛   

GMT+8, 2024-5-13 03:43 , Processed in 0.048487 second(s), 23 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表