From 99e2e923d0c77b02f3fb4ff648ea916954868606 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 28 Feb 2025 23:13:39 +0800 Subject: chore(store): move everything to store. --- works/life/computer-network-experiment/client.cpp | 103 ---------------------- 1 file changed, 103 deletions(-) delete mode 100644 works/life/computer-network-experiment/client.cpp (limited to 'works/life/computer-network-experiment/client.cpp') diff --git a/works/life/computer-network-experiment/client.cpp b/works/life/computer-network-experiment/client.cpp deleted file mode 100644 index 73ae52f..0000000 --- a/works/life/computer-network-experiment/client.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/** Created by crupest. - * This is the client program. - */ - -#include "Common.h" -#include "IO.h" - -#include -#include - -#ifdef WIN32 -#include -#include -#else -#include -#include -#include -#endif - -const auto connect_address = "127.0.0.1"; // control connect address -const u_short port = 1234; // control connect port - -namespace { -folly::ProducerConsumerQueue send_queue(100); -folly::CancellationSource cancellation_source; -} // namespace - -void PrintHelp() { - SendOutput(CRUT("Input anything to send to server. Or just enter to receive " - "lastest messages from server.\n")); -} - -void OnInputLine(StringView line) { - if (line.empty()) { - return; - } else { - send_queue.write(ConvertCharStringBack(line) + '\n'); - } -} - -int Main() { - int client_socket; - - if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) == -1) { - PrintErrorMessageAndExit(CRUT("Failed to create socket!\n")); - } - - sockaddr_in server_address; - - server_address.sin_family = AF_INET; - server_address.sin_port = htons(port); - server_address.sin_addr.s_addr = inet_addr(connect_address); - memset(&(server_address.sin_zero), 0, sizeof(server_address.sin_zero)); - - if (connect(client_socket, (sockaddr *)&server_address, sizeof(sockaddr)) == - -1) { - PrintErrorMessageAndExit(CRUT("Failed to connect!")); - } - - output_stream << CRUT("Please input your name:\n> "); - String name = ReadInputLine(); - - PrintHelp(); - - StartIOThread(); - - name.push_back(CRUT('\n')); - auto name_data = ConvertCharStringBack(name); - SafeSend(client_socket, name_data); - - std::thread receive_thread([client_socket] { - std::string rest; - while (true) { - if (cancellation_source.isCancellationRequested()) { - break; - } - - std::string data; - if (!SafeReadUntil(client_socket, '\n', data, rest)) { - PrintErrorMessageAndExit(CRUT("Failed to receive message.\n")); - } - - SendOutput(CRUT("Recived a message:\n{}\n"), ConvertCharString(data)); - } - }); - receive_thread.detach(); - - while (true) { - if (cancellation_source.isCancellationRequested()) { - break; - } - - std::string s; - if (send_queue.read(s)) { - if (!SafeSend(client_socket, s)) { - PrintErrorMessageAndExit(CRUT("Failed to send message to server.")); - } - } - } - - CloseSocket(client_socket); - return 0; -} -- cgit v1.2.3