-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2020 the gRPC authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
licenses(["notice"]) | ||
|
||
cc_binary( | ||
name = "client", | ||
srcs = ["client.cc"], | ||
defines = ["BAZEL_BUILD"], | ||
deps = [ | ||
"@com_google_absl//absl/flags:flag", | ||
"@com_google_absl//absl/flags:parse", | ||
"@com_google_absl//absl/strings:str_format", | ||
], | ||
linkshared = False, | ||
linkstatic = True, | ||
|
||
) | ||
|
||
|
||
cc_binary( | ||
name = "server", | ||
srcs = ["server.cc"], | ||
defines = ["BAZEL_BUILD"], | ||
deps = [ | ||
"@com_google_absl//absl/flags:flag", | ||
"@com_google_absl//absl/flags:parse", | ||
"@com_google_absl//absl/strings:str_format", | ||
], | ||
linkshared = False, | ||
linkstatic = True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <unistd.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
#include <arpa/inet.h> | ||
|
||
#include "absl/flags/flag.h" | ||
#include "absl/flags/parse.h" | ||
#include "absl/strings/str_format.h" | ||
|
||
#include <arpa/inet.h> | ||
#include <netdb.h> | ||
|
||
ABSL_FLAG(std::string, ip, "127.0.0.1", "Server address"); | ||
ABSL_FLAG(uint16_t, port, 50051, "Server port for the service"); | ||
ABSL_FLAG(uint32_t, loop, 1000, "client call loop times"); | ||
ABSL_FLAG(uint32_t, length, 25000, "send data bytes each time"); | ||
|
||
std::string convert2IP(std::string ip){ | ||
|
||
const char* hostname = ip.c_str(); | ||
struct addrinfo hints, *res; | ||
|
||
memset(&hints, 0, sizeof hints); | ||
hints.ai_family = AF_INET; // 使用IPv4 | ||
|
||
int status = getaddrinfo(hostname, NULL, &hints, &res); | ||
if (status != 0) { | ||
std::cerr << "getaddrinfo error: " << gai_strerror(status) << std::endl; | ||
return ""; | ||
} | ||
|
||
struct sockaddr_in* addr = (struct sockaddr_in*)res->ai_addr; | ||
char ip_address[INET_ADDRSTRLEN]; | ||
inet_ntop(AF_INET, &(addr->sin_addr), ip_address, INET_ADDRSTRLEN); | ||
|
||
std::cout << "IP Address of " << hostname << " is: " << ip_address << std::endl; | ||
|
||
freeaddrinfo(res); | ||
return ip_address; | ||
} | ||
int RunClient(uint16_t port,int loop,std::string ip,int length){ | ||
ip = convert2IP(ip); | ||
int sock = 0, valread = 0; | ||
struct sockaddr_in serv_addr; | ||
std::string send_data(length, 'a'); | ||
const char *hello = send_data.c_str(); | ||
char buffer[102400] = {0}; | ||
|
||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | ||
std::cerr << "Socket creation error" << std::endl; | ||
return -1; | ||
} | ||
|
||
serv_addr.sin_family = AF_INET; | ||
serv_addr.sin_port = htons(port); | ||
|
||
// 将 IP 地址从字符串转换为网络地址 | ||
if(inet_pton(AF_INET,ip.c_str(), &serv_addr.sin_addr)<=0) { | ||
std::cerr << "Invalid address/ Address not supported" << std::endl; | ||
return -1; | ||
} | ||
|
||
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { | ||
std::cerr << "Connection Failed" << std::endl; | ||
return -1; | ||
} | ||
auto s= std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()) | ||
.count(); | ||
for(int i=0;i<loop;++i){ | ||
// 发送消息给服务器 | ||
send(sock, hello, strlen(hello), 0); | ||
// std::cout << "Hello message sent" << std::endl; | ||
|
||
// 从服务器接收消息 | ||
valread = read(sock, buffer, 1024); | ||
// std::cout << "Server: " << buffer << std::endl; | ||
} | ||
auto e= std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()) | ||
.count(); | ||
std::cout << "=== socket;"<<"port:"<<port<<";loop:"<<loop<<";length:"<<length<<";time:" << e - s << "us ===" << std::endl; | ||
return 0; | ||
} | ||
int main(int argc, char** argv) { | ||
absl::ParseCommandLine(argc, argv); | ||
RunClient(absl::GetFlag(FLAGS_port),absl::GetFlag(FLAGS_loop),absl::GetFlag(FLAGS_ip),absl::GetFlag(FLAGS_length)); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <iostream> | ||
#include <cstring> | ||
#include <unistd.h> | ||
#include <sys/socket.h> | ||
#include <netinet/in.h> | ||
|
||
#include "absl/flags/flag.h" | ||
#include "absl/flags/parse.h" | ||
#include "absl/strings/str_format.h" | ||
|
||
ABSL_FLAG(uint16_t, port, 50051, "Server port for the service"); | ||
ABSL_FLAG(uint32_t, length, 25000, "send data bytes each time"); | ||
int RunServer(uint16_t port,int length) { | ||
std::cout<<"========== mydebug: start socket server port:"<<port<<std::endl; | ||
int server_fd, new_socket; | ||
struct sockaddr_in address; | ||
int addrlen = sizeof(address); | ||
char buffer[102400] = {0}; | ||
const char *hello = "Hello from server"; | ||
// 创建 Socket | ||
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { | ||
perror("socket failed"); | ||
return 1; | ||
} | ||
|
||
address.sin_family = AF_INET; | ||
address.sin_addr.s_addr = INADDR_ANY; | ||
address.sin_port = htons(port); | ||
|
||
// 绑定端口 | ||
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) { | ||
perror("bind failed"); | ||
return 1; | ||
} | ||
|
||
// 监听 | ||
if (listen(server_fd, 3) < 0) { | ||
perror("listen failed"); | ||
return 1; | ||
} | ||
|
||
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) { | ||
perror("accept failed"); | ||
return 1; | ||
} | ||
|
||
while(true){ | ||
// 从客户端接收消息 | ||
read(new_socket, buffer, length); | ||
// std::cout << "Client: " << buffer << std::endl; | ||
|
||
// 发送消息给客户端 | ||
send(new_socket, hello, strlen(hello), 0); | ||
// std::cout << "Hello message sent" << std::endl; | ||
} | ||
|
||
return 0; | ||
} | ||
int main(int argc, char** argv) { | ||
absl::ParseCommandLine(argc, argv); | ||
RunServer(absl::GetFlag(FLAGS_port),absl::GetFlag(FLAGS_length)); | ||
return 0; | ||
} |