C/C++winsock连接实现不同设备实时通讯

话不多说,我们先来了解一下套接字连接的原理吧:

第一步,客户端和服务端异步启动;

第二步,两端分别创建套接字,填充信息并绑定;

第三步,将服务端设为监听状态,等待连接;

               客户端发起连接请求,建立连接。

最后,双端进行数据交流,关闭服务器。

具体看代码:

服务端:

#include 
#include 

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

int main() {
    WSADATA wsaData;
    SOCKET serverSocket, clientSocket;
    struct sockaddr_in serverAddr, clientAddr;
    int clientAddrLen = sizeof(clientAddr);
    char recvBuf[1024];
    char sendBuf[1024];

    // 初始化WSA
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("WSAStartup failed!\n");
        return 1;
    }

    // 创建Socket
    serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (serverSocket == INVALID_SOCKET) {
        printf("create socket failed!\n");
        return 1;
    }

    // 绑定地址
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serverAddr.sin_port = htons(12345); // 可以修改为其他端口号
    if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
        printf("bind failed!\n");
        closesocket(serverSocket);
        return 1;
    }

    // 监听Socket
    if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
        printf("listen failed!\n");
        closesocket(serverSocket);
        return 1;
    }

    printf("server is running...\n");

    // 等待客户端连接
    clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
    if (clientSocket == INVALID_SOCKET) {
        printf("accept failed!\n");
        closesocket(serverSocket);
        return 1;
    }

    // 接收消息
    memset(recvBuf, 0, sizeof(recvBuf));
    recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
    printf("receive message: %s\n", recvBuf);

    // 发送消息
    strcpy(sendBuf, "Hello, Client!");
    send(clientSocket, sendBuf, strlen(sendBuf), 0);
    printf("send message: %s\n", sendBuf);

    // 关闭Socket
    closesocket(clientSocket);
    closesocket(serverSocket);

    // 清理WSA
    WSACleanup();

    return 0;
}

客户端:

#include 
#include 

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

int main() {
    WSADATA wsaData;
    SOCKET clientSocket;
    struct sockaddr_in serverAddr;
    char sendBuf[1024];     //发送信息存储地址
    char recvBuf[1024];   //接收信息存储地址

    // 初始化WSA
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {  //错误处理
        printf("WSAStartup failed!\n");
        return 1;
    }

    // 创建Socket
    clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);   //tcp协议,采用阻塞传输
    if (clientSocket == INVALID_SOCKET) {
        printf("create socket failed!\n");
        return 1;
    }

    // 配置Server地址
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); // 修改为实际的服务端IP(可以查询本机ip)
    serverAddr.sin_port = htons(12345); // 修改为实际的服务端端口

    // 连接服务器
    if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
        printf("connect server failed!\n");
        closesocket(clientSocket);
        return 1;
    }

    // 发送消息
    strcpy(sendBuf, "Hello, Server!");
    send(clientSocket, sendBuf, strlen(sendBuf), 0);
    printf("send message: %s\n", sendBuf);

    // 接收消息
    memset(recvBuf, 0, sizeof(recvBuf));
    recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
    printf("receive message: %s\n", recvBuf);

    // 关闭Socket
    closesocket(clientSocket);

    // 清理WSA
    WSACleanup();

    return 0;
}

你可能感兴趣的:(C++,C,服务器,前端,c++,开发语言)