提交 7ec34c40 编写于 作者: S Shuaiqiang Chang

fix: port

上级 7e02d5ef
/*
* 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 <arpa/inet.h>
#include <errno.h> #include <errno.h>
...@@ -80,12 +94,6 @@ void *checkUPort(void *sarg) { ...@@ -80,12 +94,6 @@ void *checkUPort(void *sarg) {
serverAddr.sin_addr.s_addr = inet_addr(host); serverAddr.sin_addr.s_addr = inet_addr(host);
printf("=================================\n"); 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); sprintf(sendbuf, "send msg port_%d by udp", port);
...@@ -108,21 +116,21 @@ void *checkUPort(void *sarg) { ...@@ -108,21 +116,21 @@ void *checkUPort(void *sarg) {
int main() { int main() {
int port = 6020; int port = 6020;
char *host = "127.0.0.1"; char *host = "127.0.0.1";
info *infos = malloc(10 * sizeof(info)); info *tinfo = malloc(sizeof(info));
info *uinfos = malloc(10 * sizeof(info)); info *uinfo = malloc(sizeof(info));
for (size_t i = 0; i < 10; i++) { for (size_t i = 0; i < 30; i++) {
port++; port++;
printf("For test: %s:%d\n", host, port); printf("For test: %s:%d\n", host, port);
info *pinfo = infos++; *tinfo->host = host;
*pinfo->host = host; tinfo->port = port;
pinfo->port = port; checkPort(tinfo);
checkPort(pinfo);
info *uinfo = uinfos++;
*uinfo->host = host; *uinfo->host = host;
uinfo->port = port; uinfo->port = port;
checkUPort(uinfo); checkUPort(uinfo);
} }
free(tinfo);
free(uinfo);
} }
\ No newline at end of file
/*
* 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 <arpa/inet.h>
#include <errno.h> #include <errno.h>
...@@ -158,11 +172,11 @@ static void *bindUPort(void *sarg) { ...@@ -158,11 +172,11 @@ static void *bindUPort(void *sarg) {
int main() { int main() {
int port = 6020; int port = 6020;
pthread_t *pids = malloc(20 * sizeof(pthread_t)); pthread_t *pids = malloc(60 * sizeof(pthread_t));
info * infos = malloc(10 * sizeof(info)); info * infos = malloc(30 * sizeof(info));
info * uinfos = malloc(10 * sizeof(info)); info * uinfos = malloc(30 * sizeof(info));
for (size_t i = 0; i < 10; i++) { for (size_t i = 0; i < 30; i++) {
port++; port++;
info *pinfo = infos++; info *pinfo = infos++;
...@@ -177,13 +191,13 @@ int main() { ...@@ -177,13 +191,13 @@ int main() {
info *uinfo = uinfos++; info *uinfo = uinfos++;
uinfo->port = port; uinfo->port = port;
uinfo->type = 1; uinfo->type = 1;
if (pthread_create(pids + 10 + i, NULL, bindUPort, uinfo) != 0) //创建线程 if (pthread_create(pids + 30 + i, NULL, bindUPort, uinfo) != 0) //创建线程
{ //创建线程失败 { //创建线程失败
printf("创建线程失败: %d.\n", port); printf("创建线程失败: %d.\n", port);
exit(0); exit(0);
} }
} }
for (int i = 0; i < 10; i++) { for (int i = 0; i < 30; i++) {
pthread_join(pids[i], NULL); pthread_join(pids[i], NULL);
pthread_join(pids[(10 + i)], NULL); pthread_join(pids[(10 + i)], NULL);
} }
......
/*
* 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;
}
/*
* 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);
}
}
#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
#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.
先完成此消息的编辑!
想要评论请 注册