Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
weixin_46254178
rt-thread
提交
5de51108
R
rt-thread
项目概览
weixin_46254178
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
1
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
5de51108
编写于
10月 28, 2017
作者:
B
Bernard Xiong
提交者:
GitHub
10月 28, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #914 from SummerGGift/20171027_patch_4
[Examples] update examples/network
上级
440a08f2
110ccb06
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
248 addition
and
17 deletion
+248
-17
examples/network/chargen.c
examples/network/chargen.c
+220
-0
examples/network/tcpclient.c
examples/network/tcpclient.c
+9
-6
examples/network/tcpserver.c
examples/network/tcpserver.c
+9
-6
examples/network/udpclient.c
examples/network/udpclient.c
+5
-3
examples/network/udpserver.c
examples/network/udpserver.c
+5
-2
未找到文件。
examples/network/chargen.c
0 → 100644
浏览文件 @
5de51108
#include <rtthread.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <dfs_posix.h>
#include "netdb.h"
#define MAX_SERV 32
/* Maximum number of chargen services. Don't need too many */
#define CHARGEN_THREAD_NAME "chargen"
#if RT_THREAD_PRIORITY_MAX == 32
#define CHARGEN_PRIORITY 20
/* Really low priority */
#else
#define CHARGEN_PRIORITY 200
/* Really low priority */
#endif
#define CHARGEN_THREAD_STACKSIZE 1024
struct
charcb
{
struct
charcb
*
next
;
int
socket
;
struct
sockaddr_in
cliaddr
;
socklen_t
clilen
;
char
nextchar
;
};
static
struct
charcb
*
charcb_list
=
0
;
static
int
do_read
(
struct
charcb
*
p_charcb
);
static
void
close_chargen
(
struct
charcb
*
p_charcb
);
extern
int
select
(
int
nfds
,
fd_set
*
readfds
,
fd_set
*
writefds
,
fd_set
*
exceptfds
,
struct
timeval
*
timeout
);
/**************************************************************
* void chargen_thread(void *arg)
*
* chargen task. This server will wait for connections on well
* known TCP port number: 19. For every connection, the server will
* write as much data as possible to the tcp port.
**************************************************************/
static
void
chargen_thread
(
void
*
arg
)
{
int
listenfd
;
struct
sockaddr_in
chargen_saddr
;
fd_set
readset
;
fd_set
writeset
;
int
i
,
maxfdp1
;
struct
charcb
*
p_charcb
;
/* First acquire our socket for listening for connections */
listenfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
IPPROTO_TCP
);
LWIP_ASSERT
(
"chargen_thread(): Socket create failed."
,
listenfd
>=
0
);
memset
(
&
chargen_saddr
,
0
,
sizeof
(
chargen_saddr
));
chargen_saddr
.
sin_family
=
AF_INET
;
chargen_saddr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
chargen_saddr
.
sin_port
=
htons
(
19
);
// Chargen server port
if
(
bind
(
listenfd
,
(
struct
sockaddr
*
)
&
chargen_saddr
,
sizeof
(
chargen_saddr
))
==
-
1
)
LWIP_ASSERT
(
"chargen_thread(): Socket bind failed."
,
0
);
/* Put socket into listening mode */
if
(
listen
(
listenfd
,
MAX_SERV
)
==
-
1
)
LWIP_ASSERT
(
"chargen_thread(): Listen failed."
,
0
);
/* Wait forever for network input: This could be connections or data */
for
(;;)
{
maxfdp1
=
listenfd
+
1
;
/* Determine what sockets need to be in readset */
FD_ZERO
(
&
readset
);
FD_ZERO
(
&
writeset
);
FD_SET
(
listenfd
,
&
readset
);
for
(
p_charcb
=
charcb_list
;
p_charcb
;
p_charcb
=
p_charcb
->
next
)
{
if
(
maxfdp1
<
p_charcb
->
socket
+
1
)
maxfdp1
=
p_charcb
->
socket
+
1
;
FD_SET
(
p_charcb
->
socket
,
&
readset
);
FD_SET
(
p_charcb
->
socket
,
&
writeset
);
}
/* Wait for data or a new connection */
i
=
select
(
maxfdp1
,
&
readset
,
&
writeset
,
0
,
0
);
if
(
i
==
0
)
continue
;
/* At least one descriptor is ready */
if
(
FD_ISSET
(
listenfd
,
&
readset
))
{
/* We have a new connection request!!! */
/* Lets create a new control block */
p_charcb
=
(
struct
charcb
*
)
rt_calloc
(
1
,
sizeof
(
struct
charcb
));
if
(
p_charcb
)
{
p_charcb
->
socket
=
accept
(
listenfd
,
(
struct
sockaddr
*
)
&
p_charcb
->
cliaddr
,
&
p_charcb
->
clilen
);
if
(
p_charcb
->
socket
<
0
)
rt_free
(
p_charcb
);
else
{
/* Keep this tecb in our list */
p_charcb
->
next
=
charcb_list
;
charcb_list
=
p_charcb
;
p_charcb
->
nextchar
=
0x21
;
}
}
else
{
/* No memory to accept connection. Just accept and then close */
int
sock
;
struct
sockaddr
cliaddr
;
socklen_t
clilen
;
sock
=
accept
(
listenfd
,
&
cliaddr
,
&
clilen
);
if
(
sock
>=
0
)
closesocket
(
sock
);
}
}
/* Go through list of connected clients and process data */
for
(
p_charcb
=
charcb_list
;
p_charcb
;
p_charcb
=
p_charcb
->
next
)
{
if
(
FD_ISSET
(
p_charcb
->
socket
,
&
readset
))
{
/* This socket is ready for reading. This could be because someone typed
* some characters or it could be because the socket is now closed. Try reading
* some data to see. */
if
(
do_read
(
p_charcb
)
<
0
)
break
;
}
if
(
FD_ISSET
(
p_charcb
->
socket
,
&
writeset
))
{
char
line
[
80
];
char
setchar
=
p_charcb
->
nextchar
;
for
(
i
=
0
;
i
<
59
;
i
++
)
{
line
[
i
]
=
setchar
;
if
(
++
setchar
==
0x7f
)
setchar
=
0x21
;
}
line
[
i
]
=
0
;
strcat
(
line
,
"
\n\r
"
);
if
(
write
(
p_charcb
->
socket
,
line
,
strlen
(
line
))
<
0
)
{
close_chargen
(
p_charcb
);
break
;
}
if
(
++
p_charcb
->
nextchar
==
0x7f
)
p_charcb
->
nextchar
=
0x21
;
}
}
}
}
/**************************************************************
* void close_chargen(struct charcb *p_charcb)
*
* Close the socket and remove this charcb from the list.
**************************************************************/
static
void
close_chargen
(
struct
charcb
*
p_charcb
)
{
struct
charcb
*
p_search_charcb
;
/* Either an error or tcp connection closed on other
* end. Close here */
closesocket
(
p_charcb
->
socket
);
/* Free charcb */
if
(
charcb_list
==
p_charcb
)
charcb_list
=
p_charcb
->
next
;
else
for
(
p_search_charcb
=
charcb_list
;
p_search_charcb
;
p_search_charcb
=
p_search_charcb
->
next
)
{
if
(
p_search_charcb
->
next
==
p_charcb
)
{
p_search_charcb
->
next
=
p_charcb
->
next
;
break
;
}
}
rt_free
(
p_charcb
);
}
/**************************************************************
* void do_read(struct charcb *p_charcb)
*
* Socket definitely is ready for reading. Read a buffer from the socket and
* discard the data. If no data is read, then the socket is closed and the
* charcb is removed from the list and freed.
**************************************************************/
static
int
do_read
(
struct
charcb
*
p_charcb
)
{
char
buffer
[
80
];
int
readcount
;
/* Read some data */
readcount
=
read
(
p_charcb
->
socket
,
&
buffer
,
80
);
if
(
readcount
<=
0
)
{
close_chargen
(
p_charcb
);
return
-
1
;
}
return
0
;
}
void
chargen_init
(
void
)
{
rt_thread_t
chargen
;
chargen
=
rt_thread_create
(
CHARGEN_THREAD_NAME
,
chargen_thread
,
RT_NULL
,
CHARGEN_THREAD_STACKSIZE
,
CHARGEN_PRIORITY
,
5
);
if
(
chargen
!=
RT_NULL
)
rt_thread_startup
(
chargen
);
}
#ifdef RT_USING_FINSH
#include <finsh.h>
void
chargen
()
{
chargen_init
();
}
FINSH_FUNCTION_EXPORT
(
chargen
,
start
chargen
server
);
#endif
examples/network/tcpclient.c
浏览文件 @
5de51108
#include <rtthread.h>
#include <rtthread.h>
#include <lwip/netdb.h>
/* 为了解析主机名,需要包含netdb.h头文件 */
//#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
//#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
#include <sys/socket.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h"
#define BUFSZ 1024
#define BUFSZ 1024
...
@@ -47,7 +50,7 @@ void tcpclient(const char* url, int port)
...
@@ -47,7 +50,7 @@ void tcpclient(const char* url, int port)
{
{
/* 连接失败 */
/* 连接失败 */
rt_kprintf
(
"Connect fail!
\n
"
);
rt_kprintf
(
"Connect fail!
\n
"
);
lwip_close
(
sock
);
closesocket
(
sock
);
/*释放接收缓冲 */
/*释放接收缓冲 */
rt_free
(
recv_data
);
rt_free
(
recv_data
);
...
@@ -61,7 +64,7 @@ void tcpclient(const char* url, int port)
...
@@ -61,7 +64,7 @@ void tcpclient(const char* url, int port)
if
(
bytes_received
<
0
)
if
(
bytes_received
<
0
)
{
{
/* 接收失败,关闭这个连接 */
/* 接收失败,关闭这个连接 */
lwip_close
(
sock
);
closesocket
(
sock
);
rt_kprintf
(
"
\n
received error,close the socket.
\r\n
"
);
rt_kprintf
(
"
\n
received error,close the socket.
\r\n
"
);
/* 释放接收缓冲 */
/* 释放接收缓冲 */
...
@@ -81,7 +84,7 @@ void tcpclient(const char* url, int port)
...
@@ -81,7 +84,7 @@ void tcpclient(const char* url, int port)
if
(
strcmp
(
recv_data
,
"q"
)
==
0
||
strcmp
(
recv_data
,
"Q"
)
==
0
)
if
(
strcmp
(
recv_data
,
"q"
)
==
0
||
strcmp
(
recv_data
,
"Q"
)
==
0
)
{
{
/* 如果是首字母是q或Q,关闭这个连接 */
/* 如果是首字母是q或Q,关闭这个连接 */
lwip_close
(
sock
);
closesocket
(
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
"
);
/* 释放接收缓冲 */
/* 释放接收缓冲 */
...
@@ -99,7 +102,7 @@ void tcpclient(const char* url, int port)
...
@@ -99,7 +102,7 @@ void tcpclient(const char* url, int port)
if
(
ret
<
0
)
if
(
ret
<
0
)
{
{
/* 接收失败,关闭这个连接 */
/* 接收失败,关闭这个连接 */
lwip_close
(
sock
);
closesocket
(
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
);
rt_free
(
recv_data
);
...
...
examples/network/tcpserver.c
浏览文件 @
5de51108
#include <rtthread.h>
#include <rtthread.h>
#include <lwip/sockets.h>
/* 使用BSD Socket接口必须包含sockets.h这个头文件 */
//#include <lwip/sockets.h> /* 使用BSD Socket接口必须包含sockets.h这个头文件 */
#include <sys/socket.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h"
static
const
char
send_data
[]
=
"This is TCP Server from RT-Thread."
;
/* 发送用到的数据 */
static
const
char
send_data
[]
=
"This is TCP Server from RT-Thread."
;
/* 发送用到的数据 */
void
tcpserv
(
void
*
parameter
)
void
tcpserv
(
void
*
parameter
)
...
@@ -77,7 +80,7 @@ void tcpserv(void* parameter)
...
@@ -77,7 +80,7 @@ void tcpserv(void* parameter)
if
(
ret
<
0
)
if
(
ret
<
0
)
{
{
/* 发送失败,关闭这个连接 */
/* 发送失败,关闭这个连接 */
lwip_close
(
connected
);
closesocket
(
connected
);
rt_kprintf
(
"
\n
send error,close the socket.
\r\n
"
);
rt_kprintf
(
"
\n
send error,close the socket.
\r\n
"
);
break
;
break
;
}
}
...
@@ -92,7 +95,7 @@ void tcpserv(void* parameter)
...
@@ -92,7 +95,7 @@ void tcpserv(void* parameter)
if
(
bytes_received
<
0
)
if
(
bytes_received
<
0
)
{
{
/* 接收失败,关闭这个connected socket */
/* 接收失败,关闭这个connected socket */
lwip_close
(
connected
);
closesocket
(
connected
);
break
;
break
;
}
}
else
if
(
bytes_received
==
0
)
else
if
(
bytes_received
==
0
)
...
@@ -107,13 +110,13 @@ void tcpserv(void* parameter)
...
@@ -107,13 +110,13 @@ void tcpserv(void* parameter)
if
(
strcmp
(
recv_data
,
"q"
)
==
0
||
strcmp
(
recv_data
,
"Q"
)
==
0
)
if
(
strcmp
(
recv_data
,
"q"
)
==
0
||
strcmp
(
recv_data
,
"Q"
)
==
0
)
{
{
/* 如果是首字母是q或Q,关闭这个连接 */
/* 如果是首字母是q或Q,关闭这个连接 */
lwip_close
(
connected
);
closesocket
(
connected
);
break
;
break
;
}
}
else
if
(
strcmp
(
recv_data
,
"exit"
)
==
0
)
else
if
(
strcmp
(
recv_data
,
"exit"
)
==
0
)
{
{
/* 如果接收的是exit,则关闭整个服务端 */
/* 如果接收的是exit,则关闭整个服务端 */
lwip_close
(
connected
);
closesocket
(
connected
);
stop
=
RT_TRUE
;
stop
=
RT_TRUE
;
break
;
break
;
}
}
...
@@ -126,7 +129,7 @@ void tcpserv(void* parameter)
...
@@ -126,7 +129,7 @@ void tcpserv(void* parameter)
}
}
/* 退出服务 */
/* 退出服务 */
lwip_close
(
sock
);
closesocket
(
sock
);
/* 释放接收缓冲 */
/* 释放接收缓冲 */
rt_free
(
recv_data
);
rt_free
(
recv_data
);
...
...
examples/network/udpclient.c
浏览文件 @
5de51108
#include <rtthread.h>
#include <rtthread.h>
#include <lwip/netdb.h>
/* 为了解析主机名,需要包含netdb.h头文件 */
//#include <lwip/netdb.h> /* 为了解析主机名,需要包含netdb.h头文件 */
#include <lwip/sockets.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
//#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
#include <sys/socket.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h"
const
char
send_data
[]
=
"This is UDP Client from RT-Thread.
\n
"
;
/* 发送用到的数据 */
const
char
send_data
[]
=
"This is UDP Client from RT-Thread.
\n
"
;
/* 发送用到的数据 */
void
udpclient
(
const
char
*
url
,
int
port
,
int
count
)
void
udpclient
(
const
char
*
url
,
int
port
,
int
count
)
...
@@ -40,7 +42,7 @@ void udpclient(const char* url, int port, int count)
...
@@ -40,7 +42,7 @@ void udpclient(const char* url, int port, int count)
}
}
/* 关闭这个socket */
/* 关闭这个socket */
lwip_close
(
sock
);
closesocket
(
sock
);
}
}
#ifdef RT_USING_FINSH
#ifdef RT_USING_FINSH
...
...
examples/network/udpserver.c
浏览文件 @
5de51108
#include <rtthread.h>
#include <rtthread.h>
#include <lwip/sockets.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
//#include <lwip/sockets.h> /* 使用BSD socket,需要包含sockets.h头文件 */
#include <sys/socket.h>
/* 使用BSD socket,需要包含sockets.h头文件 */
#include "netdb.h"
#define BUFSZ 1024
#define BUFSZ 1024
...
@@ -68,7 +71,7 @@ void udpserv(void* paramemter)
...
@@ -68,7 +71,7 @@ void udpserv(void* paramemter)
/* 如果接收数据是exit,退出 */
/* 如果接收数据是exit,退出 */
if
(
strcmp
(
recv_data
,
"exit"
)
==
0
)
if
(
strcmp
(
recv_data
,
"exit"
)
==
0
)
{
{
lwip_close
(
sock
);
closesocket
(
sock
);
/* 释放接收用的数据缓冲 */
/* 释放接收用的数据缓冲 */
rt_free
(
recv_data
);
rt_free
(
recv_data
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录