Ubuntu下C++网络编程实战

avatar
作者
猴君
阅读量:0

在 Ubuntu 下进行 C++ 网络编程实战,你需要了解一些基本的网络编程概念和相关库

  1. 安装必要的库和工具:
sudo apt-get update sudo apt-get install build-essential g++ cmake 
  1. 创建一个新的项目文件夹并进入:
mkdir cpp_networking_project cd cpp_networking_project 
  1. 创建一个名为 CMakeLists.txt 的文件,用于配置项目:
cmake_minimum_required(VERSION 3.10) project(cpp_networking_project)  set(CMAKE_CXX_STANDARD 14)  add_executable(server server.cpp) add_executable(client client.cpp) 
  1. 创建服务器(server.cpp)和客户端(client.cpp)源代码文件:

server.cpp:

#include<iostream> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <cstring>  const int PORT = 8080; const int BUFFER_SIZE = 1024;  int main() {     int server_fd, new_socket;     struct sockaddr_in address;     int addrlen = sizeof(address);     char buffer[BUFFER_SIZE] = {0};     char *message = "Hello from server!";      // 创建 socket     if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {         std::cerr << "Socket creation failed"<< std::endl;         return -1;     }      // 绑定 socket     address.sin_family = AF_INET;     address.sin_addr.s_addr = INADDR_ANY;     address.sin_port = htons(PORT);      if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {         std::cerr << "Bind failed"<< std::endl;         return -1;     }      // 监听连接     if (listen(server_fd, 3) < 0) {         std::cerr << "Listen failed"<< std::endl;         return -1;     }      // 接受连接     if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0) {         std::cerr << "Accept failed"<< std::endl;         return -1;     }      // 读取客户端发送的数据     read(new_socket, buffer, BUFFER_SIZE);     std::cout << "Message from client: "<< buffer<< std::endl;      // 向客户端发送消息     send(new_socket, message, strlen(message), 0);     std::cout << "Message sent to client"<< std::endl;      // 关闭 socket     close(new_socket);     close(server_fd);      return 0; } 

client.cpp:

#include<iostream> #include <sys/socket.h> #include <arpa/inet.h> #include <unistd.h> #include <cstring>  const std::string SERVER_IP = "127.0.0.1"; const int PORT = 8080; const int BUFFER_SIZE = 1024;  int main() {     int sock = 0;     struct sockaddr_in serv_addr;     char buffer[BUFFER_SIZE] = {0};     char *message = "Hello from client!";      // 创建 socket     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {         std::cerr << "Socket creation failed"<< std::endl;         return -1;     }      serv_addr.sin_family = AF_INET;     serv_addr.sin_port = htons(PORT);      // 将 IP 地址转换为二进制格式     if (inet_pton(AF_INET, SERVER_IP.c_str(), &serv_addr.sin_addr) <= 0) {         std::cerr << "Invalid address / Address not supported"<< std::endl;         return -1;     }      // 连接到服务器     if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {         std::cerr << "Connection failed"<< std::endl;         return -1;     }      // 向服务器发送消息     send(sock, message, strlen(message), 0);      // 从服务器接收消息     read(sock, buffer, BUFFER_SIZE);     std::cout << "Message from server: "<< buffer<< std::endl;      // 关闭 socket     close(sock);      return 0; } 
  1. 编译并运行项目:
cmake . make ./server & ./client 

这个例子展示了如何在 Ubuntu 下使用 C++ 进行基本的网络编程。你可以根据自己的需求修改代码,实现更复杂的功能。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!