0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

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

华仔的编程随笔 来源:华仔的编程随笔 作者:华仔的编程随笔 2023-05-27 08:59 次阅读

创建tcp服务

在虚拟机上新建tcp_server.c:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define SERVER_PORT 3861
#define LISENT_NUM 10

int main(int argc, char * argv[])
{
    int sfd, cfd;
    struct sockaddr_in clientaddr;
    struct sockaddr_in serverAddr;
    char buff[1024];
    int size = sizeof(struct sockaddr);
    pthread_t client_thread[LISENT_NUM];

    if((sfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 
    {
        perror("socket");
        exit(-1);
    }

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

    if (bind(sfd, (struct sockaddr*)&serverAddr, sizeof(struct sockaddr)) == -1)
	{
		perror("bind");
		close(sfd);
		exit(-1);
    }
    if(listen(sfd, LISENT_NUM) == -1)
    {
        perror("listen");
        close(sfd);
        exit(-1);
    }

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

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

    while (1)
    {
        usleep(1000*10);
        if (send(cfd, "hello MYD/YG2L", 6, MSG_NOSIGNAL) == -1)
        {
            perror("send");
            exit(-1);
        }
        printf("send: hello MYD/YG2L\\n");
        usleep(1000*10);
        if(recv(cfd, buff, sizeof(buff), 0) == -1)
        {
            perror("recv");
            exit(-1);
        }
        printf("receive: %s\\n", buff);
    }
    return0;

}
  1. 加载sdk:source /opt/yg2lx/environment-setup-aarch64-poky-linux
  2. 编译:
lugl@lugl-virtual-machine:~/MYDG2L$ source /opt/yg2lx/environment-setup-aarch64-poky-linux
lugl@lugl-virtual-machine:~/MYDG2L$ $CC tcp_server.c -o tcp_server
In file included from /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/bits/libc-header-start.h:33,
                 from /opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/stdio.h:27,
                 from tcp_server.c:1:
/opt/yg2lx/sysroots/aarch64-poky-linux/usr/include/features.h:397:4: warning: #warning _FORTIFY_SOURCE requires compiling with optimization (-O) [-Wcpp]
 #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
    ^~~~~~~
lugl@lugl-virtual-machine:~/MYDG2L$ ls
tcp_server  tcp_server.c
lugl@lugl-virtual-machine:~/MYDG2L$ file tcp_server
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
root@myir-yg2lx:~# ./tcp_server
#@ listen SERVER_PORT 3861
main: server waiting connect...
client (ip = 192.168.3.166 : SERVER_PORT = 53634) connect success
send: hello MYD/YG2L
receive: hello
send: hello MYD/YG2L
receive: hello
send: hello MYD/YG2L
receive: hello MYD
send: hello MYD/YG2L
  1. 打开TCP调试工具,连上服务器:
    image.png

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

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • TCP
    TCP
    +关注

    关注

    8

    文章

    1270

    浏览量

    78282
收藏 人收藏

    评论

    相关推荐

    米尔-RZ/G2UL开发板】1.开箱

    /G2UL 都能提供出色的性能和稳定的运行。以下是一些外观特写: 正面照片 背面照片 规格 米尔-RZ/
    发表于 02-04 23:38

    飞凌嵌入式之G2L核心开发板开箱体验

    FET-G2LD-C核心基于高性能、超高效处理 RZ/
    发表于 06-08 15:20

    【飞凌RZ/G2L开发板试用体验】开箱帖子,飞凌质量YYDS

    ,品质指的信赖。飞凌RZ/G2L开发板基于RZ/G2L
    发表于 08-15 18:46

    【飞凌RZ/G2L开发板试用体验】+01.开箱(zmj)

    带来的正是能满足这些要求的飞凌RZ/G2L开发板评估套件。1.RZ/V2L评估套件简介
    发表于 08-28 19:13

    【飞凌RZ/G2L开发板】飞凌RZ/G2L开发板试用测评报告

    、性能和试用的一些体会和试用开发过程记录下来,也分享给准备使用此开发板的朋友们。一、基本介绍收到开发板
    发表于 08-29 02:01

    米尔RZ/G2L开发板-试用体验】开箱

    感谢 感谢电子发烧友论坛、感谢米尔电子,把米尔RZ/G2L
    发表于 05-14 19:41

    米尔RZ/G2L开发板-试用体验】开箱 + 开机

    感谢 感谢电子发烧友论坛、感谢米尔电子,把米尔RZ/G2L
    发表于 05-18 19:33

    米尔RZ/G2L开发板-试用体验】LCD 显示测试

    实验器材 1、米尔RZ/G2L开发板 2、MY-
    发表于 05-22 19:26

    米尔RZ/G2L开发板-试用体验】米尔-RZG2L - 64位双核MPU开发板开箱测评

    刚收到米尔RZ/G2L开发板打开包装后看到的很大的一块黑色PCB,做工精美的
    发表于 05-22 21:53

    米尔RZ/G2L开发板-试用体验】米尔RZ/G2L开发板开箱视频

    今天刚刚收到米尔RZ/G2L开发板,拆开包裹后给人的感觉是惊艳,板卡设计真的很棒,来看看视频
    发表于 05-22 21:58

    150套开发板免费送!还有5G手机拿?米尔RZ/G2L开发板创意秀

    /12 05 送的是哪款开发板米尔MYD-YG2LX开发板,该款产品采用
    发表于 05-24 16:36

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

    : hello MYD/YG2L receive: hello MYD send: hello MYD/YG2L 打开TCP调试工具,连上服务器: 发送数据后,
    发表于 05-27 08:57

    米尔RZ/G2L开发板-试用体验】米尔RZ/G2L开发板使用SSH登录

    收到的米尔RZ/G2L开发板上电测试一下SSH登录方式和其它测试! SSH登录 在使用
    发表于 06-11 21:47

    米尔RZ/G2L开发板-试用体验】认识一下米尔RZ/G2L开发板的核心

    收到米尔RZ/G2L开发板后一直对米尔旗下
    发表于 07-29 00:21

    米尔瑞萨RZ/G2L开发板-初体验

    介绍米尔RZ/G2L开发板
    的头像 发表于 05-15 09:10 696次阅读
    <b class='flag-5'>米尔</b>瑞萨<b class='flag-5'>RZ</b>/<b class='flag-5'>G2L</b><b class='flag-5'>开发板</b>-初体验