网站首页 全球最实用的IT互联网站!

人工智能P2P分享Wind搜索发布信息网站地图标签大全

当前位置:诺佳网 > 电子/半导体 > 嵌入式技术 >

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

时间:2023-05-27 08:59

人气:

作者:admin

标签:

导读:在米尔RZ/G2L开发板上用C 创建TCP服务器...

创建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

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

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信