在线词典项目框架搭建(linux c语言)

#include
#include
#include
#include
#include
#include
#include
#include 

#define N 32


#define R 1  //user - register
#define L 2  //user - login
#define Q 3  //user - query
#define H 4  //user - history

//定义通信双方的结构体信息
typedef struct{
	int type;
	char name[N];
	char data[256];
}MSG;


void do_register(int sockfd,MSG *msg)
{
	printf("register ...\n");
}

int do_login(int sockfd,MSG *msg)
{
	printf("login ...\n");

	return 1;
}

int do_query(int sockfd,MSG *msg)
{
	printf("login ...\n");

	return 1;
}

int do_history(int sockfd,MSG *msg)
{
	printf("login ...\n");

	return 1;
}



//  ./server ip 10000
int main(int argc, const char *argv[])
{
	int sockfd;
	int n;
	MSG msg;
	struct sockaddr_in serveraddr;
	
	if(argc != 3)
	{
		printf("Usage:%s serverip port.\n",argv[0]);
		return -1;
	}

	if((sockfd = socket(AF_INET,SOCK_STREAM,0)) <0)
	{
		perror("fail to socket.\n");
		return -1;
	}

	bzero(&serveraddr,sizeof(serveraddr));
	serveraddr.sin_family = AF_INET;
	serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
	serveraddr.sin_port = htons(atoi(argv[2]));

	if(connect(sockfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))<0)
	{
		perror("fail to connect");
		return -1;
	}

	while(1)
	{
		printf("*************************************************************\n");
		printf("*1、register         2、login           3、quit\n");
		printf("*************************************************************\n");
		printf("Please choose:");
		scanf("%d",&n);
		getchar();

		switch(n)
		{
		case 1:
			do_register(sockfd,&msg);
			break;
		case 2:
			if(do_login(sockfd,&msg) == 1)
			{
				goto next;
			}
			break;
		case 3:
			close(sockfd);
			exit(0);
			break;
		default:
			printf("Invalid data cmd.\n");
		}
	}


next:
	while(1)
	{
		printf("******************************************************\n");
		printf("*1、query_word      2、history_record      3、quit   *\n");
		printf("******************************************************\n");
		printf("Please choose:");
		scanf("%d",&n);
		getchar();

		switch(n)
		{
		case 1:
			do_query(sockfd,&msg);
			break;
		case 2:
			do_history(sockfd,&msg);
			break;
		case 3:
			close(sockfd);
			exit(0);
			break;
		default:
			printf("Invalid data cmd .\n");
		}
	}


	return 0;
}

你可能感兴趣的:(c语言,linux,开发语言)