windows进程通信socket源代码
时间: 2023-07-08 16:16:40 浏览: 97
以下是一个简单的 Windows 进程间通信的示例,使用了 socket 进行通信。其中,一个进程作为服务端,另一个进程作为客户端,通过 socket 进行通信。
服务端代码:
```c++
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
// 初始化 Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建 socket
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
cout << "Error at socket(): " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 绑定 socket
sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = INADDR_ANY;
service.sin_port = htons(27015);
iResult = bind(listenSocket, (SOCKADDR*)&service, sizeof(service));
if (iResult == SOCKET_ERROR) {
cout << "bind failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 监听 socket
if (listen(listenSocket, 1) == SOCKET_ERROR) {
cout << "listen failed with error: " << WSAGetLastError() << endl;
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 等待客户端连接
cout << "Waiting for client to connect..." << endl;
SOCKET clientSocket = INVALID_SOCKET;
while (clientSocket == INVALID_SOCKET) {
clientSocket = accept(listenSocket, NULL, NULL);
}
// 客户端已连接
cout << "Client connected." << endl;
// 发送数据到客户端
const char* sendData = "Hello, client!";
iResult = send(clientSocket, sendData, strlen(sendData), 0);
if (iResult == SOCKET_ERROR) {
cout << "send failed with error: " << WSAGetLastError() << endl;
closesocket(clientSocket);
WSACleanup();
return 1;
}
// 关闭连接
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
客户端代码:
```c++
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
// 初始化 Winsock
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}
// 创建 socket
SOCKET connectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (connectSocket == INVALID_SOCKET) {
cout << "Error at socket(): " << WSAGetLastError() << endl;
WSACleanup();
return 1;
}
// 连接到服务端
sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
clientService.sin_port = htons(27015);
iResult = connect(connectSocket, (SOCKADDR*)&clientService, sizeof(clientService));
if (iResult == SOCKET_ERROR) {
cout << "Unable to connect to server: " << WSAGetLastError() << endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
// 接收服务端发送的数据
char recvBuffer[1024];
iResult = recv(connectSocket, recvBuffer, 1024, 0);
if (iResult > 0) {
recvBuffer[iResult] = '\0';
cout << "Received: " << recvBuffer << endl;
}
else if (iResult == 0) {
cout << "Connection closed." << endl;
}
else {
cout << "recv failed with error: " << WSAGetLastError() << endl;
closesocket(connectSocket);
WSACleanup();
return 1;
}
// 关闭连接
closesocket(connectSocket);
WSACleanup();
return 0;
}
```
上述代码中,服务端使用 `socket` 创建一个监听 socket,并调用 `bind` 绑定到一个指定的 IP 地址和端口上。然后,使用 `listen` 开始监听客户端连接请求。当有客户端连接时,服务端调用 `accept` 接收客户端连接,并使用 `send` 发送数据到客户端。最后,关闭连接。
客户端使用 `socket` 创建一个连接 socket,并调用 `connect` 连接到服务端。然后,使用 `recv` 接收服务端发送的数据。最后,关闭连接。
注意:以上代码仅作为示例,实际应用中还需要考虑安全性、错误处理等问题。
阅读全文