请用C++写一段代码,实现一个远端程序和本机程序的通信
时间: 2024-02-18 08:00:59 浏览: 68
好的,以下是一个简单的示例代码,演示如何使用套接字(Socket)在C++中实现远端程序和本机程序的通信:
远端程序(服务器端)代码:
```c++
#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); // 初始化Winsock库
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建套接字
SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ListenSocket == INVALID_SOCKET) {
cout << "Error at socket(): " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 绑定套接字到本地IP和端口
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(1234);
iResult = bind(ListenSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
cout << "bind failed with error: " << WSAGetLastError() << endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// 监听套接字
iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
cout << "listen failed with error: " << WSAGetLastError() << endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// 接受连接请求
SOCKET ClientSocket;
sockaddr_in clientAddr;
int clientAddrLen = sizeof(clientAddr);
ClientSocket = accept(ListenSocket, (SOCKADDR*)&clientAddr, &clientAddrLen);
if (ClientSocket == INVALID_SOCKET) {
cout << "accept failed: " << WSAGetLastError() << endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}
// 发送数据到客户端
char sendData[1024] = "Hello, client!";
iResult = send(ClientSocket, sendData, strlen(sendData), 0);
if (iResult == SOCKET_ERROR) {
cout << "send failed: " << WSAGetLastError() << endl;
closesocket(ClientSocket);
WSACleanup();
return 1;
}
// 关闭套接字
closesocket(ClientSocket);
closesocket(ListenSocket);
WSACleanup();
return 0;
}
```
本机程序(客户端)代码:
```c++
#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData); // 初始化Winsock库
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建套接字
SOCKET ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
cout << "Error at socket(): " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 连接远程服务器
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(1234);
serverAddr.sin_addr.s_addr = inet_addr("192.168.1.100"); // 服务器IP地址
iResult = connect(ConnectSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
cout << "Unable to connect to server: " << WSAGetLastError() << endl;
closesocket(ConnectSocket);
WSACleanup();
return 1;
}
// 接收服务器发送的数据
char recvData[1024] = {0};
iResult = recv(ConnectSocket, recvData, 1024, 0);
if (iResult > 0) {
cout << "Received data: " << recvData << endl;
} else if (iResult == 0) {
cout << "Connection closed" << endl;
} else {
cout << "recv failed: " << WSAGetLastError() << endl;
}
// 关闭套接字
closesocket(ConnectSocket);
WSACleanup();
return 0;
}
```
请确保在编译和运行代码之前,已经正确安装了Winsock库。在Windows操作系统上,可以使用Visual Studio集成开发环境(IDE)来编译和运行代码。如果您使用其他的IDE或编译器,请参考相关的文档来实现项目的编译和运行。
阅读全文