您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在Linux C++开发中,进行数据传输通常涉及以下几种方式:
#include <iostream>
#include <fstream>
#include <string>
int main() {
std::ifstream input_file("input.txt");
std::ofstream output_file("output.txt");
if (input_file.is_open() && output_file.is_open()) {
std::string line;
while (getline(input_file, line)) {
output_file << line << std::endl;
}
input_file.close();
output_file.close();
} else {
std::cerr << "Error opening files." << std::endl;
}
return 0;
}
客户端示例:
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
std::cerr << "Error creating socket." << std::endl;
return 1;
}
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
inet_pton(AF_INET, "127.0.0.1", &server_addr.sin_addr);
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
std::cerr << "Error connecting to server." << std::endl;
return 1;
}
std::string message = "Hello, Server!";
send(client_socket, message.c_str(), message.length(), 0);
close(client_socket);
return 0;
}
服务器端示例:
#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
std::cerr << "Error creating socket." << std::endl;
return 1;
}
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
std::cerr << "Error binding socket." << std::endl;
return 1;
}
if (listen(server_socket, 5) == -1) {
std::cerr << "Error listening on socket." << std::endl;
return 1;
}
sockaddr_in client_addr;
socklen_t client_addr_size = sizeof(client_addr);
int client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_addr_size);
if (client_socket == -1) {
std::cerr << "Error accepting connection." << std::endl;
return 1;
}
char buffer[1024];
int received = recv(client_socket, buffer, sizeof(buffer) - 1, 0);
if (received == -1) {
std::cerr << "Error receiving data." << std::endl;
return 1;
}
buffer[received] = '\0';
std::cout << "Received message: " << buffer << std::endl;
close(client_socket);
close(server_socket);
return 0;
}
有名管道示例:
#include <iostream>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int main() {
int pipefds[2];
if (pipe(pipefds) == -1) {
std::cerr << "Error creating pipe." << std::endl;
return 1;
}
pid_t pid = fork();
if (pid == -1) {
std::cerr << "Error forking process." << std::endl;
return 1;
} else if (pid == 0) { // 子进程
close(pipefds[1]); // 关闭写端
char buffer[1024];
read(pipefds[0], buffer, sizeof(buffer) - 1);
buffer[received] = '\0';
std::cout << "Received message from parent: " << buffer << std::endl;
close(pipefds[0]); // 关闭读端
} else { // 父进程
close(pipefds[0]); // 关闭读端
std::string message = "Hello, Child!";
write(pipefds[1], message.c_str(), message.length());
close(pipefds[1]); // 关闭写端
}
return 0;
}
这些方法可以根据实际需求进行选择,以实现Linux C++开发中的数据传输。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。