#include#include #include #include #include int main() { int fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY); if (fd < 0) { std::cerr << "Error: could not open serial port.\n"; return EXIT_FAILURE; } struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B115200); cfsetospeed(&options, B115200); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); options.c_oflag &= ~OPOST; options.c_cc[VMIN] = 1; options.c_cc[VTIME] = 0; tcsetattr(fd, TCSANOW, &options); return 0; }
#includeThis code initializes a session for an online chat application. It creates a TCP/IP socket and a server that listens for incoming connections on port 12345. Once a connection is established, the server reads a message from the client and prints it to the console. This code uses the boost.asio library.#include int main() { boost::asio::io_service io_service; boost::asio::ip::tcp::acceptor acceptor(io_service, boost::asio::ip::tcp::endpoint( boost::asio::ip::tcp::v4(), 12345)); boost::asio::ip::tcp::socket socket(io_service); acceptor.accept(socket); boost::asio::streambuf buf; boost::asio::read_until(socket, buf, '\n'); std::istream input_stream(&buf); std::string message; std::getline(input_stream, message); std::cout << message << std::endl; return 0; }