使用C++x写一段代码在linux下使用socket编程实现客户端与服务端对话
时间: 2023-08-31 17:39:14 浏览: 173
### 回答1:
这是一段 Linux 下使用 C 语言实现 socket 编程的示例代码(客户端):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAXDATASIZE 100
int main(int argc, char *argv[]) {
int sockfd, num;
char buf[MAXDATASIZE];
struct sockaddr_in server;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket() error.");
exit(1);
}
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr.s_addr = inet_addr("127.0.0.1");
bzero(&(server.sin_zero), 8);
if (connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
perror("connect() error.");
exit(1);
}
if ((num = recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
perror("recv() error.");
exit(1);
}
buf[num - 1] = '\0';
printf("Server Message: %s\n", buf);
close(sockfd);
return 0;
}
```
这是一段 Linux 下使用 C 语言实现 socket 编程的示例代码(服务器端):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8888
#define BACKLOG 1
int main() {
int sockfd, new_fd;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket() error.");
exit(1);
}
int opt = SO_REUSEADDR;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
server.sin_addr
### 回答2:
下面是一个使用C语言编写的在Linux下使用Socket编程实现客户端和服务端对话的示例代码:
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define MAX_BUFFER_SIZE 1024
int main()
{
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
char buffer[MAX_BUFFER_SIZE];
int str_len;
socklen_t client_address_size;
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("socket() error");
exit(1);
}
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(1234);
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
perror("bind() error");
exit(1);
}
if (listen(server_socket, 5) == -1) {
perror("listen() error");
exit(1);
}
client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_size);
if (client_socket == -1) {
perror("accept() error");
exit(1);
}
while (1) {
str_len = read(client_socket, buffer, MAX_BUFFER_SIZE - 1);
if (str_len == -1) {
perror("read() error");
exit(1);
}
buffer[str_len] = '\0';
printf("Client: %s\n", buffer);
printf("Server: ");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
if (!strcmp(buffer, "q\n") || !strcmp(buffer, "Q\n")) {
break;
}
write(client_socket, buffer, strlen(buffer));
}
close(client_socket);
close(server_socket);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define MAX_BUFFER_SIZE 1024
int main()
{
int client_socket;
struct sockaddr_in server_address;
char buffer[MAX_BUFFER_SIZE];
int str_len;
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("socket() error");
exit(1);
}
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_port = htons(1234);
server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
perror("connect() error");
exit(1);
}
while (1) {
printf("Client: ");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
if (!strcmp(buffer, "q\n") || !strcmp(buffer, "Q\n")) {
break;
}
write(client_socket, buffer, strlen(buffer));
str_len = read(client_socket, buffer, MAX_BUFFER_SIZE - 1);
if (str_len == -1) {
perror("read() error");
exit(1);
}
buffer[str_len] = '\0';
printf("Server: %s\n", buffer);
}
close(client_socket);
return 0;
}
```
以上是一个简单的客户端和服务端对话的例子,运行服务端程序后,再运行客户端程序就可以实现在终端上进行对话。客户端输入的内容会发送给服务端,在服务端的终端显示,并等待服务端的回复,服务端在接收到客户端的消息后会回复给客户端,并在客户端的终端显示。当输入"q"或"Q"时,客户端和服务端会断开连接并退出程序。
### 回答3:
下面是一段使用C语言在Linux下实现客户端与服务端对话的代码:
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_address;
char buffer[BUF_SIZE];
// 创建套接字
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("创建套接字失败");
exit(1);
}
// 设置服务器地址
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); // 服务器IP地址
server_address.sin_port = htons(8888); // 服务器端口号
// 连接服务器
if (connect(client_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
perror("连接服务器失败");
exit(1);
}
while (1) {
// 输入要发送的消息
printf("请输入要发送的消息(输入exit退出):");
fgets(buffer, BUF_SIZE, stdin);
// 发送消息给服务器
if (send(client_socket, buffer, strlen(buffer), 0) == -1) {
perror("发送消息给服务器失败");
exit(1);
}
// 退出循环的条件
if (strcmp(buffer, "exit\n") == 0) {
break;
}
// 接收服务器返回的消息
memset(buffer, 0, BUF_SIZE);
if (recv(client_socket, buffer, BUF_SIZE - 1, 0) == -1) {
perror("接收服务器消息失败");
exit(1);
}
// 打印服务器返回的消息
printf("服务器消息:%s", buffer);
}
// 关闭套接字
close(client_socket);
return 0;
}
```
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define BUF_SIZE 1024
int main() {
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_size;
char buffer[BUF_SIZE];
// 创建套接字
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("创建套接字失败");
exit(1);
}
// 设置服务器地址
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY); // 任意IP地址
server_address.sin_port = htons(8888); // 服务器监听端口号
// 绑定套接字
if (bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) == -1) {
perror("绑定套接字失败");
exit(1);
}
// 监听套接字
if (listen(server_socket, 5) == -1) {
perror("监听套接字失败");
exit(1);
}
printf("等待客户端连接...\n");
// 接受客户端连接
client_address_size = sizeof(client_address);
client_socket = accept(server_socket, (struct sockaddr*)&client_address, &client_address_size);
if (client_socket == -1) {
perror("接受客户端连接失败");
exit(1);
}
printf("客户端连接成功\n");
while (1) {
// 接收客户端发送的消息
memset(buffer, 0, BUF_SIZE);
if (recv(client_socket, buffer, BUF_SIZE - 1, 0) == -1) {
perror("接收客户端消息失败");
exit(1);
}
// 退出循环的条件
if (strcmp(buffer, "exit\n") == 0) {
break;
}
// 打印客户端发送的消息
printf("客户端消息:%s", buffer);
// 回复客户端消息
if (send(client_socket, buffer, strlen(buffer), 0) == -1) {
perror("回复客户端消息失败");
exit(1);
}
}
// 关闭套接字
close(client_socket);
close(server_socket);
return 0;
}
```
这段代码实现了一个简单的客户端和服务端对话的功能。客户端使用`socket`函数创建了一个套接字,然后通过`connect`函数连接到服务器上。连接成功后,进入一个循环,循环中首先通过`fgets`函数输入要发送的消息,然后通过`send`函数将消息发送给服务器。接着接收服务器返回的消息,再打印出来。如果输入的消息是"exit",则退出循环,关闭套接字,程序结束。
服务端使用`socket`函数创建一个套接字,并设置服务器地址。然后通过`bind`函数将套接字与服务器地址绑定,并通过`listen`函数监听套接字。接着使用`accept`函数接受客户端连接,接收成功后进入一个循环。循环中首先接收客户端发送的消息,然后通过`send`函数将消息回复给客户端。如果接收到的消息是"exit",则退出循环,关闭套接字,程序结束。
阅读全文