码迷,mamicode.com
首页 > Windows程序 > 详细

windows socket 完善数据简单结构化

时间:2019-08-31 01:23:48      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:windows   break   我不知道   color   客户端   nbsp   lse   else   soc   

client

//交互命令
enum CMD {
    CMD_LOGIN,
    CMD_LOGIN_RESULT,
    CMD_LOGOUT,
    CMD_LOGOUT_RESULT,
    CMD_ERROR
};
//消息头
struct DataHeader {
    int cmd;//消息指令
    int dataLength;//消息长度
};
//登录消息
struct Login : public DataHeader {
    Login() {
        cmd = CMD_LOGIN;
        dataLength = sizeof(Login);
    }
    char szName[64];//用户名
    char szPass[64];//密码
};
//登录返回消息
struct LoginResult : public DataHeader {
    LoginResult() {
        cmd = CMD_LOGIN_RESULT;
        dataLength = sizeof(LoginResult);
        iResult = 1;
    }
    int iResult;
};
//登出消息
struct Logout : public DataHeader {
    Logout() {
        cmd = CMD_LOGOUT;
        dataLength = sizeof(Logout);
    }
    char szName[64];
};
//登出返回消息
struct LogoutResult : public DataHeader {
    LogoutResult() {
        cmd = CMD_LOGOUT_RESULT;
        dataLength = sizeof(LogoutResult);
        iResult = 2;
    }
    int iResult;
};
//客户端完善结构化消息
int function08() {
    SOCKET sktCli = init("192.168.3.18", 8080);
    if (sktCli == INVALID_SOCKET) {
        return -1;
    }

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

        DataHeader dh = {0};
        if (strcmp(szCmd, "login") == 0) {
            Login login;
            strcpy(login.szName, "爱白菜的小昆虫登录");
            strcpy(login.szPass, "我不知道密码是啥");
            send(sktCli, (const char *)&login, sizeof(login), 0);
            LoginResult result;
            recv(sktCli, (char *)&result, sizeof(result), 0);
            printf("[server] cmd=%d dataLength=%d iResult=%d\n", result.cmd, result.dataLength, result.iResult);
        }
        else if(strcmp(szCmd, "logout") == 0){
            Logout logout;
            strcpy(logout.szName, "爱白菜的小昆虫退出");
            send(sktCli, (const char *)&logout, sizeof(logout), 0);
            LogoutResult result;
            recv(sktCli, (char *)&result, sizeof(result), 0);
            printf("[server] cmd=%d dataLength=%d iResult=%d\n", result.cmd, result.dataLength, result.iResult);
        }
        else {
        }
    }

    destroy(sktCli);
    return 0;
}

server

//交互命令
enum CMD{
    CMD_LOGIN,
    CMD_LOGIN_RESULT,
    CMD_LOGOUT,
    CMD_LOGOUT_RESULT,
    CMD_ERROR
};
//消息头
struct DataHeader {
    int cmd;//消息指令
    int dataLength;//消息长度
};
//登录消息
struct Login : public DataHeader{
    Login() {
        cmd = CMD_LOGIN;
        dataLength = sizeof(Login);
    }
    char szName[64];//用户名
    char szPass[64];//密码
};
//登录返回消息
struct LoginResult : public DataHeader {
    LoginResult() {
        cmd = CMD_LOGIN_RESULT;
        dataLength = sizeof(LoginResult);
        iResult = 1;
    }
    int iResult;
};
//登出消息
struct Logout : public DataHeader {
    Logout() {
        cmd = CMD_LOGOUT;
        dataLength = sizeof(Logout);
    }
    char szName[64];
};
//登出返回消息
struct LogoutResult : public DataHeader {
    LogoutResult() {
        cmd = CMD_LOGOUT_RESULT;
        dataLength = sizeof(LogoutResult);
        iResult = 2;
    }
    int iResult;
};
//服务端完善结构化消息
int function08() {
    SOCKET sktServ = init("0.0.0.0", 8080);
    if (sktServ == INVALID_SOCKET) {
        return -1;
    }

    SOCKET sktCli = accept(sktServ, NULL, NULL);
    while (true) {
        DataHeader header;
        int ret = recv(sktCli, (char *)&header, sizeof(header), 0);
        if (ret == SOCKET_ERROR) {
            printf("[server] recv error ...\n");
            break;
        }
        if (ret == 0) {
            printf("[client] exit ...\n");
            break;
        }
        printf("[client] cmd=%d datalength=%d\n", header.cmd, header.dataLength);
        
        switch (header.cmd) {
        case CMD_LOGIN: {
            Login login;
            //客户端发送login结构体,但是服务端前面接受了DataHeader,所以这里只需要接受剩下的消息
            recv(sktCli, (char *)&login + sizeof(DataHeader), sizeof(login) - sizeof(DataHeader), 0);
            printf("[client] name=%s pass=%s\n", login.szName, login.szPass);

            LoginResult result;
            send(sktCli, (const char *)&result, sizeof(result), 0);
            break;
        }
        case CMD_LOGOUT: {
            Logout logout;
            //客户端发送login结构体,但是服务端前面接受了DataHeader,所以这里只需要接受剩下的消息
            recv(sktCli, (char *)&logout + sizeof(DataHeader), sizeof(logout) - sizeof(DataHeader), 0);
            printf("[client] name=%s\n", logout.szName);

            LogoutResult result;
            send(sktCli, (const char *)&result, sizeof(result), 0);
            break;
        }
        default:
            break;
        }
    }

    destroy(sktServ);
    return 0;
}

 

windows socket 完善数据简单结构化

标签:windows   break   我不知道   color   客户端   nbsp   lse   else   soc   

原文地址:https://www.cnblogs.com/xuqiulin/p/11437696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!