在window环境下用标准C语言实现基于tcp协议的简易机器人聊天
时间: 2023-12-10 08:05:06 浏览: 112
实现基于TCP协议的简易机器人聊天,需要使用Socket编程。以下是一个简单的示例程序,你可以根据需要修改和扩展。
服务端代码(server.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define PORT 8888
int main(int argc, char *argv[]) {
WSADATA wsaData;
SOCKET server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int client_addr_len = sizeof(client_addr);
char buffer[1024];
char response[1024] = "Hello, I am a simple chatbot. What can I do for you?\r\n";
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return -1;
}
// Create server socket
server_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (server_socket == INVALID_SOCKET) {
printf("socket failed: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
// Bind server socket to port
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(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("bind failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return -1;
}
// Listen for incoming connections
if (listen(server_socket, 1) == SOCKET_ERROR) {
printf("listen failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return -1;
}
printf("Server started, listening on port %d...\n", PORT);
// Accept incoming connections
while (1) {
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(server_socket);
WSACleanup();
return -1;
}
printf("Client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Send welcome message to client
send(client_socket, response, strlen(response), 0);
// Receive and process messages from client
while (1) {
memset(buffer, 0, sizeof(buffer));
if (recv(client_socket, buffer, sizeof(buffer)-1, 0) == SOCKET_ERROR) {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(client_socket);
break;
}
printf("Received message from client: %s\n", buffer);
// Process message and generate response
// In this example, the bot simply echoes the message back to the client
sprintf(response, "You said: %s\r\n", buffer);
// Send response to client
send(client_socket, response, strlen(response), 0);
}
}
closesocket(server_socket);
WSACleanup();
return 0;
}
```
客户端代码(client.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8888
int main(int argc, char *argv[]) {
WSADATA wsaData;
SOCKET client_socket;
struct sockaddr_in server_addr;
char buffer[1024];
// Initialize Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed: %d\n", WSAGetLastError());
return -1;
}
// Create client socket
client_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (client_socket == INVALID_SOCKET) {
printf("socket failed: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
// Connect to server
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
printf("connect failed: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return -1;
}
printf("Connected to server at %s:%d\n", SERVER_IP, PORT);
// Receive welcome message from server
memset(buffer, 0, sizeof(buffer));
if (recv(client_socket, buffer, sizeof(buffer)-1, 0) == SOCKET_ERROR) {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return -1;
}
printf("%s", buffer);
// Send messages to server
while (1) {
memset(buffer, 0, sizeof(buffer));
printf("You: ");
fgets(buffer, sizeof(buffer), stdin);
// Send message to server
if (send(client_socket, buffer, strlen(buffer), 0) == SOCKET_ERROR) {
printf("send failed: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return -1;
}
// Receive response from server
memset(buffer, 0, sizeof(buffer));
if (recv(client_socket, buffer, sizeof(buffer)-1, 0) == SOCKET_ERROR) {
printf("recv failed: %d\n", WSAGetLastError());
closesocket(client_socket);
WSACleanup();
return -1;
}
printf("%s", buffer);
}
closesocket(client_socket);
WSACleanup();
return 0;
}
```
运行服务端程序后,可以在命令行窗口看到服务器已经启动并监听端口。然后运行客户端程序,输入服务器的IP地址和端口,即可与机器人聊天。
阅读全文