网络字典客户端

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define  N  256
#define  R  1   //  user register
#define  L  2   //  user login
#define  Q  3   //  query word
#define  H  4   //  history record

#define DATABASE "my.db"

typedef struct sockaddr SA;

typedef struct
{
 int type;
 char name[N];
 char data[N];   // password or word or remark
} MSG;

void do_register(int sockfd, MSG *pbuf)
{
 pbuf->type = R;
 printf("input name : ");
 scanf("%s", pbuf->name);
 printf("input password : ");
 scanf("%s", pbuf->data);
 send(sockfd, pbuf, sizeof(MSG), 0);

    bzero(pbuf, sizeof(MSG));
 recv(sockfd, pbuf, sizeof(MSG), 0);
 printf("register : %s\n", pbuf->data);
}

int do_login(int sockfd, MSG *pbuf)
{
 pbuf->type = L;
 printf("input name : ");
 scanf("%s", pbuf->name);
 printf("input password : ");
 scanf("%s", pbuf->data);
 send(sockfd, pbuf, sizeof(MSG), 0);

    bzero(pbuf, sizeof(MSG));
 recv(sockfd, pbuf, sizeof(MSG), 0);
 if (strncmp(pbuf->data, "OK", 3) == 0)
 {
  printf("login : OK\n");
  return 1;
 }
 printf("login : %s\n", pbuf->data);

 return 0;
}

void do_query(int sockfd, MSG *pbuf)
{
 pbuf->type = Q;
 while ( 1 )
 {
  printf("input word : ");
  scanf("%s", pbuf->data);
  if (strcmp(pbuf->data, "#") == 0) break;
  send(sockfd, pbuf, sizeof(MSG), 0);

        bzero(pbuf, sizeof(MSG));
  recv(sockfd, pbuf, sizeof(MSG), 0);
        printf("%s\n", pbuf->data);
  printf("\n");
 }

 return;
}

void do_history(int sockfd, MSG *pbuf)
{
 pbuf->type = H;
 send(sockfd, pbuf, sizeof(MSG), 0);
 while ( 1 )
 {
        bzero(pbuf, sizeof(MSG));
  recv(sockfd, pbuf, sizeof(MSG), 0);
  if (pbuf->data[0] == '\0') break;
  printf("%s\n", pbuf->data);
 }

 return;
}

int main(int argc, char *argv[])
{
 int sockfd;
 struct sockaddr_in servaddr;
 MSG buf;
 char clean[N];

 if (argc < 3)
 {
  printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
  exit(-1);
 }

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

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

 if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
 {
  perror("fail to connect");
  exit(-1);
 }

 int n;
 while ( 1 )
 {
  printf("************************************\n");
  printf("* 1: register   2: login   3: quit *\n");
  printf("************************************\n");
  printf("please choose : ");
  if (scanf("%d", &n) == 0)
  {
   fgets(clean, N, stdin);
   printf("\n");
   continue;
  }
  switch ( n )
  {
  case 1 :
   printf("\n");
   do_register(sockfd, &buf);
   printf("\n");
   break;
  case 2 :
   printf("\n");
   if (do_login(sockfd, &buf) == 1)
   {
    printf("\n");
    goto next;
   }
   printf("\n");
   break;
  case 3 :
   close(sockfd);
   exit(0);
  }
 }

next:
    while (1)
    {
  printf("***********************************************\n");
  printf("* 1: query_word   2: history_record   3: quit *\n");
  printf("***********************************************\n");
  printf("please choose : ");

  if (scanf("%d", &n) == 0)
  {
   fgets(clean, N, stdin);
   printf("\n");
   continue;
  }

  switch ( n )
  {
  case 1 :
   printf("\n");
   do_query(sockfd, &buf);
   printf("\n");
   break;
  case 2 :
   printf("\n");
   do_history(sockfd, &buf);
   printf("\n");
   break;
  case 3 :
   close(sockfd);
   exit(0);
  }
    }

    exit(0);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值