????Linux???????????
???????????? ???????[ 2015/2/4 17:58:55 ] ????????Linux ??????
????if(tempfd==ONLINEOUT)
????{
????return ONLINEOUT;
????}
????else
????{
????temp->sockfd=tempfd;
????return BOOTOK;
????}
????break;
????}
????case STEP:
????{
????int tempfd;
????tempfd=read_online_fd(db??errmsg??temp->toname);
????if(tempfd==ONLINEOUT)
????{
????return ONLINEOUT;
????}
????else
????{
????flag=update_flag(db??errmsg??temp->toname??0);
????if(flag==1)
????{
????temp->sockfd=tempfd;
????}
????return flag;
????}
????break;
????}
????case BAN:
????{
????int tempfd;
????tempfd=read_online_fd(db??errmsg??temp->toname);
????if(tempfd==ONLINEOUT)
????{
????return ONLINEOUT;
????}
????else
????{
????sayflag=read_online_flag(db??errmsg??temp->toname);
????if(sayflag==1)
????{
????return TOFLAGOK;
????}
????else
????{
????flag=update_flag(db??errmsg??temp->toname??1);
????if(flag==1)
????{
????temp->sockfd=tempfd;
????}
????return flag;
????}
????}
????break;
????}
????case SET:
????{
????int tempfd;
????tempfd=read_online_fd(db??errmsg??temp->toname);
????if(tempfd==ONLINEOUT)
????{
????flag=delete_user(db??errmsg??temp->toname);
????return flag;
????}
????else
????{
????return ONLINEIN;
????}
????break;
????}
????case SEE:
????{
????return SEEOK;
????break;
????}
????case DATA:
????{
????return DATAOK;
????break;
????}
????case SEND:
????{
????int tempfd;
????tempfd=read_online_fd(db??errmsg??temp->toname);
????if(tempfd==ONLINEOUT)
????{
????return ONLINEOUT;
????}
????else
????{
????temp->sockfd=tempfd;
????return SENDOK;
????}
????break;
????}
????case CHANGE:
????{
????flag=read_user(db??errmsg??temp->toname);
????if(flag==USERIN)
????{
????return REGNO;//???????
????}
????else
????{
????flag=update_user(db??errmsg??temp->name??temp->toname);//????
????if(flag==1)
????{
????sayflag=update_db_data(db??errmsg??temp->name??temp->toname);//???????
????return sayflag;
????}
????else
????{
????return 0;
????}
????}
????break;
????}
????}
????}
??????????????????????????????????????
????int cmd_user(sqlite3*db??char**errmsg??vpChat temp??int sockfd);
????socket.c???????
????#include"data.h"
????void mybzero(struct sockaddr_in*sin)//??server_addr_in?????и??
????{
????bzero(sin??sizeof(struct sockaddr_in));/*??????*/
????sin->sin_family=AF_INET;//
????sin->sin_addr.s_addr=htonl(INADDR_ANY);//????????κ?ip?????ip?????????????????
????sin->sin_port=htons(portnumber);//????????????????????
????}
????int mysocket()//????socket???????????TCPЭ??????
????{
????int lfd;
????if((lfd=socket(AF_INET??SOCK_STREAM??0))==-1)//AF_INET:IPV4;SOCK_STREAM:TCP
????{
????fprintf(stderr??"Socket error:%s a"??strerror(errno));
????exit(1);
????}
????return lfd;
????}
????void mybind(int lfd??struct sockaddr_in*sin) //????bind??????serer_addr??????sockfd??
????{
????if(bind(lfd??(struct sockaddr*)(sin)??sizeof(struct sockaddr))==-1)
????{
????fprintf(stderr??"Bind error:%s a"??strerror(errno));
????exit(1);
????}
????}
????void mylisten(int lfd)//?????????????????????
????{
????if(listen(lfd??20)==-1)
????{
????fprintf(stderr??"Listen error:%s a"??strerror(errno));
????exit(1);
????}
????}
????int myaccept(int lfd??struct sockaddr_in*cin??socklen_t*addr_len)//?????????????
????{
????int cfd;
????if((cfd=accept(lfd??(struct sockaddr*)cin??addr_len))==-1)
????{
????fprintf(stderr??"Accept error:%s a"??strerror(errno));
????exit(1);
????}
????return cfd;
????}
????void mywrite(vpChat temp)
????{
????int num;
????if((num=write(temp->sockfd??temp??sizeof(stChat)))==-1)
????{
????printf("send error! ");
????}
????}
????????????????
????????????????????????????????????????????????????????????????
????#include"data.h"
????#include"package.h"
????void display(stChat temp)//???????????????
????{
????printf("name=%s "??temp.name);
????printf("passwd=%s "??temp.passwd);
????printf("cmd=%d "??temp.cmd);
????printf("revert=%d "??temp.revert);
????printf("toname=%s "??temp.toname);
????printf("msg=%s "??temp.msg);
????printf("flag=%d "??temp.flag);
????printf("sockfd=%d "??temp.sockfd);
????printf("time=%s "??temp.time);
????printf("filename=%s "??temp.filename);
????}
????int main()
????{
????time_t timep;
????time(&timep);
????char*timedata=ctime(&timep);
????sqlite3*db=NULL;
????char*errmsg;
????open_db(&db);//???????
????creat_user_db(db??&errmsg);
????creat_data_db(db??&errmsg);
????creat_online_db(db??&errmsg);
????creat_server_db(db??&errmsg);
????insert_server_db(db??timedata??&errmsg);//??server????????????
????insert_server();
????int lfd;
????int cfd;
????int sfd;
????int rdy;
????stChat temp;
????struct sockaddr_in sin;
????struct sockaddr_in cin;
????int client[FD_SETSIZE];/*?????????????????????????*/
????int maxi;
????int maxfd;/*????????*/
????fd_set rset;
????fd_set allset;
????socklen_t addr_len;/*?????????*/
????int i;
????int n;
????int len;
????int opt=1;/*????????*/
????char addr_p[20];
????mybzero(&sin);
????lfd=mysocket();
????/*?????????????????????*/
????setsockopt(lfd??SOL_SOCKET??SO_REUSEADDR??&opt??sizeof(opt));
????mybind(lfd??&sin); //????bind??????serer_addr??????sockfd??
????mylisten(lfd);//?????????????????????
????printf(" ????????????????????? ");
????maxfd=lfd;/*???????????????г????*/
????maxi=-1;
????/*????????????????????????*/
????for(i=0;i<FD_SETSIZE;i++)
????{
????client[i]=-1;
????}
????FD_ZERO(&allset);/*????????????????*/
????FD_SET(lfd??&allset);/*???????????????????*/
????/*????????????????*/
????while(1)
????{
????rset=allset;
????/*????????????????????????*/
????rdy=select(maxfd+1??&rset??NULL??NULL??NULL);
????if(FD_ISSET(lfd??&rset))
????{
????addr_len=sizeof(sin);
????cfd=myaccept(lfd??&cin??&addr_len);//?????????????
????/*???????????λ??*/
????for(i=0;i<FD_SETSIZE;i++)
????{//printf("%d "??client[i]);
????if(client[i]<=0)
????{
????client[i]=cfd;/*????????????????????????????λ??*/
????break;
????}
????}
????/*?????????????????????????????????*/
????if(i==FD_SETSIZE)
????{
????printf("too many clients");
????exit(1);
????}
????FD_SET(cfd??&allset);/*???????????*/
????if(cfd>maxfd)/*?μ???????????*/
????{
????maxfd=cfd;
????}
????if(i>maxi)
????{
????maxi=i;
????}
????if(--rdy<=0)/*?????????????????*/
????{
????continue;
????}
????}
????/*??????????????????????*/
????for(i=0;i<FD_SETSIZE;i++)
????{
????if((sfd=client[i])<0)
????{
????continue;
????}
????if(FD_ISSET(sfd??&rset))
????{
????printf("?????sfd=%d?????????? "??sfd);
????n=read(sfd??&temp??sizeof(stChat));
????if(n==0)
????{
????printf("?????sfd=%d?????????????. "??sfd);
????delete_online_db(db??&errmsg??sfd);
????fflush(stdout);/*?????????*/
????close(sfd);
????FD_CLR(sfd??&allset);/*?????????????????*/
????client[i]=-1;
????}
????else
????{
????temp.sockfd=sfd;
????/*???????????????????*/
????inet_ntop(AF_INET??&cin.sin_addr??addr_p??sizeof(addr_p));
????addr_p[strlen(addr_p)]='