mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-03 03:45:00 +00:00
53 lines
1.8 KiB
C++
53 lines
1.8 KiB
C++
#include <iostream>
|
|
#include <boost/asio.hpp>
|
|
#include <string>
|
|
|
|
using boost::asio::ip::tcp;
|
|
|
|
int main() {
|
|
try {
|
|
// Create an io_context object
|
|
boost::asio::io_context io_context;
|
|
|
|
// Resolve the server address and port
|
|
tcp::resolver resolver(io_context);
|
|
tcp::resolver::results_type endpoints = resolver.resolve("127.0.0.1", "4024");
|
|
|
|
// Create and connect the socket
|
|
tcp::socket socket(io_context);
|
|
boost::asio::connect(socket, endpoints);
|
|
|
|
std::cout << "Connected to the server." << std::endl;
|
|
|
|
// Receive initial message from the server
|
|
boost::asio::streambuf buffer;
|
|
boost::asio::read_until(socket, buffer, "\n");
|
|
std::istream is(&buffer);
|
|
std::string initial_message;
|
|
std::getline(is, initial_message);
|
|
std::cout << "Initial message from server: " << initial_message << std::endl;
|
|
|
|
// Send and receive messages
|
|
while (true) {
|
|
// Send a message to the server
|
|
std::string message;
|
|
std::cout << "Enter message: ";
|
|
std::getline(std::cin, message);
|
|
message += "\n"; // Add newline to mark the end of the message
|
|
boost::asio::write(socket, boost::asio::buffer(message));
|
|
|
|
// Receive a response from the server
|
|
boost::asio::streambuf response_buffer;
|
|
boost::asio::read_until(socket, response_buffer, "\n");
|
|
std::istream response_stream(&response_buffer);
|
|
std::string reply;
|
|
std::getline(response_stream, reply);
|
|
std::cout << "Reply from server: " << reply << std::endl;
|
|
}
|
|
|
|
} catch (std::exception& e) {
|
|
std::cerr << "Exception: " << e.what() << std::endl;
|
|
}
|
|
|
|
return 0;
|
|
} |