用C语言socket通信,写一个聊天对话,互相可以发送文字
时间: 2023-12-29 11:55:33 浏览: 64
用C语言实现socket通信
好的,以下是一个简单的C语言socket通信程序,可以实现聊天对话:
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1" // 服务器IP地址
#define SERVER_PORT 8080 // 服务器端口号
#define BUFFER_SIZE 1024 // 缓冲区大小
int main() {
int client_socket; // 客户端套接字
struct sockaddr_in server_addr; // 服务器地址
// 创建客户端套接字
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
printf("Failed to create socket.\n");
return -1;
}
// 初始化服务器地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &server_addr.sin_addr) <= 0) {
printf("Invalid server IP address.\n");
return -1;
}
// 连接服务器
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("Failed to connect to server.\n");
return -1;
}
// 发送和接收消息
char buffer[BUFFER_SIZE];
while (1) {
// 发送消息
printf("Enter message: ");
fgets(buffer, BUFFER_SIZE, stdin);
if (send(client_socket, buffer, strlen(buffer), 0) < 0) {
printf("Failed to send message.\n");
break;
}
// 接收消息
memset(buffer, 0, BUFFER_SIZE);
if (recv(client_socket, buffer, BUFFER_SIZE, 0) < 0) {
printf("Failed to receive message.\n");
break;
}
printf("Received message: %s", buffer);
}
// 关闭套接字
close(client_socket);
return 0;
}
```
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_PORT 8080 // 服务器端口号
#define BUFFER_SIZE 1024 // 缓冲区大小
int main() {
int server_socket, client_socket; // 服务器套接字和客户端套接字
struct sockaddr_in server_addr, client_addr; // 服务器地址和客户端地址
socklen_t client_addr_len = sizeof(client_addr); // 客户端地址长度
// 创建服务器套接字
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
printf("Failed to create socket.\n");
return -1;
}
// 初始化服务器地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
// 绑定服务器地址
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
printf("Failed to bind socket.\n");
return -1;
}
// 监听连接请求
if (listen(server_socket, 1) < 0) {
printf("Failed to listen socket.\n");
return -1;
}
// 接受连接请求
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket < 0) {
printf("Failed to accept connection.\n");
return -1;
}
// 发送和接收消息
char buffer[BUFFER_SIZE];
while (1) {
// 接收消息
memset(buffer, 0, BUFFER_SIZE);
if (recv(client_socket, buffer, BUFFER_SIZE, 0) < 0) {
printf("Failed to receive message.\n");
break;
}
printf("Received message: %s", buffer);
// 发送消息
printf("Enter message: ");
fgets(buffer, BUFFER_SIZE, stdin);
if (send(client_socket, buffer, strlen(buffer), 0) < 0) {
printf("Failed to send message.\n");
break;
}
}
// 关闭套接字
close(client_socket);
close(server_socket);
return 0;
}
```
运行客户端和服务器端程序,客户端输入消息后,服务器端会输出接收到的消息,并等待输入回复消息,客户端会接收到服务器端的回复消息。
阅读全文