国庆作业day6

服务器

#include 
#define IP "192.168.101.66"
#define PORT 6666
int main(int argc, const char *argv[])
{
	//创建套接字
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	if(fd < 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);

	int reuse = 1;
    if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    {   
        ERR_MSG("setsockopt");
        return -1; 
    }
	//绑定IP和端口
	if(bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
		ERR_MSG("bind");
		return -1;
	}

	//将套接变成被动监听状态
	if(listen(fd, 64) < 0){
		ERR_MSG("listen");
		return -1;
	}

	//接收连接后的套接字
	struct sockaddr_in cin;
	socklen_t len = sizeof(cin);
	int newfd = accept(fd, (struct sockaddr *)&sin, &len);
	if(newfd < 0){
		ERR_MSG("accept");
		return -1;
	}

	sqlite3 *pdb;
	//打开函数库
	if(sqlite3_open("./sq.db", &pdb) != SQLITE_OK){
		fprintf(stderr, "sqlite3_open __%d__\n", __LINE__);
		return -1;
	}
	printf("sqlite3_open success\n");

	char sql[128] = "CREATE TABLE IF NOT EXISTS passwd (name char primary key, passwd char);";
	char *errmsg = NULL;
	//创建表格
	if(sqlite3_exec(pdb, sql, NULL, NULL, &errmsg) != SQLITE_OK){
		fprintf(stderr, "sqlite3_exec __%d__\n", __LINE__);
		return -1;
	}

	char buf[128] = "";
	ssize_t res = 0;
	char name[64] = "";
	char pwd[64] = "";
	char *p = NULL;
	int i = 0;
	while(1){
		//接收
		res = recv(newfd, buf, sizeof(buf), 0);
		if(res < 0){
			ERR_MSG("recv");
			return -1;
		}else if(0 == res){
			printf("读取完毕\n");
			break;
		}
		printf("%s __%d__\n",buf, __LINE__);

		i = 0;
		while(buf[i] != ' '){
			i++;
		}

		bzero(name, sizeof(name));
		bzero(pwd, sizeof(pwd));
		strcpy(name, buf);
		name[i] = 0;

		p = &buf[i+1];
		strcpy(pwd, p);

		//将账号密码写入数据库
		bzero(sql, sizeof(sql));
		sprintf(sql, "insert into passwd values(\"%s\", %s);", name, pwd);
		printf("%s\n", sql);
		char *emsg;
		if(sqlite3_exec(pdb, sql, NULL, NULL, &emsg) != SQLITE_OK){
			fprintf(stderr, "sqlite3_exec __%d__\n", __LINE__);
			return -1;
		}
		printf("注册成功\n");
	}
	//关闭函数库
	if(sqlite3_close(pdb) != SQLITE_OK){
		fprintf(stderr, "sqlite3_close __%d__\n", __LINE__);
		return -1;
	}

	//关闭套接字
	close(fd);
	close(newfd);
	return 0;
}

客户端

#include 
#define IP "192.168.101.66"
#define PORT 6666
int main(int argc, const char *argv[])
{
	//创建套接字
	int fd = socket(AF_INET, SOCK_STREAM, 0);
	if(fd < 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(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0){
		ERR_MSG("connect");
		return -1;
	}
	char s[128] = "";
	while(1){
		//发送
		printf("请输入:账户 密码 >>>");
		bzero(s, sizeof(s));
		fgets(s, sizeof(s), stdin);
		s[strlen(s)-1] = 0;
		if(0 == strcmp(s, "quit")){
			printf("客户端退出\n");
			break;
		}
		if(send(fd, s, sizeof(s), 0) < 0){
			ERR_MSG("send");
			return -1;
		}
	}
	//关闭套接字
	close(fd);
	return 0;
}

你可能感兴趣的:(jvm,java,开发语言)