| 
                         客户端运行起来。先输入zhuce或者denglu 回车结束。  输入账号密码的时候回车结束。  实现的功能:  注册、登录账号;  注册之后可以直接查找。  登录时账号密码不对会直接退出。并关闭服务器端。  账号和密码保存在数据库中。  输入存在的单词,会返回该单词的意思。  输入不存在的单词,会报错。      
    
将单词插入数据库中:  
/*************************************************************************  > File Name: c.c  > Author:  > Mail:  > Created Time: Mon 15 Jun 2015 03:27:27 AM PDT  ************************************************************************/      #include<stdio.h>  #include<stdlib.h>  #include<string.h>  #include<unistd.h>  #include<sys/types.h>  #include<sqlite3.h>  #include<fcntl.h>  #include<sys/stat.h>      int main(int argc,const char * argv[])  {    FILE *fp1;    int i =0;    int num = 0;    char *p,*s,*y;    char *errmsg;    char sql[256];    char buff2[1024];    sqlite3 *db =NULL;    if(sqlite3_open("db.db",&db)!=SQLITE_OK)    {      sqlite3_errmsg(db);      exit(EXIT_FAILURE);    }    sqlite3_exec(db,"create table dir (ID INTEGER,WORD TEXT,MEANING TEXT)",NULL,&errmsg);    fp1 = fopen("dict.txt","r");    while(1)    {      fgets(buff2,1024,fp1);      p = buff2;      s = buff2;      y = buff2;      num = 0;      while(1)      {        while(*p!='x20')        {          p++;          y++;        }        if(*p == 'x20')        {          *p = ' ';        }        p = p+1;        num = num +1;        y = y+1;        while(*p =='x20')        {          p++;          y++;          num++;                }        while(*p!='n')        {          p++;          num++;        }        if(*p == 'n')        {          *p = ' ';        }        p = p+1;        num = num+1;        i++;        printf("di%dci charun",i);        //printf("s:%s,y:%s",s,y);        sprintf(sql,"insert into dir values(%d,"%s","%s") ",i,y);        sqlite3_exec(db,sql,&errmsg);        break;      }    }  }    
    
服务器端程序:  
/*************************************************************************  > File Name: server.c  > Author:  > Mail:  > Created Time: Mon 15 Jun 2015 03:59:26 AM PDT  ************************************************************************/      #include<stdio.h>  #include<stdlib.h>  #include<string.h>  #include<unistd.h>  #include<sys/types.h>  #include<sys/socket.h>  #include<netinet/in.h>  #include<arpa/inet.h>  #include<netinet/ip.h>  #include<sqlite3.h>      int callback(void * h,int f_num,char **f_val,char **f_name);  int main(int argc,const char *argv[])  {    int listenfd = socket(PF_INET,SOCK_STREAM,0);    if(listenfd == -1)    {      perror("socket");      exit(EXIT_FAILURE);    }    int connfd;    char *p,*z,*m;    void *h;    char *errmsg;    sqlite3 *db ;    char sql[256];    char buff[1024];    char buff1[1024];    char buff2[1024];    char buffer[1024];    char buffer2[1024];    struct sockaddr_in servaddr;    bzero(&servaddr,sizeof(servaddr));    servaddr.sin_family = PF_INET;    servaddr.sin_port = htons(8888);    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");    int on = 1;    if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))==-1)    {      perror("setsockopt");      exit(EXIT_FAILURE);    }    if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1)    {      perror("bind");      exit(EXIT_FAILURE);    }    printf("bind success!n");    listen(listenfd,8);    printf("listening...n");    fd_set readfds,tmpfds;        FD_ZERO(&readfds);    FD_SET(0,&readfds);    FD_SET(listenfd,&readfds);        int maxfd = listenfd;        while(1)    {      tmpfds = readfds;      //struct timeval tv ={2,0};      int ret = select((maxfd+1),&tmpfds,NULL);      if(ret == -1)      {        perror("select");        exit(EXIT_FAILURE);      }      else if(ret = 0)      {        ;      }      else      {       int i;        for(i = 0;i<=maxfd;i++)        {          if(FD_ISSET(i,&tmpfds))          {            if(i==STDIN_FILENO)            {              fgets(buff,sizeof(buff),stdin);              if(strncmp(buff,"quit",4)==0)              {                exit(0);              }              printf("shuru:%sn",buff);            }            else if(i==listenfd)            {              connfd = accept(listenfd,NULL);              FD_SET(connfd,&readfds);              maxfd = maxfd>connfd?maxfd:connfd;            }            else            {              int recvbytes = 0;              recvbytes = recv(i,buff,0);              if(recvbytes == -1)              {                perror("recv");                close(i);                FD_CLR(i,&readfds);              }              if(recvbytes == 0)              {                printf("client[%d] shutdownn",i);                close(i);                FD_CLR(i,&readfds);              }              printf("client:%sn",buff);              if(strncmp(buff,"zhuce",5)==0)              {                recv(i,0);                if(sqlite3_open("db.db",&db)!=SQLITE_OK)                {                  sqlite3_errmsg(db);                  exit(EXIT_FAILURE);                }                sqlite3_exec(db,"create table name (NAME TEXT,MIMA TEXT)",&errmsg);                p = buff;                z = buff;                m = buff;                while(1)                {                  while(*p!='n')                  {                    p++;                    m++;                  }                  if(*p == 'n')                  {                    *p = ' ';                  }                  p = p+1;                  m = m+1;                  while(*p!='n')                  {                    p++;                  }                  if(*p == 'n')                  {                    *p =' ';                  }                  sprintf(sql,"insert into name values("%s","%s")",z,m);                  printf("sql:%sn",sql);                  //break;                  if(sqlite3_exec(db,&errmsg)!=SQLITE_OK)                  {                    printf("insert error:%sn",errmsg);                    exit(EXIT_FAILURE);                  }                  break;                }                                              while(1)                {                  if(recv(i,buff1,sizeof(buff1),0)==-1)                  {                    perror("recv");                    exit(EXIT_FAILURE);                  }                                   buff1[strlen(buff1)-1]=' ';                  if(strncmp(buff1,4)==0)                  {                    send(i,0);                    exit(0);                  }                  sprintf(sql,"select * from dir where WORD = "%s" ",buff1);                  h = buff2;                  strcpy(buff2,"bucunzai");                  if(sqlite3_exec(db,callback,(void *)h,&errmsg) != SQLITE_OK)                  {                    printf("select error :%sn",errmsg);                    exit(EXIT_FAILURE);                  }                  //printf("xxxx:%sn",buff2);                                   if(send(i,buff2,sizeof(buff2),0)==-1)                  {                    perror("send");                    exit(EXIT_FAILURE);                  }                  //bzero(buff2,sizeof(buff2));                }              }              //if(strncmp(buff))              if(strncmp(buff,"denglu",6)==0)              {                recv(i,&db)!=SQLITE_OK)                {                  sqlite3_errmsg(db);                  exit(EXIT_FAILURE);                }                //sqlite3_exec(db,"select * from name where name = "%s" and mima = "%s"",sql);                                   h = buff2;                  strcpy(buff2,&errmsg)!=SQLITE_OK)                          {                    printf("insert error:%sn",errmsg);                    printf("zhaobudaozhegerenn");                    exit(EXIT_FAILURE);                  }                  printf("zhanghao:%sn",buff2);                  if(strncmp(buff2,"bucunzai",8)==0)                  {                    strcpy(buffer2,"quit");                    send(i,buffer2,sizeof(buffer2),0);                    exit(0) ;                   }                  else if(strncmp(buff2,8)!=0)                  {                    while(1)                    {                      if(recv(i,0)==-1)                      {                        perror("recv");                        exit(EXIT_FAILURE);                      }                                           buff1[strlen(buff1)-1]=' ';                      if(strncmp(buff1,4)==0)                      {                        send(i,0);                        exit(0);                      }                      sprintf(sql,buff1);                      h = buff2;                      if(sqlite3_exec(db,&errmsg) != SQLITE_OK)                      {                        printf("select error :%sn",errmsg);                        exit(EXIT_FAILURE);                      }                      //printf("xxxx:%sn",buff2);                                           if(send(i,0)==-1)                      {                        perror("send");                        exit(EXIT_FAILURE);                      }                    }                                     }                  break;                }                                            }                }          }        }      }    }  }  int callback(void *h,char **f_name)  {    printf("f_num = %dn",f_num);    int j = 0;    for(j = 0;j<f_num;j++)    {      printf("%s:%sn",f_name[j],f_val[j]);      sprintf(h,"%s:%sn",f_val[j]);    }       //printf("%s:%sn",f_name[f_num],f_val[f_num]);    return 0;  }    
    
客户端程序:  
/************************************************************************* > File Name: client.c > Author: > Mail: > Created Time: Mon 15 Jun 2015 04:24:26 AM PDT ************************************************************************/   #include<stdio.h> #include<stdlib.h> #include<string.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netinet/ip.h> #include<arpa/inet.h> #include<sqlite3.h>   int main(int argc,const char *argv[]) {   int clientfd;   char buff[1024];   char buff1[1024];   char buff2[1024];   char buff3[1024];   pid_t pid;   clientfd = socket(PF_INET,0);   if(clientfd == -1)   {     exit(EXIT_FAILURE);   }   struct sockaddr_in servaddr;   servaddr.sin_family = PF_INET;   servaddr.sin_port = htons(8888);   servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");     if(connect(clientfd,(struct sockaddr *)&servaddr,sizeof(servaddr))==-1)   {     perror("connect");     exit(EXIT_FAILURE);   }   printf("connected..n");   printf("-----zhuce / denglu-----n");   if((pid = fork())==-1)   {     perror("fork");     exit(EXIT_FAILURE);   }   else if(pid == 0)   {     while(1)     {       fgets(buff,stdin);             if(send(clientfd,0)==-1)       {         perror("send");         exit(EXIT_FAILURE);       }       //printf("buff:%sn",buff);       if(strncmp(buff,5)==0)       {         printf("****zhanghao****Enter****n");         fgets(buff1,stdin);         printf("******mima******Enter****n");         fgets(buff2,stdin);         strcat(buff1,buff2);         if(send(clientfd,0)==-1)         {           perror("send");           exit(EXIT_FAILURE);         }         printf("***input words**quit->tuichu***n");         //printf("quit tuichu n");         while(1)         {           fgets(buff3,sizeof(buff3),stdin);           if(send(clientfd,buff3,0)== -1)           {             perror("send");             exit(EXIT_FAILURE);                       }           if(strncmp(buff3,4)==0)                   {             //raise(SIGSTOP);             printf("child process exitn");             exit(0);           }          sleep(1);           printf("***input words**quit->tuichu***n");         }       }       if(strncmp(buff,6)==0)       {         printf("****zhanghao****Enter****n");         fgets(buff1,0)==-1)         {           perror("send");           exit(EXIT_FAILURE);         }               }     }   }   else   {     while(1)     {       if(recv(clientfd,0)==-1)       {         perror("recv");         exit(EXIT_FAILURE);       }       if(strncmp(buff,4)==0)       {         exit(0);       }       printf("%sn",buff);     }       }     return 0; }                          (编辑:李大同) 
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! 
                     |