10.5作业

结合数据库的方式完成用户注册,要求客户端输入账户密码,发送给服务器
服务器接收到账户密码后,写入到数据库中不允许重复注册

服务器

#include 

#define IP "172.19.158.35"
#define PORT 8888
int do_login(int newfd, sqlite3* ppdb);
int main(int argc, const char* argv[])
{

    // 打开数据库
    sqlite3* ppdb = NULL;
    if (SQLITE_OK != sqlite3_open("my_sql.db", &ppdb)) {
        fprintf(stderr, "__%d__:%s\n", __LINE__, sqlite3_errmsg(ppdb));
        return -1;
    }
    // 创建表
    char sql[128] = "create table if not exists user(name char,pswd char);";
    char* errmsg = NULL;
    if (SQLITE_OK != sqlite3_exec(ppdb, sql, NULL, NULL, &errmsg)) {
        fprintf(stderr, "__%d__:%s\n", __LINE__, errmsg);
        return -1;
    }

    int sfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sfd < 0)
        PRINT_ERR("socket");

    // 允许端口快速复用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
        PRINT_ERR("setsockopt");

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);
    if (bind(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
        PRINT_ERR("bind");

    if (listen(sfd, 128) < 0)
        PRINT_ERR("listen");

    struct sockaddr_in cin;
    socklen_t cinlen = sizeof(cin);
    char buf[128] = "";

    int newfd = accept(sfd, (struct sockaddr*)&cin, &cinlen);
    if (newfd < 0)
        PRINT_ERR("accept");
    printf("[%s:%d]fd=%d>>>连接成功!\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);

    do_login(newfd, ppdb); // 就当是多线程吧

    sqlite3_close(ppdb);
    close(sfd);
    return 0;
}

int do_login(int newfd, sqlite3* ppdb)
{
    char buf[256] = "";
    size_t res = -1;
    char name[64] = "";
    char pswd[64] = "";
    while (1) {
        bzero(buf,sizeof(buf));
        bzero(name,sizeof(name));
        bzero(pswd,sizeof(pswd));

        res = recv(newfd, buf, sizeof(buf), 0);
        if (res < 0)
            PRINT_ERR("recv");
        else if (0 == res) {
            puts("客户端退出!");
            break;
        }
        sscanf(buf, "%s%s", name, pswd);

        // 判断name是否存在
        char sql[128] = "";
        sprintf(sql, "select * from user where name='%s'", name);
        char** pres = NULL;
        char* errmsg = NULL;
        if (SQLITE_OK != sqlite3_get_table(ppdb, sql, &pres, NULL, NULL, &errmsg)) {
            fprintf(stderr, "__%d__:%s\n", __LINE__, errmsg);
            return -1;
        }

        bzero(buf, sizeof(buf));
        if (0 == strcmp(pres[2], name))
            sprintf(buf, "账号已存在!");
        else {
            // 注册
            bzero(sql, sizeof(sql));
            sprintf(sql, "insert into user values('%s','%s')", name, pswd);
            if (SQLITE_OK != sqlite3_exec(ppdb, sql, NULL, NULL, &errmsg)) {
                fprintf(stderr, "__%d__:%s\n", __LINE__, errmsg);
                return -1;
            }
            sprintf(buf, "注册成功!");
        }

        if (send(newfd, buf, sizeof(buf), 0) < 0)
            PRINT_ERR("send");
        sqlite3_free_table(pres);
    }

    close(newfd);
    return 0;
}

客户端

#include 
#define IP "172.19.158.35"
#define PORT 8888
int do_login(int cfd);

int main(int argc, const char* argv[])
{
    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if (cfd < 0)
        PRINT_ERR("socket");

    struct sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(PORT);
    sin.sin_addr.s_addr = inet_addr(IP);

    if (connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
        PRINT_ERR("connect");

    while (1) {
        puts("-------------------------------------------------------");
        puts("------------------------1.登录-------------------------");
        puts("------------------------2.注册-------------------------");
        puts("------------------------3.退出-------------------------");
        puts("-------------------------------------------------------");
        char c = 0;
        printf("请选择>>>");
        c = getchar();
        while (getchar() != 10)
            ;
        switch (c) {
        case '1':
            break;
        case '2':
            do_login(cfd);
            break;
        case '3':
            goto END;
        }
    }
END:

    close(cfd);
    return 0;
}

int do_login(int cfd)
{
    char buf[256] = "";
    char name[64] = "";
    char pswd[64] = "";
    int res = 0;
    printf("格式: name  password\n");
    res = scanf("%s%s", name, pswd);
    while (getchar() != 10)
        ;
    if (2 != res) {
        puts("格式错误!");
        return -1;
    }
    sprintf(buf, "%s %s", name, pswd);
    if (send(cfd, buf, sizeof(buf), 0) < 0)
        PRINT_ERR("send");

    bzero(buf, sizeof(buf));
    if (recv(cfd, buf, sizeof(buf), 0) < 0)
        PRINT_ERR("recv");
    puts(buf);
    return 0;
}

你可能感兴趣的:(网络,服务器,数据库)