Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <boost/asio.hpp>
- #include <fstream>
- #include <iostream>
- #include <string>
- #include <thread>
- #include <utility>
- #include "./cmake-build-debug/player.pb.h"
- using boost::asio::ip::tcp;
- // NOLINTNEXTLINE
- int main(int argc, char *argv[]) {
- boost::asio::io_context io_context;
- tcp::acceptor acceptor(io_context, tcp::endpoint(tcp::v4(), 8000));
- std::cout << "Listening at " << acceptor.local_endpoint() << std::endl;
- while (true) {
- tcp::socket s = acceptor.accept();
- std::thread([s = std::move(s)]() mutable {
- std::cout << "Connected client: " << s.remote_endpoint() << " --> " << s.local_endpoint() << std::endl;
- tcp::iostream client(std::move(s));
- std::string mes;
- client << "Hello from C++ server!\n";
- while (client) {
- std::string buffer;
- PlayerAction tmp_;
- if (client >> buffer){
- std::cout << buffer;
- }
- getline(client, buffer);
- tmp_.ParseFromString(buffer);
- std::string answer_from_server;
- std::cout << tmp_.key_pressed() << '\n';
- tmp_.SerializePartialToString(&answer_from_server);
- std::cout << "aboba\n";
- std::cout << answer_from_server << ' ' << answer_from_server.size();
- // std::cout << (answer_from_server != "");
- client << "take it back: " << answer_from_server << std::endl;
- // std::cout << "Received: " << msg << std::endl;
- }
- // client >> buffer;
- std::cout << "Disconnected" << std::endl;
- }).detach();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement