Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
KASAKI11
TDengine
提交
7ec34c40
T
TDengine
项目概览
KASAKI11
/
TDengine
与 Fork 源项目一致
Fork自
taosdata / TDengine
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7ec34c40
编写于
6月 02, 2020
作者:
S
Shuaiqiang Chang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: port
上级
7e02d5ef
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
278 addition
and
20 deletion
+278
-20
src/kit/taosnetwork/client.c
src/kit/taosnetwork/client.c
+22
-14
src/kit/taosnetwork/server.c
src/kit/taosnetwork/server.c
+20
-6
src/kit/taosnetwork/taosnetwork_client.c
src/kit/taosnetwork/taosnetwork_client.c
+66
-0
src/kit/taosnetwork/taosnetwork_server.c
src/kit/taosnetwork/taosnetwork_server.c
+57
-0
src/kit/taosnetwork/test_client.c
src/kit/taosnetwork/test_client.c
+50
-0
src/kit/taosnetwork/test_server.c
src/kit/taosnetwork/test_server.c
+63
-0
未找到文件。
src/kit/taosnetwork/client.c
浏览文件 @
7ec34c40
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <errno.h>
...
...
@@ -80,12 +94,6 @@ void *checkUPort(void *sarg) {
serverAddr
.
sin_addr
.
s_addr
=
inet_addr
(
host
);
printf
(
"=================================
\n
"
);
// if (connect(clientSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
// perror("connect");
// return NULL;
// }
// printf("Connect to: %s:%d...success\n", host, port);
sprintf
(
sendbuf
,
"send msg port_%d by udp"
,
port
);
...
...
@@ -108,21 +116,21 @@ void *checkUPort(void *sarg) {
int
main
()
{
int
port
=
6020
;
char
*
host
=
"127.0.0.1"
;
info
*
infos
=
malloc
(
10
*
sizeof
(
info
));
info
*
uinfo
s
=
malloc
(
10
*
sizeof
(
info
));
info
*
tinfo
=
malloc
(
sizeof
(
info
));
info
*
uinfo
=
malloc
(
sizeof
(
info
));
for
(
size_t
i
=
0
;
i
<
1
0
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
3
0
;
i
++
)
{
port
++
;
printf
(
"For test: %s:%d
\n
"
,
host
,
port
);
info
*
pinfo
=
infos
++
;
*
pinfo
->
host
=
host
;
pinfo
->
port
=
port
;
checkPort
(
pinfo
);
*
tinfo
->
host
=
host
;
tinfo
->
port
=
port
;
checkPort
(
tinfo
);
info
*
uinfo
=
uinfos
++
;
*
uinfo
->
host
=
host
;
uinfo
->
port
=
port
;
checkUPort
(
uinfo
);
}
free
(
tinfo
);
free
(
uinfo
);
}
\ No newline at end of file
src/kit/taosnetwork/server.c
浏览文件 @
7ec34c40
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <arpa/inet.h>
#include <errno.h>
...
...
@@ -158,11 +172,11 @@ static void *bindUPort(void *sarg) {
int
main
()
{
int
port
=
6020
;
pthread_t
*
pids
=
malloc
(
2
0
*
sizeof
(
pthread_t
));
info
*
infos
=
malloc
(
1
0
*
sizeof
(
info
));
info
*
uinfos
=
malloc
(
1
0
*
sizeof
(
info
));
pthread_t
*
pids
=
malloc
(
6
0
*
sizeof
(
pthread_t
));
info
*
infos
=
malloc
(
3
0
*
sizeof
(
info
));
info
*
uinfos
=
malloc
(
3
0
*
sizeof
(
info
));
for
(
size_t
i
=
0
;
i
<
1
0
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
3
0
;
i
++
)
{
port
++
;
info
*
pinfo
=
infos
++
;
...
...
@@ -177,13 +191,13 @@ int main() {
info
*
uinfo
=
uinfos
++
;
uinfo
->
port
=
port
;
uinfo
->
type
=
1
;
if
(
pthread_create
(
pids
+
1
0
+
i
,
NULL
,
bindUPort
,
uinfo
)
!=
0
)
//创建线程
if
(
pthread_create
(
pids
+
3
0
+
i
,
NULL
,
bindUPort
,
uinfo
)
!=
0
)
//创建线程
{
//创建线程失败
printf
(
"创建线程失败: %d.
\n
"
,
port
);
exit
(
0
);
}
}
for
(
int
i
=
0
;
i
<
1
0
;
i
++
)
{
for
(
int
i
=
0
;
i
<
3
0
;
i
++
)
{
pthread_join
(
pids
[
i
],
NULL
);
pthread_join
(
pids
[(
10
+
i
)],
NULL
);
}
...
...
src/kit/taosnetwork/taosnetwork_client.c
0 → 100644
浏览文件 @
7ec34c40
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define SERVER_PORT 8000
#define SIZE 200
int
main
()
{
struct
sockaddr_in
servaddr
,
cliaddr
;
socklen_t
cliaddr_len
;
int
client_sockfd
;
char
buf
[
SIZE
];
char
recvbuf
[
SIZE
];
int
i
,
n
,
flag
=
0
;
int
len
,
iDataNum
;
client_sockfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
bzero
(
&
servaddr
,
sizeof
(
servaddr
));
servaddr
.
sin_family
=
AF_INET
;
servaddr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
servaddr
.
sin_port
=
htons
(
SERVER_PORT
);
if
(
connect
(
client_sockfd
,
(
struct
sockaddr
*
)
&
servaddr
,
sizeof
(
servaddr
))
<
0
)
{
printf
(
"Connected error..
\n
"
);
return
0
;
}
printf
(
"Connected to server..
\n
"
);
/*循环的发送接收信息并打印接收信息(可以按需发送)--recv返回接收到的字节数,send返回发送的字节数*/
while
(
1
)
{
printf
(
"Enter string to send:"
);
scanf
(
"%s"
,
buf
);
if
(
!
strcmp
(
buf
,
"quit"
))
{
break
;
}
len
=
(
sizeof
buf
);
recvbuf
[
0
]
=
'\0'
;
iDataNum
=
recv
(
client_sockfd
,
recvbuf
,
SIZE
,
0
);
recvbuf
[
iDataNum
]
=
'\0'
;
printf
(
"%s
\n
"
,
recvbuf
);
}
return
0
;
}
src/kit/taosnetwork/taosnetwork_server.c
0 → 100644
浏览文件 @
7ec34c40
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define SERVER_PORT 8000
#define SIZE 200
int
main
()
{
struct
sockaddr_in
servaddr
,
cliaddr
;
socklen_t
cliaddr_len
;
int
listenfd
,
connfd
;
char
buf
[
BUFSIZ
];
int
i
,
n
,
flag
=
0
;
listenfd
=
socket
(
AF_INET
,
SOCK_STREAM
,
0
);
bzero
(
&
servaddr
,
sizeof
(
servaddr
));
servaddr
.
sin_family
=
AF_INET
;
servaddr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
servaddr
.
sin_port
=
htons
(
SERVER_PORT
);
bind
(
listenfd
,
(
struct
sockaddr
*
)
&
servaddr
,
sizeof
(
servaddr
));
listen
(
listenfd
,
20
);
printf
(
"Accepting connections..
\n
"
);
while
(
1
)
{
cliaddr_len
=
sizeof
(
cliaddr
);
connfd
=
accept
(
listenfd
,
(
struct
sockaddr
*
)
&
cliaddr
,
&
cliaddr_len
);
//如果得不到客户端发来的消息,将会被阻塞,一直等到消息到来
n
=
read
(
connfd
,
buf
,
SIZE
);
//如果n<=0,表示客户端已断开
while
(
1
)
{
if
(
n
!=
0
)
{
for
(
i
=
0
;
i
<
n
;
i
++
)
printf
(
"%c"
,
buf
[
i
]);
//输出客户端发来的信息
}
else
{
printf
(
"Client say close the connection..
\n
"
);
break
;
}
n
=
read
(
connfd
,
buf
,
SIZE
);
}
close
(
connfd
);
}
}
src/kit/taosnetwork/test_client.c
0 → 100644
浏览文件 @
7ec34c40
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define SERVER_PORT 8888
#define BUFF_LEN 512
#define SERVER_IP "172.0.5.182"
void
udp_msg_sender
(
int
fd
,
struct
sockaddr
*
dst
)
{}
/*
client:
socket-->sendto-->revcfrom-->close
*/
int
main
(
int
argc
,
char
*
argv
[])
{
int
client_fd
;
struct
sockaddr_in
ser_addr
;
client_fd
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
client_fd
<
0
)
{
printf
(
"create socket fail!
\n
"
);
return
-
1
;
}
memset
(
&
ser_addr
,
0
,
sizeof
(
ser_addr
));
ser_addr
.
sin_family
=
AF_INET
;
// ser_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
ser_addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
//注意网络序转换
ser_addr
.
sin_port
=
htons
(
SERVER_PORT
);
//注意网络序转换
socklen_t
len
;
struct
sockaddr_in
src
;
while
(
1
)
{
char
buf
[
BUFF_LEN
]
=
"TEST UDP MSG!
\n
"
;
len
=
sizeof
(
*
(
struct
sockaddr
*
)
&
ser_addr
);
printf
(
"client:%s
\n
"
,
buf
);
//打印自己发送的信息
sendto
(
client_fd
,
buf
,
BUFF_LEN
,
0
,
(
struct
sockaddr
*
)
&
ser_addr
,
len
);
memset
(
buf
,
0
,
BUFF_LEN
);
recvfrom
(
client_fd
,
buf
,
BUFF_LEN
,
0
,
(
struct
sockaddr
*
)
&
src
,
&
len
);
//接收来自server的信息
printf
(
"server:%s
\n
"
,
buf
);
sleep
(
1
);
//一秒发送一次消息
}
close
(
client_fd
);
return
0
;
}
\ No newline at end of file
src/kit/taosnetwork/test_server.c
0 → 100644
浏览文件 @
7ec34c40
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define SERVER_PORT 8888
#define BUFF_LEN 1024
void
handle_udp_msg
(
int
fd
)
{
char
buf
[
BUFF_LEN
];
//接收缓冲区,1024字节
socklen_t
len
;
int
count
;
struct
sockaddr_in
clent_addr
;
// clent_addr用于记录发送方的地址信息
while
(
1
)
{
memset
(
buf
,
0
,
BUFF_LEN
);
len
=
sizeof
(
clent_addr
);
count
=
recvfrom
(
fd
,
buf
,
BUFF_LEN
,
0
,
(
struct
sockaddr
*
)
&
clent_addr
,
&
len
);
// recvfrom是拥塞函数,没有数据就一直拥塞
if
(
count
==
-
1
)
{
printf
(
"recieve data fail!
\n
"
);
return
;
}
printf
(
"client:%s
\n
"
,
buf
);
//打印client发过来的信息
memset
(
buf
,
0
,
BUFF_LEN
);
sprintf
(
buf
,
"I have recieved %d bytes data!
\n
"
,
count
);
//回复client
printf
(
"server:%s
\n
"
,
buf
);
//打印自己发送的信息给
sendto
(
fd
,
buf
,
BUFF_LEN
,
0
,
(
struct
sockaddr
*
)
&
clent_addr
,
len
);
//发送信息给client,注意使用了clent_addr结构体指针
}
}
/*
server:
socket-->bind-->recvfrom-->sendto-->close
*/
int
main
(
int
argc
,
char
*
argv
[])
{
int
server_fd
,
ret
;
struct
sockaddr_in
ser_addr
;
server_fd
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
// AF_INET:IPV4;SOCK_DGRAM:UDP
if
(
server_fd
<
0
)
{
printf
(
"create socket fail!
\n
"
);
return
-
1
;
}
memset
(
&
ser_addr
,
0
,
sizeof
(
ser_addr
));
ser_addr
.
sin_family
=
AF_INET
;
ser_addr
.
sin_addr
.
s_addr
=
htonl
(
INADDR_ANY
);
// IP地址,需要进行网络序转换,INADDR_ANY:本地地址
ser_addr
.
sin_port
=
htons
(
SERVER_PORT
);
//端口号,需要网络序转换
ret
=
bind
(
server_fd
,
(
struct
sockaddr
*
)
&
ser_addr
,
sizeof
(
ser_addr
));
if
(
ret
<
0
)
{
printf
(
"socket bind fail!
\n
"
);
return
-
1
;
}
handle_udp_msg
(
server_fd
);
//处理接收到的数据
close
(
server_fd
);
return
0
;
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录