【米尔瑞萨RZ/G2L开发板-试用体验】创建TCP服务器
<div class='showpostmsg'><p>头书写一个tcp通信的实例</p><p>1、在虚拟机上新建tcp_server.c,代码如下:</p>
<pre>
<code>
#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;
int size = sizeof(struct sockaddr);
pthread_t client_thread;
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;
}
</code></pre>
<p>2、加载sdk:source /opt/yg2lx/environment-setup-aarch64-poky-linux</p>
<p>3、编译:</p>
<pre>
<code>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)
</code></pre>
<p>4、上传到开发板,修改运行权限、运行:</p>
<pre>
<code>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</code></pre>
<p>5、打开网终调试助手,连接服务器后发送数据:</p>
<p> 如上图,调试正常。</p>
<p>【总结】在安装好交叉编译环境后,就可以愉快的进行linux下的编程了。</p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>交叉编译环境的安装麻烦不</p>
<p>还要装虚拟机</p>
火辣西米秀 发表于 2023-5-28 22:02
交叉编译环境的安装麻烦不
还要装虚拟机
<p>如果你是linux\macos就不需要安装虚拟机。当然win下也可以。</p>
<p>代码复制的好像有点问题</p>
<p> </p>
wangerxian 发表于 2023-5-29 09:56
代码复制的好像有点问题
<p>我回去,重新上传一下,谢谢提醒。</p>
<p>我觉得是可以值得试试太棒了这边。支持物联网方面的功能很多。支持esp32 和rp2040 树莓派pi pico picow。</p>
多点学习啊 发表于 2023-6-2 11:19
我觉得是可以值得试试太棒了这边。支持物联网方面的功能很多。支持esp32 和rp2040 树莓派pi pico picow。
<p>多谢大佬支持,我在努力学习中!</p>
页:
[1]