在windows环境下用C语言完成基于TCP协议的简易机器人通讯
时间: 2024-02-05 18:13:57 浏览: 69
首先,你需要一个TCP服务器和TCP客户端程序,可以使用C语言编写。以下是一个简单的示例程序:
服务器端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUF_SIZE 1024
#define PORT 8888
int main()
{
WSADATA wsa;
SOCKET s, new_socket;
struct sockaddr_in server, client;
int c, recv_len;
char buf[BUF_SIZE];
char *response = "Hello, I'm a simple chatbot! How can I help you?\n";
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
//Bind
if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d" , WSAGetLastError());
}
puts("Bind done.");
//Listen to incoming connections
listen(s , 3);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
while( (new_socket = accept(s , (struct sockaddr *)&client, &c)) != INVALID_SOCKET )
{
puts("Connection accepted");
//Reply to the client
send(new_socket , response , strlen(response) , 0);
//Receive a message from the client
while( (recv_len = recv(new_socket , buf, BUF_SIZE, 0)) != SOCKET_ERROR )
{
//Process the message and generate response
//Here you can call your AI chatbot code to generate the response
printf("Received message: %s\n", buf);
char *reply = "I'm sorry, I don't understand. Can you please rephrase?";
send(new_socket , reply , strlen(reply) , 0);
}
if(recv_len == SOCKET_ERROR)
{
printf("recv failed with error code : %d" , WSAGetLastError());
}
closesocket(new_socket);
puts("Connection closed");
}
if (new_socket == INVALID_SOCKET)
{
printf("accept failed with error code : %d" , WSAGetLastError());
return 1;
}
closesocket(s);
WSACleanup();
return 0;
}
```
客户端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") //Winsock Library
#define BUF_SIZE 1024
#define SERVER_ADDR "127.0.0.1"
#define PORT 8888
int main()
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char message[BUF_SIZE], response[BUF_SIZE];
int recv_len;
printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}
printf("Initialised.\n");
//Create a socket
if((s = socket(AF_INET, SOCK_STREAM, 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}
printf("Socket created.\n");
//Prepare the sockaddr_in structure
server.sin_addr.s_addr = inet_addr(SERVER_ADDR);
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
printf("Connect error");
return 1;
}
puts("Connected");
//Receive a message from the server
if((recv_len = recv(s , response , BUF_SIZE, 0)) == SOCKET_ERROR)
{
printf("recv failed with error code : %d" , WSAGetLastError());
}
else
{
response[recv_len] = '\0';
printf("Server response: %s\n", response);
}
while(1)
{
printf("Enter message : ");
fgets(message, BUF_SIZE, stdin);
//Send some data
if(send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}
//Receive a reply from the server
if((recv_len = recv(s , response , BUF_SIZE, 0)) == SOCKET_ERROR)
{
printf("recv failed with error code : %d" , WSAGetLastError());
break;
}
response[recv_len] = '\0';
printf("Server response: %s\n", response);
}
closesocket(s);
WSACleanup();
return 0;
}
```
这两个程序可以在Windows平台下使用。你可以运行服务器程序,然后运行客户端程序与它建立连接。当客户端发送消息时,服务器将收到该消息并生成一个响应。你可以在服务器程序中调用你的AI chatbot代码来生成响应。
阅读全文