From fcd2304e04dd3fabdefce4293b1b76ad99f8ed73 Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 6 Jun 2021 22:13:43 +0800 Subject: import(life): ... --- .../computer-network-experiment/CMakeLists.txt | 8 +++-- works/life/computer-network-experiment/Common.cpp | 15 ++++++++- works/life/computer-network-experiment/Common.h | 2 ++ works/life/computer-network-experiment/client.cpp | 38 +++++++--------------- works/life/computer-network-experiment/server.cpp | 16 ++++----- 5 files changed, 40 insertions(+), 39 deletions(-) (limited to 'works/life/computer-network-experiment') diff --git a/works/life/computer-network-experiment/CMakeLists.txt b/works/life/computer-network-experiment/CMakeLists.txt index dc82736..4d4277c 100644 --- a/works/life/computer-network-experiment/CMakeLists.txt +++ b/works/life/computer-network-experiment/CMakeLists.txt @@ -10,10 +10,14 @@ set(CMAKE_CXX_STANDARD 17) find_package(fmt CONFIG REQUIRED) find_package(Microsoft.GSL CONFIG REQUIRED) add_library(base STATIC Common.cpp StringUtil.cpp Output.cpp) -target_link_libraries(base PUBLIC Microsoft.GSL::GSL fmt::fmt) +target_link_libraries(base PUBLIC Microsoft.GSL::GSL fmt::fmt Folly::folly) +if(WIN32) +target_link_libraries(base PUBLIC Ws2_32) +endif() add_executable(client client.cpp) +target_link_libraries(client PRIVATE base) add_executable(server server.cpp) find_package(folly CONFIG REQUIRED) -target_link_libraries(server PRIVATE base Folly::folly) +target_link_libraries(server PRIVATE base) diff --git a/works/life/computer-network-experiment/Common.cpp b/works/life/computer-network-experiment/Common.cpp index 02ac550..2e01dcb 100644 --- a/works/life/computer-network-experiment/Common.cpp +++ b/works/life/computer-network-experiment/Common.cpp @@ -5,7 +5,6 @@ #ifdef WIN32 #include #include -#pragma comment(lib, "Ws2_32.lib") #endif [[noreturn]] void PrintErrorMessageAndExit(StringView message, @@ -46,3 +45,17 @@ void InitWSA() { } } #endif + +int main() { +#ifdef WIN32 + InitWSA(); +#endif + + int c = Main(); + +#ifdef WIN32 + WSACleanup(); +#endif + + return c; +} diff --git a/works/life/computer-network-experiment/Common.h b/works/life/computer-network-experiment/Common.h index 1f4fa23..45a7da1 100644 --- a/works/life/computer-network-experiment/Common.h +++ b/works/life/computer-network-experiment/Common.h @@ -14,6 +14,8 @@ inline auto &output_stream = std::wcout; inline auto &error_stream = std::wcerr; #define CRUT(string_literal) L##string_literal +int Main(); + inline String ConvertCharString(std::string_view s) { return cru::ToUtf16WString(s); } diff --git a/works/life/computer-network-experiment/client.cpp b/works/life/computer-network-experiment/client.cpp index 926ece6..aeb352f 100644 --- a/works/life/computer-network-experiment/client.cpp +++ b/works/life/computer-network-experiment/client.cpp @@ -2,32 +2,22 @@ * This is the client program. */ +#include "Common.h" +#include "Output.h" + +#ifdef WIN32 #include -#include -#include #include - -#pragma comment(lib, "ws2_32.lib") +#endif const auto connect_address = "127.0.0.1"; // control connect address const u_short port = 1234; // control connect port -int main() { - WSADATA wsa_data; - - if (WSAStartup(MAKEWORD(2, 2), &wsa_data)) // initialize wsa - { - std::cerr << "WSA start up error!\n"; - WSACleanup(); - return 1; - } - +int Main() { int client_socket; if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { - std::cerr << "Failed to create socket!\n"; - WSACleanup(); - return 1; + PrintErrorMessageAndExit(CRUT("Failed to create socket!\n")); } sockaddr_in server_address; @@ -39,28 +29,22 @@ int main() { if (connect(client_socket, (sockaddr *)&server_address, sizeof(sockaddr)) == SOCKET_ERROR) { - std::cerr << "Failed to connect!"; - WSACleanup(); - return 1; + PrintErrorMessageAndExit(CRUT("Failed to connect!")); } const int buffer_size = 100; - char * buffer = new char[buffer_size]; + char *buffer = new char[buffer_size]; int received_number = recv(client_socket, buffer, buffer_size, 0); if (received_number == -1) { - std::cerr << "Failed to recv."; - WSACleanup(); - return 1; + PrintErrorMessageAndExit(CRUT("Failed to recv.")); } std::string s(buffer, received_number); - std::cout << "Received message:\n" << s; + SendOutput(CRUT("Received message:\n")); closesocket(client_socket); - WSACleanup(); - return 0; } diff --git a/works/life/computer-network-experiment/server.cpp b/works/life/computer-network-experiment/server.cpp index 297114b..9cf655a 100644 --- a/works/life/computer-network-experiment/server.cpp +++ b/works/life/computer-network-experiment/server.cpp @@ -2,15 +2,17 @@ * This is the server program. */ +#include "Common.h" #include "Output.h" -#include -#include #include -#include -#include #include +#ifdef WIN32 +#include +#include +#endif + const auto bind_address = "127.0.0.1"; // control bind address const u_short port = 1234; // control bind port @@ -48,11 +50,7 @@ void ResponseThreadProc(int socket, sockaddr_in address) { closesocket(socket); } -int main() { -#ifdef WIN32 - InitWSA(); -#endif - +int Main() { int server_socket; if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { -- cgit v1.2.3