作业10.5

服务器
 

#include
#define IP "192.168.0.100"
#define PORT 8888
int fun(int newfd, sqlite3* sqldb);
int main(int argc, const char* argv[])
{
 
    // 打开数据库
    sqlite3* sqldb = NULL;
    if (SQLITE_OK != sqlite3_open("my_sql.db", &sqldb)) {
        fprintf(stderr, "__%d__:%s\n", __LINE__, sqlite3_errmsg(sqldb));
        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)a
	{
        ERR_MSG("socket");
		return -1;
	}
    // 允许端口快速复用
    int reuse = 1;
    if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
	{
        ERR_MSG("setsockopt");
		return -1;
	}
 
    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)
	{
        ERR_MSG("bind");
		return -1;
	}
    if (listen(sfd, 128) < 0)
	{
        ERR_MSG("listen");
		return -1;
	}
    struct sockaddr_in cin;
    socklen_t cinlen = sizeof(cin);
    char buf[128] = "";
 
    int newfd = accept(sfd, (struct sockaddr*)&cin, &cinlen);
    if (newfd < 0)
	{
        ERR_MSG("accept");
		return 0;
	}
    printf("[%s:%d]fd=%d  连接成功!\n", inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
 
    fun(newfd, sqldb);  
    sqlite3_close(sqldb);
    close(sfd);
    return 0;
}
 
int fun (int newfd, sqlite3* sqldb)
{
    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)
            ERR_MSG("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(sqldb, 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(sqldb, sql, NULL, NULL, &errmsg)) {
                fprintf(stderr, "__%d__:%s\n", __LINE__, errmsg);
                return -1;
            }
            sprintf(buf, "注册成功!");
        }
 
        if (send(newfd, buf, sizeof(buf), 0) < 0)
            ERR_MSG("send");
        sqlite3_free_table(pres);
    }
 
    close(newfd);
    return 0;
}

客户端
 

#include 
#define IP "192.168.0.100"
#define PORT 8888
int fun(int cfd);
 
int main(int argc, const char* argv[])
{
    int cfd = socket(AF_INET, SOCK_STREAM, 0);
    if (cfd < 0)
	{
        ERR_MSG("socket");
		return -1;
 	}
    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)
	{
        ERR_MSG("connect");
		return -1;
 	}
    while (1) {
        puts("-------------------------------------------");
        puts("----------------1.登录---------------------");
        puts("----------------2.注册---------------------");
        puts("----------------3.退出---------------------");
        puts("-------------------------------------------");
        char a = 0;
        printf("请选择>>>");
        a = getchar();
        while (getchar() != 10);
        switch (a) 
		{
        case '1':
            break;
        case '2':
            fun(cfd);
            break;
        case '3':
            goto END;
        }
    }
END:
 
    close(cfd);
    return 0;
}
 
int fun(int cfd)
{
    char buf[256] = "";
    char name[64] = "";
    char pswd[64] = "";
    int res = 0;
    printf("name and  password >>> ");
    scanf("%s%s", name, pswd);
    while (getchar() != 10);
    sprintf(buf, "%s %s", name, pswd);
    if (send(cfd, buf, sizeof(buf), 0) < 0)
	{
        ERR_MSG("send");
		return -1;
 	}
    bzero(buf, sizeof(buf));
    if (recv(cfd, buf, sizeof(buf), 0) < 0)
	{
        ERR_MSG("recv");
		return -1;
	}
    puts(buf);
    return 0;
}

你可能感兴趣的:(网络)