在windows中用C++完成以下功能,在主函数中插入TCP socket客户端通讯函数,当程序启动时与主机通讯,当返回特定字符串后进行主函数之后的代码。主函数完成后利用TCP发送特定字符串,并关闭TCP通讯
时间: 2024-05-12 22:20:24 浏览: 101
TCP服务器和客户端C++类,提供简单的TCP通讯功能.zip
以下是一个示例代码,实现了上述功能:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 12345
#define RESPONSE_MSG "Hello, world!"
void client_communication() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return;
}
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to create socket.\n");
WSACleanup();
return;
}
struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddress.sin_port = htons(SERVER_PORT);
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
printf("Failed to connect to server.\n");
closesocket(clientSocket);
WSACleanup();
return;
}
char buffer[1024];
int len = recv(clientSocket, buffer, 1024, 0);
if (len == SOCKET_ERROR) {
printf("Failed to receive data.\n");
closesocket(clientSocket);
WSACleanup();
return;
}
buffer[len] = '\0';
if (strcmp(buffer, RESPONSE_MSG) != 0) {
printf("Received unexpected response: %s\n", buffer);
closesocket(clientSocket);
WSACleanup();
return;
}
// Do something after receiving the expected response
printf("Received expected response: %s\n", buffer);
// Send a message to the server
const char* msg = "Hello from client!";
if (send(clientSocket, msg, strlen(msg), 0) == SOCKET_ERROR) {
printf("Failed to send data.\n");
}
// Close the socket and clean up
closesocket(clientSocket);
WSACleanup();
}
int main() {
// Connect to server and wait for response
client_communication();
// Do something after the communication
printf("Program finished.\n");
// Send a message to the server to indicate the end of communication
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return -1;
}
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Failed to create socket.\n");
WSACleanup();
return -1;
}
struct sockaddr_in serverAddress;
memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(SERVER_IP);
serverAddress.sin_port = htons(SERVER_PORT);
if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
printf("Failed to connect to server.\n");
closesocket(clientSocket);
WSACleanup();
return -1;
}
const char* msg = "End of communication";
if (send(clientSocket, msg, strlen(msg), 0) == SOCKET_ERROR) {
printf("Failed to send data.\n");
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
在这个程序中,`client_communication()`函数实现了与服务器进行通讯的功能。程序启动后,它会创建一个TCP客户端socket,并尝试连接到指定的服务器地址和端口号。一旦连接成功,它会尝试接收数据,如果接收到的数据不是预期的字符串,则会退出函数并关闭socket。如果接收到的数据是预期的字符串,则会打印出来,并执行主函数中的后续代码。在主函数的最后,它会再次创建一个TCP客户端socket,并向服务器发送一个特定的字符串,以表示通讯结束。最终,它会关闭所有socket并清理资源。
阅读全文