Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
网络学习营
【网络编程学习】+ Eric + qemu网络编程入门
发布于 2018-07-30 22:39:47 浏览:3190
订阅该版
* 本帖最后由 EricYYG 于 2018-8-2 22:00 编辑 * 一,运行qemu 1. 工具准备 * RT-Thread源码 * env工具,使用全功能lite版 2. 在 t-thread\bsp\qemu-vexpress-a9目录下,运行env,使用scons编译程序,然后输入qemu.bat运行   注意:第一次运行时候,需要输入mkfs sd0格式化我们的文件系统,不然无法正常使用。 二,安装TAP网卡 1,下载并安装 tap-windows-9.21.2.exe   2,重命名   3,同时选择能上网的网卡,然后共享网络   三,运行qemu.bat网络环境 1,打开qemu.bat,添加如下内容   2,开启ping工具   3,使用scons重新编译工程,然后运行输入ifconfig查看网络连接   注意这里如果没有正常,可以尝试输入 pkgs --upgrade pkgs --update 后在scons编译工程! 4,尝试ping百度看看   四,运行TCP客户端例程 1,先使用menuconfig配置,编译运行   2,打开网络调试助手,配置如图   3,在我们系统命令行msh下输入: msh > tcpclient 192.168.137.1 8765 4,使用网络调试助手发送消息给我们的系统   5,运行结果,两边都能收发数据   6,注意 如果提示没有找到tcpclient命令,则应该menuconfig后,使用 pkgs --update更新后,在scons编译 如果连接不通,尝试关闭防火墙后在运行 五,运行udpclient客户端例程 1,使用menuconfig配置  pkgs --update后在scons编译运行  2,打开网络调试助手,配置如图   3,在系统命令行下输入: msh > udpclient 192.168.137.1 8765 4,例程会向我们的udp服务器发送十条数据,如下图:   六,利用ntp协议获取网络时间 [NTP](https://baike.baidu.com/item/NTP) 网络时间协议(Network Time Protocol),是用来同步网络中各个计算机时间的协议。 1,使用menuconfig配置   2,运行程序,输入:ntp_sync   七,通过telnet 协议连接和控制设备 Telnet协议是一种应用层协议,使用于互联网及局域网中,使用虚拟终端机的形式,提供双向、以文字字符串为主的交互功能。属于 TCP/IP 协议族的其中之一,是 Internet 远程登录服务的标准协议和主要方式,常用于网页服务器的远程控制,可供用户在本地主机运行远程主机上的工作。 RT-Thread 目前支持的是 Telnet 服务器, Telnet 客户端连接成功后,将会远程连接到设备的 Finsh/MSH ,实现设备的远程控制。 1,menuconfig配置   2,编译运行  其中ip address:192.168.137.96,就是系统ip地址 输入:telnet_server   3,打开xshell,配置如下:   4,xshell连接成功   八,通过 TFTP 协议连接和控制设备 [TFTP](https://baike.baidu.com/item/TFTP) (Trivial File Transfer Protocol, 简单文件传输协议)是 TCP/IP 协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂、开销不大的文件传输服务,端口号为 69 ,比传统的 FTP 协议要轻量级很多,适用于小型的嵌入式产品上。 RT-Thread 目前支持的是 TFTP 服务器。 1,安装tftp客户端 下载安装 [Tftpd64-4.60-setup.exe](https://github.com/RT-Thread-packages/netutils/blob/master/tools/Tftpd64-4.60-setup.exe),使用 TFTP 前,请先安装该软件。 2,menuconfig配置   3,增大lwip堆栈   4,运行系统,输入ifconfig查看ip   6,在msh shell中运行tftp server   7,配置tftp客户端   8,发送文件到qemu     在qemu中能看到如下文件:   注意如果出现下图情况,应该是文件系统没有格式化,无法创建文件,先运行mkfs sd0,再重新启动一次qemu   9,从qemu中获取文件 先创建一个文件   配置tftp客户端   在本地能看到   九,tcp client聊天室小程序 ** ``` #include
#include
/* 使用BSD socket,需要包含socket.h头文件 */ #include "netdb.h" #include
#define BUFSZ 1024 static const char send_chat_data[] = "This is TCP Client from RT-Thread."; /* 发送用到的数据 */ static const char chat_hello[] = "hello boys and girls
"; /* 发送用到的数据 */ static const char chat_login[] = "login
"; /* 发送用到的数据 */ static const char chat_look[] = "look
"; /* 发送用到的数据 */ static const char chat_say_hello[] = "say hello boys and girls
"; /* 发送用到的数据 */ static const char chat_say_job[] = "say My name is Eric, I'm doing this job at 201800802
"; /* 发送用到的数据 */ char chat_say_see[128] = "say I see "; /* 发送用到的数据 */ static const char chat_who[] = "who
"; /* 发送用到的数据 */ static const char chat_say_myself[] = "say I can see myself in the room
"; /* 发送用到的数据 */ static const char chat_say_logout[] = "say I prepare logout chat room
"; /* 发送用到的数据 */ static const char chat_quit[] = "logout
"; /* 发送用到的数据 */ int tcp_chat_rec(int sock, char *recv_buf, int recv_buf_len) { int bytes_received; while(1) { rt_memset(recv_buf, 0, sizeof(recv_buf)); /* 从sock连接中接收最大BUFSZ - 1字节数据 */ bytes_received = recv(sock, recv_buf, recv_buf_len - 1, 0); if (bytes_received < 0) { /* 接收失败,关闭这个连接 */ closesocket(sock); rt_kprintf("
received error,close the socket.
"); /* 释放接收缓冲 */ rt_free(recv_buf); return -1; } else if (bytes_received > 0) { /* 有接收到数据,把末端清零 */ recv_buf[bytes_received] = '\0'; /* 在控制终端显示收到的数据 */ rt_kprintf("%s
", recv_buf); if(rt_strstr(recv_buf, "
") != RT_NULL) break; } else if (bytes_received == 0) { /* 打印recv函数返回值为0的警告信息 */ rt_kprintf("
Received warning,recv function return 0.
"); } } return bytes_received; } int tcp_chat_send(int sock, const char *chat_data) { int ret; //rt_kprintf("
", chat_data); /* 发送数据到sock连接 */ ret = send(sock, chat_data, strlen(chat_data), 0); if (ret < 0) { /* 接收失败,关闭这个连接 */ closesocket(sock); rt_kprintf("
send error,close the socket.
"); } else if (ret == 0) { /* 打印send函数返回值为0的警告信息 */ rt_kprintf("
Send warning,send function return 0.
"); } return ret; } void tcp_chat_client(const char *url, int port) { int i; char *recv_data, who[128]; struct hostent *host; int sock; struct sockaddr_in server_addr; /* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */ host = gethostbyname(url); /* 分配用于存放接收数据的缓冲 */ recv_data = rt_malloc(BUFSZ); if (recv_data == RT_NULL) { rt_kprintf("No memory
"); return; } /* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /* 创建socket失败 */ rt_kprintf("Socket error
"); /* 释放接收缓冲 */ rt_free(recv_data); return; } /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *)host->h_addr); rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); rt_kprintf("
Connect Chat Server"); for(i=0; i< 100; i++) { /* 连接到服务端 */ if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) { /* 连接失败 */ rt_kprintf("."); rt_thread_delay(RT_TICK_PER_SECOND*5); if(i >= 99) { closesocket(sock); /*释放接收缓冲 */ rt_free(recv_data); return; } else continue; } else { rt_kprintf("
Connect success!
"); break; } } tcp_chat_rec(sock, recv_data, BUFSZ); //while (1) { rt_kprintf("
"); tcp_chat_send(sock, chat_hello); tcp_chat_rec(sock, recv_data, BUFSZ); rt_kprintf("
"); tcp_chat_send(sock, chat_login); rt_kprintf("
"); tcp_chat_send(sock, chat_say_job); tcp_chat_rec(sock, recv_data, BUFSZ); rt_kprintf("
"); tcp_chat_send(sock, chat_look); tcp_chat_rec(sock, recv_data, BUFSZ); tcp_chat_rec(sock, recv_data, BUFSZ); rt_kprintf("
"); tcp_chat_send(sock, chat_who); tcp_chat_rec(sock, recv_data, BUFSZ); tcp_chat_rec(sock, who, BUFSZ); for(i=0; i<128; i++) if(who* == ' ') who* = ' '; rt_kprintf("
"); tcp_chat_send(sock, chat_say_see); tcp_chat_send(sock, who); tcp_chat_send(sock, "
"); tcp_chat_rec(sock, recv_data, BUFSZ); rt_kprintf("
"); tcp_chat_send(sock, chat_say_logout); tcp_chat_rec(sock, recv_data, BUFSZ); rt_kprintf("
"); tcp_chat_send(sock, chat_quit); } return; } ``` ``` main.c #include
#include
#include
#include
#include "chat.h" int main(void) { printf("hello rt-thread
"); rt_thread_delay(RT_TICK_PER_SECOND*5); tcp_chat_client("183.193.243.90", 11222); //tcp_chat_client("192.168.137.1", 11222); return 0; } ``` 运行结果如下: **
查看更多
2
个回答
默认排序
按发布时间排序
armink
2018-07-31
这家伙很懒,什么也没写!
玩的很溜呀,一下子折腾了这么多功能
EricYYG
2018-07-31
这家伙很懒,什么也没写!
>玩的很溜呀,一下子折腾了这么多功能 --- 哪里有,我这个都是小打小闹!练练手而已
撰写答案
登录
注册新账号
关注者
0
被浏览
3.2k
关于作者
EricYYG
这家伙很懒,什么也没写!
提问
13
回答
9
被采纳
0
关注TA
发私信
相关问题
1
【LWIP学习营】第一关开发环境搭建
2
LWIP学习营第一周入门移植问题汇总贴
3
【LWIP学习营】f407+lan8720A小结
4
【LwIP学习营】【第一周】仅零散记录,无主题
5
【LWIP学习营】正点原子探索者F407+LAN8720第一周小结
6
【LwIP学习营】【第一周】网络通信基础及实现TCP 聊天客户端
7
【LwIP学习营】【第一周】LWIP移植
8
【LwIP学习营】【第一周】LWIP移植
9
【LwIP学习营】【第一周】开发板适配
10
【LwIP学习营】【第一周】环境搭建和配置验证
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-thred的stm32h723对应bsp包CubeMX添加其他外设报错
2
RT-Thread中的time溢出问题,时间戳溢出,解决方法
3
ART-PI使用env驱动ETH网卡,pc和板子可以ping通
4
SystemView线程名字不显示
5
只用网页也能跑RT-Smart 无门槛腾讯Cloud studio + smart-build快速构建
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
9
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
xiaorui
3
个答案
1
次被采纳
winfeng
2
个答案
1
次被采纳
本月文章贡献
catcatbing
2
篇文章
5
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
阳光的掌控者
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部