请你用c++ 写一个建立网络通信的 程序
时间: 2023-09-26 16:06:55 浏览: 81
C++网络通信
### 回答1:
好的,以下是一个使用 C 语言实现的简单的网络通信程序的例子:
```
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#define PORT 8080
#define MAX_BUF_LEN 1024
int main(int argc, char const *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_address;
char buffer[MAX_BUF_LEN];
// 创建 socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
printf("创建 socket 失败\n");
return 1;
}
// 填写服务器信息
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
// 绑定 socket 到服务器信息
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0) {
printf("绑定 socket 失败\n");
return 1;
}
// 监听 socket
if (listen(server_socket, 10) < 0) {
printf("监听 socket 失败\n");
return 1;
}
// 接收客户端连接请求
printf("等待客户端连接...\n");
client_socket = accept(server_socket, NULL, NULL);
if (client_socket < 0) {
printf("接收客户端连接请求失败\n");
return 1;
}
// 接收客户端的数据
int bytes_received = recv(client_socket, buffer, MAX_BUF_LEN, 0);
if (bytes_received < 0) {
printf("接收客户端数据失败\n");
return 1;
}
// 向客户端发送数据
int bytes_sent = send(client_socket, buffer, bytes_received, 0);
if (bytes_sent < 0) {
printf("向客户端发送数据失败\n");
return 1;
}
printf("已经成功接收
### 回答2:
在C语言中,我们可以使用socket库来建立网络通信的程序。下面是一个简单的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
int main() {
int sockfd, new_sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;
char buffer[1024];
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Error in socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;
memset(server_addr.sin_zero, '\0', sizeof(server_addr.sin_zero));
if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Error in binding");
exit(1);
}
if (listen(sockfd, 10) < 0) {
perror("Error in listening");
exit(1);
}
printf("Server waiting for client connection...\n");
addr_size = sizeof(client_addr);
new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &addr_size);
if (new_sockfd < 0) {
perror("Error in accepting");
exit(1);
}
printf("Client connected.\n");
strcpy(buffer, "Hello client!");
send(new_sockfd, buffer, strlen(buffer), 0);
memset(buffer, 0, sizeof(buffer));
recv(new_sockfd, buffer, sizeof(buffer), 0);
printf("Client: %s\n", buffer);
close(new_sockfd);
close(sockfd);
return 0;
}
```
这个程序创建了一个能够监听8080端口的套接字,并且等待客户端的连接。当客户端连接成功后,服务器向客户端发送"Hello client!"的消息,并且等待客户端的回复。回复收到后,服务器打印出客户端发送的消息,并且关闭连接。
请注意,这只是一个简单的例子来演示网络通信的基本原理,实际中可能需要更多的代码和处理来处理错误和其他功能。
### 回答3:
可以使用C语言编写一个建立网络通信的程序。以下是一个基本的例子:
```c
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8080
int main() {
// 创建socket
int server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Failed to create socket");
exit(EXIT_FAILURE);
}
// 绑定IP和端口号
struct sockaddr_in server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.s_addr = inet_addr(SERVER_IP);
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
perror("Failed to bind");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(server_socket, 3) == -1) {
perror("Failed to listen");
exit(EXIT_FAILURE);
}
printf("Waiting for incoming connections...\n");
// 接受连接请求
int client_socket;
struct sockaddr_in client_address;
int client_address_length = sizeof(client_address);
if ((client_socket = accept(server_socket, (struct sockaddr*)&client_address, (socklen_t*)&client_address_length)) == -1) {
perror("Failed to accept");
exit(EXIT_FAILURE);
}
printf("Connection accepted from %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
// 从客户端接收数据
char buffer[1024] = {0};
if (recv(client_socket, buffer, sizeof(buffer), 0) == -1) {
perror("Failed to receive data");
exit(EXIT_FAILURE);
}
printf("Received data: %s\n", buffer);
// 发送响应给客户端
const char* response = "Thank you for connecting!";
if (send(client_socket, response, strlen(response), 0) == -1) {
perror("Failed to send response");
exit(EXIT_FAILURE);
}
printf("Response sent.\n");
// 关闭socket
close(client_socket);
close(server_socket);
return 0;
}
```
这个程序创建了一个简单的服务端,它会监听指定端口上的连接请求。一旦接受到连接请求,它会从客户端接收数据并发送一个简单的响应。
请注意,此示例中使用的IP和端口号是示例值,你可能需要根据自己的需求进行修改。此外,该程序只是一个简单的示例,实际的网络通信程序通常会更加复杂和细致。
阅读全文