Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
rt-thread
提交
5f4bcfa3
R
rt-thread
项目概览
BaiXuePrincess
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
5f4bcfa3
编写于
8月 10, 2017
作者:
B
Bernard Xiong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[examples] code cleanup.
上级
f6b6a5cf
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
99 addition
and
83 deletion
+99
-83
examples/network/tcpclient.c
examples/network/tcpclient.c
+25
-17
examples/network/tcpsendpacket.c
examples/network/tcpsendpacket.c
+54
-49
examples/network/tcpserver.c
examples/network/tcpserver.c
+20
-17
未找到文件。
examples/network/tcpclient.c
浏览文件 @
5f4bcfa3
#include <rtthread.h>
#include <lwip/netdb.h>
/* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/netdb.h>
/* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
#define BUFSZ
1024
#define BUFSZ
1024
static
const
char
send_data
[]
=
"This is TCP Client from RT-Thread."
;
/* 发送用到的数据 */
void
tcpclient
(
const
char
*
url
,
int
port
)
{
int
ret
;
char
*
recv_data
;
struct
hostent
*
host
;
int
sock
,
bytes_received
;
int
ret
;
struct
sockaddr_in
server_addr
;
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
...
...
@@ -61,15 +62,18 @@ void tcpclient(const char* url, int port)
{
/* 接收失败,关闭这个连接 */
lwip_close
(
sock
);
rt_kprintf
(
"
\n
received error,close the socket.
\r\n
"
);
rt_kprintf
(
"
\n
received error,close the socket.
\r\n
"
);
/* 释放接收缓冲 */
rt_free
(
recv_data
);
break
;
}
else
if
(
bytes_received
==
0
)
{
/* 打印recv函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Received warning,recv function return 0.
\r\n
"
);
}
}
else
if
(
bytes_received
==
0
)
{
/* 打印recv函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Received warning,recv function return 0.
\r\n
"
);
continue
;
}
/* 有接收到数据,把末端清零 */
recv_data
[
bytes_received
]
=
'\0'
;
...
...
@@ -78,7 +82,8 @@ void tcpclient(const char* url, int port)
{
/* 如果是首字母是q或Q,关闭这个连接 */
lwip_close
(
sock
);
rt_kprintf
(
"
\n
got a 'q' or 'Q',close the socket.
\r\n
"
);
rt_kprintf
(
"
\n
got a 'q' or 'Q',close the socket.
\r\n
"
);
/* 释放接收缓冲 */
rt_free
(
recv_data
);
break
;
...
...
@@ -91,17 +96,20 @@ void tcpclient(const char* url, int port)
/* 发送数据到sock连接 */
ret
=
send
(
sock
,
send_data
,
strlen
(
send_data
),
0
);
if
(
ret
<
0
)
if
(
ret
<
0
)
{
/* 接收失败,关闭这个连接 */
lwip_close
(
sock
);
rt_kprintf
(
"
\n
send error,close the socket.
\r\n
"
);
rt_kprintf
(
"
\n
send error,close the socket.
\r\n
"
);
rt_free
(
recv_data
);
break
;
}
else
if
(
ret
==
0
)
{
/* 打印send函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Send warning,send function return 0.
\r\n
"
);
}
}
else
if
(
ret
==
0
)
{
/* 打印send函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Send warning,send function return 0.
\r\n
"
);
}
}
return
;
...
...
examples/network/tcpsendpacket.c
浏览文件 @
5f4bcfa3
#include <rtthread.h>
#include <lwip/netdb.h>
/* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/netdb.h>
/* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
void
tcp_senddata
(
const
char
*
url
,
int
port
,
int
length
)
{
struct
hostent
*
host
;
int
sock
,
err
,
result
,
timeout
,
index
;
struct
sockaddr_in
server_addr
;
rt_uint8_t
*
buffer_ptr
;
struct
hostent
*
host
;
int
sock
,
err
,
result
,
timeout
,
index
;
struct
sockaddr_in
server_addr
;
rt_uint8_t
*
buffer_ptr
;
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host
=
gethostbyname
(
url
);
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
))
==
-
1
)
{
/* 创建socket失败 */
rt_kprintf
(
"Socket error
\n
"
);
return
;
}
/* 通过函数入口参数url获得host地址(如果是域名,会做域名解析) */
host
=
gethostbyname
(
url
);
/* 创建一个socket,类型是SOCKET_STREAM,TCP类型 */
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
))
==
-
1
)
{
/* 创建socket失败 */
rt_kprintf
(
"Socket error
\n
"
);
return
;
}
/* 申请内存 */
buffer_ptr
=
rt_malloc
(
length
);
/* 构造发送数据 */
for
(
index
=
0
;
index
<
length
;
index
++
)
buffer_ptr
[
index
]
=
index
&
0xff
;
/* 申请内存 */
buffer_ptr
=
rt_malloc
(
length
);
/* 构造发送数据 */
for
(
index
=
0
;
index
<
length
;
index
++
)
buffer_ptr
[
index
]
=
index
&
0xff
;
timeout
=
100
;
/* 设置发送超时时间100ms */
lwip_setsockopt
(
sock
,
SOL_SOCKET
,
SO_SNDTIMEO
,
&
timeout
,
sizeof
(
timeout
));
/* 初始化预连接的服务端地址 */
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
));
timeout
=
100
;
/* 设置发送超时时间100ms */
lwip_setsockopt
(
sock
,
SOL_SOCKET
,
SO_SNDTIMEO
,
&
timeout
,
sizeof
(
timeout
));
/* 初始化预连接的服务端地址 */
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
));
/* 连接到服务端 */
err
=
connect
(
sock
,
(
struct
sockaddr
*
)
&
server_addr
,
sizeof
(
struct
sockaddr
));
rt_kprintf
(
"TCP thread connect error code: %d
\n
"
,
err
);
/* 连接到服务端 */
err
=
connect
(
sock
,
(
struct
sockaddr
*
)
&
server_addr
,
sizeof
(
struct
sockaddr
));
rt_kprintf
(
"TCP thread connect error code: %d
\n
"
,
err
);
while
(
1
)
{
/* 发送数据到sock连接 */
result
=
send
(
sock
,
buffer_ptr
,
length
,
MSG_DONTWAIT
);
if
(
result
==
-
1
)
//数据发送错误处理
{
rt_kprintf
(
"TCP thread send error: %d
\n
"
,
result
);
lwip_close
(
sock
);
//关闭连接,重新创建连接
rt_thread_delay
(
10
);
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
))
==
-
1
)
rt_kprintf
(
"TCP Socket error:%d
\n
"
,
sock
);
err
=
connect
(
sock
,
(
struct
sockaddr
*
)
&
server_addr
,
sizeof
(
struct
sockaddr
));
rt_kprintf
(
"TCP thread connect error code: %d
\n
"
,
err
);
}
else
if
(
result
==
0
)
{
/* 打印send函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Send warning,send function return 0.
\r\n
"
);
}
}
while
(
1
)
{
/* 发送数据到sock连接 */
result
=
send
(
sock
,
buffer_ptr
,
length
,
MSG_DONTWAIT
);
if
(
result
<
0
)
//数据发送错误处理
{
rt_kprintf
(
"TCP thread send error: %d
\n
"
,
result
);
lwip_close
(
sock
);
/* 关闭连接,重新创建连接 */
rt_thread_delay
(
10
);
if
((
sock
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
))
==
-
1
)
rt_kprintf
(
"TCP Socket error:%d
\n
"
,
sock
);
err
=
connect
(
sock
,
(
struct
sockaddr
*
)
&
server_addr
,
sizeof
(
struct
sockaddr
));
rt_kprintf
(
"TCP thread connect error code: %d
\n
"
,
err
);
}
else
if
(
result
==
0
)
{
/* 打印send函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Send warning,send function return 0.
\r\n
"
);
}
}
}
#ifdef RT_USING_FINSH
...
...
examples/network/tcpserver.c
浏览文件 @
5f4bcfa3
...
...
@@ -74,18 +74,19 @@ void tcpserv(void* parameter)
{
/* 发送数据到connected socket */
ret
=
send
(
connected
,
send_data
,
strlen
(
send_data
),
0
);
if
(
ret
<
0
)
{
/* 接收失败,关闭这个连接 */
lwip_close
(
sock
);
rt_kprintf
(
"
\n
send error,close the socket.
\r\n
"
);
break
;
}
else
if
(
ret
==
0
)
{
/* 打印send函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Send warning,send function return 0.
\r\n
"
);
}
if
(
ret
<
0
)
{
/* 发送失败,关闭这个连接 */
lwip_close
(
connected
);
rt_kprintf
(
"
\n
send error,close the socket.
\r\n
"
);
break
;
}
else
if
(
ret
==
0
)
{
/* 打印send函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Send warning,send function return 0.
\r\n
"
);
}
/* 从connected socket中接收数据,接收buffer是1024大小,但并不一定能够收到1024大小的数据 */
bytes_received
=
recv
(
connected
,
recv_data
,
1024
,
0
);
if
(
bytes_received
<
0
)
...
...
@@ -93,11 +94,13 @@ void tcpserv(void* parameter)
/* 接收失败,关闭这个connected socket */
lwip_close
(
connected
);
break
;
}
else
if
(
bytes_received
==
0
)
{
/* 打印recv函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Received warning,recv function return 0.
\r\n
"
);
}
}
else
if
(
bytes_received
==
0
)
{
/* 打印recv函数返回值为0的警告信息 */
rt_kprintf
(
"
\n
Received warning,recv function return 0.
\r\n
"
);
continue
;
}
/* 有接收到数据,把末端清零 */
recv_data
[
bytes_received
]
=
'\0'
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录