windows socket 数据交互

client

//ip地址 端口
SOCKET init(const char *IP, u_short port) {
    WSADATA wd;
    int ret = WSAStartup(MAKEWORD(2, 2), &wd);
    if (ret != 0) {
        cout << "WSAStartup error ..." << endl;
        return INVALID_SOCKET;
    }
    cout << "WSAStartup success ..." << endl;

    //创建套接字
    SOCKET skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (skt == SOCKET_ERROR) {
        cout << "socket error ..." << endl;
        return INVALID_SOCKET;
    }
    cout << "socket success ..." << endl;

    //服务端信息
    sockaddr_in addrServ;
    addrServ.sin_family = AF_INET;//IPV4协议
    addrServ.sin_port = htons(port);//端口
    addrServ.sin_addr.s_addr = inet_addr(IP);//IP地址

    //连接服务器
    if (connect(skt, (sockaddr *)&addrServ, sizeof(addrServ)) == SOCKET_ERROR) {
        cout << "connect error ..." << endl;
        return -1;
    }
    cout << "connect success ..." << endl;

    return skt;
}

void destroy(SOCKET skt) {
    closesocket(skt);
    WSACleanup();
}

void printMenu() {
    printf("*************************\n");
    printf("*getName                *\n");
    printf("*getAge                 *\n");
    printf("*exit                   *\n");
    printf("*************************\n");
}

//socket客户端代码 客户端发送命令 接受服务器发送的消息
int function06() {
    SOCKET sktCli = init("192.168.3.18", 8080);
    if (sktCli == INVALID_SOCKET) {
        return -1;
    }

    while (true) {
        printMenu();
        char szCmd[64] = {0};
        scanf("%s", szCmd);
        if (strcmp(szCmd, "exit") == 0) {
            printf("[client] exit ...\n");
            break;
        }
        send(sktCli, szCmd, strlen(szCmd)+1, 0);

        char szRecv[1024] = {0};
        int ret = recv(sktCli, szRecv, sizeof(szRecv), 0);
        if (ret <= 0) {
            printf("[client] server exit ...\n");
            break;
        }
        printf("[server] msg:%s\n", szRecv);
    }

    destroy(sktCli);
    return 0;
}

server

//初始化
SOCKET init(const char *IP, u_short port) {
    WSADATA wd;
    int ret = WSAStartup(MAKEWORD(2, 2), &wd);
    if (ret != 0) {
        cout << "WSAStartup error ..." << endl;
        return -1;
    }
    cout << "WSAStartup success ..." << endl;

    //创建套接字
    SOCKET skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (skt == INVALID_SOCKET) {
        cout << "socket error ..." << endl;
        return -1;
    }
    cout << "socket success ..." << endl;
    //服务器信息
    sockaddr_in addrServ;
    addrServ.sin_family = AF_INET;
    addrServ.sin_port = htons(port);
    addrServ.sin_addr.s_addr = inet_addr(IP);

    //套接字绑定端口和ip
    if (bind(skt, (sockaddr *)&addrServ, sizeof(addrServ)) == SOCKET_ERROR) {
        cout << "bind error ..." << endl;
        return -1;
    }
    cout << "bind success ..." << endl;

    //开始监听套接字
    if (listen(skt, SOMAXCONN) == SOCKET_ERROR) {
        cout << "listen error ..." << endl;
        return -1;
    }
    cout << "listen success ..." << endl;

    return skt;
}

void destroy(SOCKET skt) {
    closesocket(skt);
    WSACleanup();
}

//接受客户端发送的命令 发送消息给客户端
int function06() {
    SOCKET sktServ = init("0.0.0.0", 8080);
    if (sktServ == INVALID_SOCKET) {
        return -1;
    }

    SOCKET sktCli = accept(sktServ, NULL, NULL);
    while (true) {
        char szCmd[64] = {0};
        int ret = recv(sktCli, szCmd, sizeof(szCmd), 0);
        if (ret == SOCKET_ERROR) {
            printf("[server] recv error ...\n");
            break;
        }
        if (ret == 0) {
            printf("[client] exit ...\n");
            break;
        }
        printf("[client] %s\n", szCmd);
        char szMsg[1024];
        if (strcmp(szCmd, "getName") == 0) {
            sprintf(szMsg, "%s", "爱白菜的小昆虫.");
        }
        else if (strcmp(szCmd, "getAge") == 0) {
            sprintf(szMsg, "%s", "1000.");
        }
        else {
            sprintf(szMsg, "%s", "???.");
        }
        send(sktCli, szMsg, strlen(szMsg)+1, 0);
    }

    destroy(sktServ);
    return 0;
}

 

windows socket 数据交互

上一篇:C# consume RestApi


下一篇:Matlab函数编译成dll供c调用