网络socket编程的标准模板(在线词典注册和登录)

1.服务器
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define N 32
#define R 1		//user-register
#define L 2		//user-login
#define DATABASE "my.db"

typedef struct message
{
	int type;
	char name[N];
	char data[128];
}MSG;

void exitfun();
void do_client(sqlite3 *db, int acceptfd);
void do_login(sqlite3 *db, MSG *msg, int acceptfd);
void do_register(sqlite3 *db, MSG *msg, int acceptfd);

int main(int argc, char *argv[])
{
	struct sockaddr_in sin = {0};
	struct sockaddr_in cin = {0};
	int sockfd;
	int ret;
	socklen_t len = sizeof(sin);
	int choice;
	pid_t pid;
	MSG msg;
	char buf[128] = {0};
	int acceptfd;
	sqlite3 *db;

	if(argc != 3)
	{
		perror("input wrong, please input server ip and port!");
		exit(-1);
	}

	if(sqlite3_open(DATABASE, &db) != SQLITE_OK)
	{
		perror("sqlite3_open");
		exit(-1);
	}

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("socket");
		exit(-1);
	}

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = inet_addr(argv[1]);
	sin.sin_port = htons(atoi(argv[2]));

	if((ret = bind(sockfd, (struct sockaddr *)&sin, sizeof(sin))) < 0)
	{
		perror("bind");
		exit(-1);
	}

	if((ret = listen(sockfd, 5)) < 0)
	{
		perror("listen");
		exit(-1);
	}

	signal(SIGCHLD, exitfun);
	while(1)
	{
		if((acceptfd = accept(sockfd, (struct sockaddr *)&cin, &len)) < 0)
		{
			perror("accept");
			exit(-1);
		}
		if((inet_ntop(AF_INET, (void *)&cin.sin_addr, buf, sizeof(cin))) == NULL)
		{
			perror("inet_ntop");
			exit(-1);
		}
		printf("Client (%s\t%d) is connected!\n", buf, ntohs(cin.sin_port));

		if((pid =fork()) < 0)
		{
			perror("fork");
			exit(-1);
		}

		if(pid == 0)
		{
			close(sockfd);
			do_client(db, acceptfd);
		}

		if(pid > 0)
		{
			close(acceptfd);
		}
	}
	return 0;
}

void exitfun(int sig)
{
	printf("client exit\n");
	wait(NULL);
}

void do_client(sqlite3 *db, int acceptfd)
{
	MSG msg;

	while(recv(acceptfd, &msg, sizeof(msg), 0) > 0)
	{
		switch(msg.type)
		{
			case R:
				do_register(db, &msg, acceptfd);
				break;
			case L:
				do_login(db, &msg, acceptfd);
				break;
			default:
				printf("Invaild input\n");
				break;
		}
	}

	close(acceptfd);
	exit(0);
}

void do_login(sqlite3 *db, MSG *msg, int acceptfd)
{
	char sql[128] = {0};
	char buf[32] = {0};
	int nrow;
	int ncolumn;
	char *errmsg;
	char ** result;

	sprintf(sql, "select * from user where name = '%s' and passwd = '%s'", msg->name, msg->data);
	if(sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
	{
		printf("%s\n", errmsg);
		exit(-1);
	}

	if(nrow == 1)
	{
		strcpy(buf,"login success");
		if(send(acceptfd, buf, sizeof(buf), 0) < 0)
		{
			perror("login send");
			exit(-1);
		}
	}
	else if(nrow == 0)
	{
		strcpy(buf,"name or passwd wrong");
		if(send(acceptfd, buf, sizeof(buf), 0) < 0)
		{
			perror("login send");
			exit(-1);
		}
	}
}

void do_register(sqlite3 *db, MSG *msg, int acceptfd)
{
	char *errmsg;
	char sql[128] = {0};
	char buf[32] = {0};

	sprintf(sql, "insert into user values ('%s', %s)", msg->name, msg->data);
	if((sqlite3_exec(db, sql, NULL, NULL, &errmsg)) != SQLITE_OK)
	{
		printf("%s\n",errmsg);
		strcpy(buf, "name already exist");
	}
	else
		strcpy(buf, "register ok");
	if((send(acceptfd, buf, sizeof(buf), 0)) < 0)
	{
		perror("register send");
		exit(-1);
	}
}

2.客户端
#include 
#include 
#include 
#include 
#include 

#define N 32
#define R 1		//user-register
#define L 2		//user-login

typedef struct message
{
	int type;
	char name[N];
	char data[128];
}MSG;

void do_register(int sockfd, MSG *msg);
int do_login(int sockfd, MSG *msg);
void do_search(int sockfd, MSG *msg);
void do_history(int sockfd, MSG *msg);

int main(int argc, char *argv[])
{
	struct sockaddr_in sin = {0};
	int sockfd;
	int ret;
	int choice;
	MSG msg;

	if(argc != 3)
	{
		perror("input wrong, please input server ip and port!");
		exit(-1);
	}

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("socket");
		exit(-1);
	}

	sin.sin_family = AF_INET;
	sin.sin_addr.s_addr = inet_addr(argv[1]);
	sin.sin_port = htons(atoi(argv[2]));

	if((ret = connect(sockfd, (struct sockaddr *)&sin, sizeof(sin))) < 0)
	{
		perror("connect");
		exit(-1);
	}

	while(1)
	{
		printf("*******************************\n");
		printf("1.register    2.login    3.quit\n");
		printf("*******************************\n");

		printf("input your choice\n");
		scanf("%d",&choice);
		getchar();

		switch(choice)
		{
			case 1:
				do_register(sockfd, &msg);
				break;
			case 2:
				do_login(sockfd, &msg);
				break;
			default:
				printf("Invaild choice\n");
				break;
		}
	}
}

void do_register(int sockfd, MSG *msg)
{
	char buf[32] = {0};

	msg->type = R;

	printf("input name:\n");
	scanf("%s",msg->name);
	getchar();

	printf("input passwd:\n");
	scanf("%s",msg->data);

	if((send(sockfd, msg, sizeof(MSG), 0)) < 0)
	{
		perror("send");
		exit(-1);
	}

	if((recv(sockfd, buf, sizeof(buf), 0)) <0)
	{
		perror("recv");
		exit(-1);
	}

	printf("%s\n", buf);
}

int do_login(int sockfd, MSG *msg)
{
	char buf[32] = {0};

	msg->type = L;

	printf("input name:\n");
	scanf("%s",msg->name);
	getchar();

	printf("input passwd:\n");
	scanf("%s",msg->data);

	if((send(sockfd, msg, sizeof(MSG), 0)) < 0)
	{
		perror("send");
		exit(-1);
	}

	if((recv(sockfd, buf, sizeof(buf), 0)) <0)
	{
		perror("recv");
		exit(-1);
	}

	if((strcmp(buf, "login success")) == 0)
	{
		printf("%s\n", buf);
		return 1;
	}
	else if((strcmp(buf, "name or passwd wrong")) == 0)
	{
		printf("%s\n", buf);
	}
	return 0;

}

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