本文最后更新于 43 天前,其中的信息可能已经有所发展或是发生改变。
创建文件
创建文件并已如下形式放置
.
client.cpp
server.cpp
文件内容
client.cpp
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define SERVER_IP "127.0.0.1"
#define PORT 12345
#define BUFFER_SIZE 1024
int client_sock;
bool create_server_connect()
{
int ret;
struct sockaddr_in server_addr;
// 创建客户端套接字
client_sock = socket(AF_INET, SOCK_STREAM, 0);
if (client_sock < 0) {
perror("Socket creation failed");
return false;
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
// 将服务器的 IP 地址转换为网络字节序
ret = inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr);
if (ret <= 0) {
perror("Invalid address or address not supported");
close(client_sock);
return false;
}
// 连接到服务器
ret = connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret < 0) {
perror("Connection failed");
close(client_sock);
return false;
}
return true;
}
bool send_data(const char *message)
{
ssize_t sent_bytes;
sent_bytes = send(client_sock, message, strlen(message), 0);
if (sent_bytes < 0) {
perror("Send failed");
close(client_sock);
return false;
}
return true;
}
bool recv_socket_data(char *buffer, int buff_len)
{
ssize_t received_bytes;
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// 接收客户端数据
memset(buffer, '\0', buff_len);
received_bytes = recv(client_sock, buffer, buff_len - 1, 0);
if (received_bytes < 0) {
perror("Receive failed");
close(client_sock);
return false;
}
buffer[received_bytes] = '\0'; // 防止数组越界
return true;
}
int main()
{
bool ret;
char buffer[BUFFER_SIZE];
const char *message = "Hello, Server!";
ret = create_server_connect();
if (!ret) {
return -1;
}
std::cout << "Connected to server.\n";
ret = send_data(message);
if (!ret) {
return -1;
}
std::cout << "Message sent to server: " << message << "\n";
// 接收服务器响应
ret = recv_socket_data(buffer ,sizeof(buffer));
if (!ret) {
return -1;
}
std::cout << "Received from server: " << buffer << std::endl;
// 关闭客户端套接字
close(client_sock);
return 0;
}
server.cpp
#include <iostream>
#include <cstring>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define PORT 12345
#define BUFFER_SIZE 1024
int server_sock, client_sock;
bool create_socket_listen()
{
struct sockaddr_in server_addr;
int ret = 0;
// 创建服务器套接字
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if (server_sock < 0) {
perror("Socket creation failed");
return false;
}
// 配置服务器地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// 绑定套接字到指定端口
ret = bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
if (ret < 0) {
perror("Bind failed");
close(server_sock);
return false;
}
// 进入监听状态
ret = listen(server_sock, 5);
if (ret < 0) {
perror("Listen failed");
close(server_sock);
return false;
}
return true;
}
bool recv_socket_data(int server_sock, char *buffer, int buff_len)
{
ssize_t received_bytes;
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
// 接受客户端连接
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_sock < 0) {
perror("Accept failed");
close(server_sock);
return false;
}
// 接收客户端数据
memset(buffer, '\0', buff_len);
received_bytes = recv(client_sock, buffer, buff_len - 1, 0);
if (received_bytes < 0) {
perror("Receive failed");
close(client_sock);
close(server_sock);
return false;
}
buffer[received_bytes] = '\0'; // 防止数组越界
return true;
}
bool send_respond(const char *response)
{
ssize_t buff_len;
buff_len = send(client_sock, response, strlen(response), 0); // 发送响应数据
if (buff_len == -1) {
return false;
}
return true;
}
int main()
{
bool ret;
char buffer[BUFFER_SIZE] = {0};
const char *response = "Hello, Client!";
std::cout << "Server start" << std::endl;
ret = create_socket_listen();
if (!ret) {
return -1;
}
std::cout << "Server listening on port " << PORT << "...\n";
ret = recv_socket_data(server_sock, buffer, sizeof(buffer));
if (!ret) {
return -1;
}
std::cout << "Received from client: " << buffer << std::endl;
ret = send_respond(response);
if (!ret) {
return -1;
}
close(server_sock);
close(client_sock);
return 0;
}
程序分为server端,和client端,使用socket进行通信,端口使用12345
编译
执行如下代码进行编译
g++ server.cpp -o server
g++ client.cpp -o client
运行
分别在两个终端执行
首先运行server端
./server
然后运行client端
./client
结果
server端的终端可见打印如下
Server start
Server listening on port 12345...
Received from client: Hello, Server!
client端的终端可见打印如下
Connected to server.
Message sent to server: Hello, Server!
Received from server: Hello, Client!