练手毛坯作品基于LINUX的"QQ闹眼子版本"(包括服务器与客户端)

     
刚学QT几天,花了一个多星期里面的空闲时间,也是出于练手的目的哈,写了这个基于linux的“QQ闹眼子版本”毛坯品,基本的聊天,传文件功能啥的都有,代码的编写,以及图形界面的设计全部一人包办,所以界面风格看起来可能比较另类,没办法啦,本人的审美眼光打小就不好,我姐也老说我,当然了,这点我完全同意哈,呵呵,就是缺这东西啊 ,哎,所以不知道 到底怎么样才算得上是美观,好了,野棉花少扯了,为了方便观看,以前是贴图,由于这个毛坯品需要操作,时间有点长,所以就直接录了一段视频,以下为视频地址,传了半个多小时才传上去的,由于手机摄像,画面粗糙,望理解啦,亲,呵呵
http://v.youku.com/v_show/id_XNDEzMTU4NjEy.html,
采用了腾讯的QQ风格,但是又不同于它,自己设计的UI,估计看起来会有点另类,呵呵,文件传输时,有文件到了,主UI界面的蝴蝶灰飞起来,当你拒绝或是接受完成时,蝴蝶就会停止飞行了,再有文件来时,又会飞起来,注意消息框,还有聊天框哈,测试了一下,局域网文件传输100M网卡,传输一个100M的文件只需要10几秒钟左右,视频最后还有QQ闹眼子版本的SHELL文字版演示,可以和图形版互相通信的,那是纯基于LINUX编写的,没有用到QT得技术,理论上来说呢,QT程序是一旦代码写完,随之编译器的不同,完全不用修改直接运行于各大平台,微软WINDOWS,苹果MAC OS,LINUX各种版本,以及嵌入式平台,包括塞班现在的平台,诺基亚早已收购QT,并在其塞班的S60第五班,塞班3,以及后来的memo,还有好像是去年推出的N9,采用的MEEGO平台,实际上呢,也是基于LINUX的内核再加上QT的技术来做的文件系统,但是由于本人在写代码的时候,用到了好多LINUX下的库文件以及函数库,所以呢,只能在LINUX下编译啦,先贴上QQ闹眼子版服务器的代码啦!
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXPENDING 5
#define SERVERPORT 5000
#define BUFFERSIZE 500
#define MAXTHREADS 500
int NUM=-1;
char fileqq[10];
char filename[100];
char qqOnline[2500];
pthread_t threadNumber[MAXTHREADS]={0};
struct key
{
int number;
int socketFds;
char ipAddress[20];
};
typedef struct qq
{
char qqNumber[6];
char qqSecret[11];
char qqIP[20];
int socketFdIng;
     
} NODEQQ;
NODEQQ qqIng[MAXTHREADS];
NODEQQ qqp[MAXTHREADS];
void *clientDo(void *);
void initQQ(NODEQQ *,NODEQQ *,int);
char *getQQ(NODEQQ *,char *,int);
int checkQQ(NODEQQ *,char *,char *);
int sockFdGet(char *);
int onLine(char *,char *,int);
int outLine(char *,char *,int);
char *ipGetQQ(char *);
char *deleteSpaceInput(char *);
char *showQQOnline();
void main()
{
struct sockaddr_in addressClient;
struct sockaddr_in addressServer;
int sockFd,sockNewFd;
int addressLen= sizeof(struct sockaddr);
int receiveBytes;
int i=1;
struct key myKey;
pthread_attr_t threadAttr;
initQQ(qqp,qqIng,MAXTHREADS);
addressServer.sin_family=AF_INET;
addressServer.sin_addr.s_addr=INADDR_ANY;
addressServer.sin_port=htons(SERVERPORT);
if((sockFd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  perror("socket");
  exit(EXIT_FAILURE);
}
setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
if(bind(sockFd,(struct sockaddr *)&addressServer,sizeof(addressServer))<0)
{
  perror("bind");
  exit(EXIT_FAILURE);
}

while(1)
{
  listen(sockFd,MAXPENDING);
  if((sockNewFd=accept(sockFd,(struct sockaddr *)&addressClient,&addressLen))<0)
  {
   perror("accept");
   continue;
  }
  pthread_attr_init(&threadAttr);
  pthread_attr_setdetachstate(&threadAttr,PTHREAD_CREATE_DETACHED);
  for(i=0;i   {
   if(threadNumber[i]==0)
   {
    myKey.number=i;
    myKey.socketFds=sockNewFd;
    memset(myKey.ipAddress,0,sizeof(myKey.ipAddress));
    strcpy(myKey.ipAddress,inet_ntoa(addressClient.sin_addr));
    pthread_create(&threadNumber[i],&threadAttr,clientDo,(struct key *)&myKey);
    break;
   } 
  } 
 
}
close(sockFd);
exit(EXIT_SUCCESS);
}
void * clientDo(void *arv)
{
int i,value;
    struct key myKey;
    int sockFd,sockTmp;;
char buf[BUFFERSIZE],tmp[10];
char qq[3][BUFFERSIZE];
char *qqN;
char qqIP[20];
int ret;
memset(qqIP,0,sizeof(qqIP));
    myKey=*(struct key *)arv;
    sockFd = myKey.socketFds;
strcpy(qqIP,myKey.ipAddress);


while(1)
{
  memset(buf,0,sizeof(buf));
  if((ret=recv(sockFd,buf,sizeof(buf),0))<0)
  {
   perror("clientDO 1 recv");
   pthread_exit("NULL");
  }
  else if(ret==0)
  {
   perror("client disconnect");
   pthread_exit("NULL");
  }
  else
  {
  
   if(!strncmp(buf,"GETQQ",5))
   {
    memset(tmp,0,sizeof(tmp));
    strcpy(tmp,&buf[5]);
    if((qqN=getQQ(qqp,tmp,MAXTHREADS))==NULL)
    {
     memset(buf,0,sizeof(buf));
     sprintf(buf,"%s","SORRY");
     if(send(sockFd,buf,sizeof(buf),0)<0)
     {
      perror("clientDO 1 send");
      pthread_exit("NULL");
     }
    }
    else
    {
     memset(buf,0,sizeof(buf));
     sprintf(buf,"YOUQQ%s",qqN);
     if(send(sockFd,buf,sizeof(buf),0)<0)
     {
      perror("clientDO 2 send");
      pthread_exit("NULL");
     }
    
    }
   }
   else
   {
    memset(qq,0,sizeof(qq));
    strncpy(&qq[0][0],buf,5);
    strcpy(&qq[1][0],&buf[5]);
    if(!checkQQ(qqp,&qq[0][0],&qq[1][0]))
    {
     memset(buf,0,sizeof(buf));
     memset(tmp,0,sizeof(tmp));
     strcpy(tmp,&qq[0][0]);
     sprintf(buf,"%s","WELCM");
     if(send(sockFd,buf,sizeof(buf),0)<0)
     {
      perror("clientDO 6 send");
      pthread_exit("NULL");
     }
     NUM++;
     value=NUM;
     if(onLine(&qq[0][0],qqIP,sockFd))
     {
      perror("online!");
     }
    
     memset(buf,0,sizeof(buf));
     if(recv(sockFd,buf,sizeof(buf),0)<0)
     {
      perror("recv");
      pthread_exit("NULL");
     }
     if(!strncmp(buf,"QQLIN",5))
     {
      sprintf(buf,"%s","QQLIN");
      sprintf(&buf[5],"%s",showQQOnline());
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       pthread_exit("NULL");
      }
     }
     while(1)
     {
      int receives;
     
      char *fileqqIP;
     
      memset(qq,0,sizeof(qq));
      memset(buf,0,sizeof(buf));
      if((receives=recv(sockFd,buf,sizeof(buf),0))<0)
      {
       perror("read receives");
       pthread_exit("NULL");
      }
       else if(receives==0)
      {
       if(outLine(tmp,qqIP,value))
       {
        perror("outline send0");
       }
       threadNumber[myKey.number] = 0;
       NUM--;
       close(sockFd);
       pthread_exit("NULL");
      
      }
      strncpy(&qq[0][0],buf,5);
      strncpy(&qq[1][0],&buf[5],5);
      strcpy(&qq[2][0],&buf[10]);
      memset(buf,0,sizeof(buf));
     
      if(!strncmp(&qq[1][0],"TFILE",5))
      {
       memset(fileqq,0,sizeof(fileqq));
       memset(filename,0,sizeof(filename));
       strncpy(fileqq,&qq[2][0],5);
       strcpy(filename,&qq[2][5]);
       if((fileqqIP=ipGetQQ(fileqq))==NULL)
       {
        memset(buf,0,sizeof(buf));
        sprintf(buf,"NONEM");
        sprintf(&buf[5],"%s",fileqq);
        if(send(sockFd,buf,sizeof(buf),0)<0)
        {
         perror("send");
         pthread_exit("NULL");
        }
       
       }
       else
       {
        if(!strncmp(filename," ",1))
        {
         memset(buf,0,sizeof(buf));
         sprintf(buf,"TFNON");
         sprintf(&buf[5],"%s",fileqq);
         if(send(sockFd,buf,sizeof(buf),0)<0)
         {
          perror("send");
          pthread_exit("NULL");
         }
        }
        else
        {
         memset(buf,0,sizeof(buf));
         sprintf(buf,"TFASK");
         sprintf(&buf[5],"%s",&qq[0][0]);
         sprintf(&buf[10],"%s",filename);
         if(send(sockFdGet(fileqq),buf,sizeof(buf),0)<0)
         {
          perror("send");
          pthread_exit("NULL");
         }
        }
       
       }
      }
      else if(!strncmp(&qq[1][0],"READY",5))
      {
        sprintf(buf,"%s","READY");
        sprintf(&buf[5],"%s",ipGetQQ(&qq[0][0]));
        if(send(sockFdGet(&qq[2][0]),buf,sizeof(buf),0)<0)
        {
         perror("clientDO 8 send");
         pthread_exit("NULL");
        }
      
      }
     
      else if(!strncmp(&qq[1][0],"NORED",5))
      {
        sprintf(buf,"%s","NORED");
        sprintf(&buf[5],"%s",&qq[0][0]);
        if(send(sockFdGet(&qq[2][0]),buf,sizeof(buf),0)<0)
        {
         perror("clientDO 8 send");
         pthread_exit("NULL");
        }
      
      }
      else if(!strncmp(&qq[1][0],"QQLIN",5))
      {
       sprintf(buf,"%s","QQLIN");
       sprintf(&buf[5],"%s",showQQOnline());
       if(send(sockFd,buf,sizeof(buf),0)<0)
       {
       perror("send");
       pthread_exit("NULL");
       }
      }
      else if(!strncmp(&qq[1][0],"CHATA",5))
      {
       int i=0;
       sprintf(buf,"%s","MESAG");
       sprintf(&buf[5],"%s",&qq[0][0]);
       sprintf(&buf[10],"%s",&qq[2][0]);
       while(qqIng[i].socketFdIng!=0)
       {
        if(send(qqIng[i].socketFdIng,buf,sizeof(buf),0)<0)
        {
         perror("send");
         pthread_exit("NULL");
        }
        i++;
       }
      }
      else
      {
       if((sockTmp=sockFdGet(&qq[1][0]))>0)
       {
        sprintf(buf,"%s","MESAG");
        sprintf(&buf[5],"%s",&qq[0][0]);
        sprintf(&buf[10],"%s",&qq[2][0]);
        if(send(sockTmp,buf,sizeof(buf),0)<0)
        {
         perror("clientDO 3 send");
         pthread_exit("NULL");
        }
      
       }
       else
       {
        sprintf(buf,"%s","NONEM");
        sprintf(&buf[5],"%s",&qq[1][0]);
        if(send(sockFd,buf,sizeof(buf),0)<0)
        {
         perror("clientDO 4 send");
         pthread_exit("NULL");
        }
       }
      }
   
     }
    }
    else
    {
     memset(buf,0,sizeof(buf));
     sprintf(buf,"ERROR");
     if(send(sockFd,buf,sizeof(buf),0)<0)
     {
      perror("clientDO 5 send");
      pthread_exit("NULL");
     }
    
    }
   }
  }
}
}
void initQQ(NODEQQ *qqp,NODEQQ *qq,int number)
{
int i;
int num=10000;
char str[6];
memset(qqp,0,sizeof(qqp));
memset(qqIng,0,sizeof(qqIng));
memset(str,0,sizeof(str));
for(i=0;i {
  memset(str,0,sizeof(str));
  sprintf(str,"%d",num+i);
// memset(qqp[i].qqNumber,0,sizeof(qqp[i].qqNumber));
// memset(qqIng[i].qqNumber,0,sizeof(qqp[i].qqNumber));
// memset(qqp[i].qqSecret,0,sizeof(qqp[i].qqSecret));
  strcpy(qqp[i].qqNumber,str);
  //qqIng[i].socketFdIng=0;
}
}
char * getQQ(NODEQQ *qqp,char * ch,int number)
{
int i;
for(i=0;i {
  if(!strlen(qqp[i].qqSecret))
  {
   strcpy(qqp[i].qqSecret,ch);
   return qqp[i].qqNumber;
  }
}
return NULL;
}
int checkQQ(NODEQQ *head,char *qq,char *secret)
{
int i;
for(i=0;i {
  if((!strcmp(head[i].qqNumber,qq))&&(!strcmp(head[i].qqSecret,secret)))
  {
   return 0;
  }
}
return 1;
}
int sockFdGet(char *ch)
{
int i;
for(i=0;i {
  if(!strcmp(qqIng[i].qqNumber,ch))
  {
   return qqIng[i].socketFdIng;
  }
}
return -1;
}
int onLine(char *ch,char *qqIP,int sfd)
{

int i;
char buf[BUFFERSIZE];
int key=0;
memset(buf,0,sizeof(buf));

for(i=0;i {
  if(qqIng[i].socketFdIng==0)
  {
   qqIng[i].socketFdIng=sfd;
   strcpy(qqIng[i].qqNumber,ch);
   strcpy(qqIng[i].qqIP,qqIP);
   key=i;
   break;
  }
}
for(i=0;i {
  if(key==i)continue;
  memset(buf,0,sizeof(buf));
  sprintf(buf,"OLINE%s",ch);
  if(strlen(qqIng[i].qqNumber))
  {
   if(send(qqIng[i].socketFdIng,buf,sizeof(buf),0)<0)
   {
    perror("send online");
    return 1;
   }
  }
}
return 0;
}
int outLine(char *ch,char *qqIP,int val)
{
int i;
char buf[BUFFERSIZE];
qqIng[val].socketFdIng=0;
memset(qqIng[val].qqNumber,0,sizeof(qqIng[val].qqNumber));
memset(qqIng[val].qqIP,0,sizeof(qqIng[val].qqIP));
for(i=0;i {
  if(i==val)continue;
  memset(buf,0,sizeof(buf));
  sprintf(buf,"XLINE%s",ch);
  if(strlen(qqIng[i].qqNumber))
  {
   if(send(qqIng[i].socketFdIng,buf,sizeof(buf),0)<0)
   {
    perror("send outline");
    return 1;
   }
   memset(buf,0,sizeof(buf));
   sprintf(buf,"%s","QQLIN");
   sprintf(&buf[5],"%s",showQQOnline());
   if(send(qqIng[i].socketFdIng,buf,sizeof(buf),0)<0)
   {
    perror("send");
    return 1;
  
   }
  } 
     
}
return 0;
}
char *ipGetQQ(char *ch)
{
int i;
for(i=0;i {
  if(!strcmp(qqIng[i].qqNumber,ch))
  {
   return qqIng[i].qqIP;
  }
}
return NULL;
}
char *deleteSpaceInput(char *ch)
{
char *p=ch;
while(*p==' ')
{
  p++; 
}
return p;
}
char *showQQOnline()
{
memset(qqOnline,0,sizeof(qqOnline));
int i,k;
for(i=0,k=0;i {
  if(strlen(qqIng[i].qqNumber))
  {
   strcpy(&qqOnline[k],qqIng[i].qqNumber);
   k+=5;
  }
}
return qqOnline;
}

然后是QQ闹眼子版的SHELL文字版的客户端的代码:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXPENDING 5
#define SERVERPORT 5000
#define FILESERVERPORT 5500
#define BUFFERSIZE 500
int transferFileNum=0;
int fileReady=0;

char tranFQQ[10];
char tranFileName[100];
char recFQQ[10];
char sendFileName[100];
struct fileMessage
{

char fileIP[20];
char tFileName[100];
};
struct fileMessage tFM;

void *whileRead(void *arv);
void timeMessageShow();
char *deleteSpaceInput(char *);
//int directContantsShow(char *);
void *fileClient(void *arv);
void *fileServer(void *arv);
int numberFileNameInput(char *);
void showQQLine(char *);
int main(int arc,char **arv)
{
struct sockaddr_in addressServer;
struct hostent *host;
char buf[BUFFERSIZE];
int sockFd;
int receiveBytes;
char qqtmp[3][BUFFERSIZE];
if(arc < 2)
{
  fprintf(stderr,"USAGE: ./QQ HOSTNAME OR IP ADDRESS\n");
  exit(1);
}
if ((host = gethostbyname(arv[1])) == NULL)
{
  perror("gethostbyname");
  exit(1);
}
addressServer.sin_family=AF_INET;
addressServer.sin_addr=*((struct in_addr *)host->h_addr);
addressServer.sin_port=htons(SERVERPORT);
if((sockFd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  perror("socket");
  exit(EXIT_FAILURE);
}

memset(buf,0,sizeof(buf));

if(connect(sockFd,(struct sockaddr *)&addressServer, sizeof(struct sockaddr))<0)
{
  perror("connect");
  exit(EXIT_FAILURE);
}
while(1)
{
 
  memset(buf,0,sizeof(buf));
  memset(qqtmp,0,sizeof(qqtmp));
  printf("PLEASE INPUT QQ NUMBER AND PASSWD\n");
  printf("QQ NUMBER APPLICATION:GETQQ xxxx(xxx[<=10] THE PASSWD YOU WANT TO SET FOR QQ)\n");
  scanf("%s%s",&qqtmp[0][0],&qqtmp[1][0]);
  sprintf(buf,"%s",&qqtmp[0][0]);
  sprintf(&buf[5],"%s",&qqtmp[1][0]);
  if(send(sockFd,buf,sizeof(buf),0)<0)
  {
   perror("write");
   exit(EXIT_FAILURE);
  }
  if(recv(sockFd,buf,sizeof(buf),0)<0)
  {
   perror("write");
   exit(EXIT_FAILURE);
  }
  else
  {
   if(!strncmp(buf,"ERROR",5))
   {
    printf("INPUT ERROR PLEASE TRY AGAIN\n");
   }
   else if(!strncmp(buf,"SORRY",5))
   {
    printf("SORRY QQ ONLINE ARE FULL,PLEASE TRY AGAIN LATER\n");
   }
   else if(!strncmp(buf,"YOUQQ",5))
   {
    printf("QQ APPLICATE SUCCESSFULLY NUMBER:%s PASSWD:%s \n",&buf[5],&qqtmp[1][0]);
   }
   else if(!strncmp(buf,"WELCM",5))
   {
    printf("\t\t\t**************LINUX SHELLQQ*************\n");
    printf("\t\t\t***<<<<<<>>>>>>>****\n");
    printf("\t\t\t*BELOW ARE THE OPERATION AND FUNCTION***\n");
    printf("\t\t\t**<1>CHAT PERSONLY:QQNUMBER MESSAGE*****\n");
    printf("\t\t\t**<2>CHAT ROOM:CHATA MESSAGE************\n");
    printf("\t\t\t**<3>SEND FILE:TFILE QQNUMBER FILENAME**\n");
    printf("\t\t\t**<4>LIST QQ ONLINE:QQLIN***************\n");
    printf("\t\t\t**<5>QUIT QQ:QUIT***********************\n");
    printf("\t\t\t**SHOW ALL MESSAGE REALTIME BY THREAD***\n");
    printf("\t\t\t**<<<<<<<<<<<<>>>>>>>>>>**\n",&qqtmp[0][0]);
    printf("\t\t\t***********YOU ARE WELCOME**************\n");
   
    memset(buf,0,sizeof(buf));
    sprintf(buf,"%s","QQLIN");
    if(send(sockFd,buf,sizeof(buf),0)<0)
    {
     perror("send");
     exit(EXIT_FAILURE);
    }
    printf("NOW QQ:%s YOU CAN CHAT WITH OTHERS ONLINE\n",&qqtmp[0][0]);
    while(1)
    {
     char deleteSI[BUFFERSIZE-10];
     pthread_t threadNumber;
      pthread_create(&threadNumber,NULL,whileRead,(int *)&sockFd);
     memset(buf,0,sizeof(buf));
     memset(deleteSI,0,sizeof(deleteSI));
     memset(&qqtmp[1][0],0,BUFFERSIZE);
     memset(&qqtmp[2][0],0,BUFFERSIZE);
     printf("OPERATION :QQ MESSAGE(IF YOU WANT TO EXIT PLEASE ENTER:QUIT)\n");
     scanf("%s",&qqtmp[1][0]);
     //getchar();
     fgets(deleteSI,BUFFERSIZE-10,stdin);
     strcpy(&qqtmp[2][0],deleteSpaceInput(deleteSI));

     if(!strncmp(&qqtmp[1][0],"QUIT",4))
     {
      printf("****BYE BYE**********\n");
      close(sockFd);
      exit(EXIT_SUCCESS);
     }

    
     else if(!strncmp(&qqtmp[1][0],"TFILE",5))
     {
      memset(sendFileName,0,sizeof(sendFileName));
      strcpy(sendFileName,deleteSpaceInput(&qqtmp[2][5]));
      sendFileName[numberFileNameInput(sendFileName)]='\0';
      printf("\ntttt%s\n",sendFileName);
      sprintf(buf,"%s",&qqtmp[0][0]);
      sprintf(&buf[5],"%s","TFILE");
      strncpy(&buf[10],&qqtmp[2][0],5);
      strcpy(&buf[15],deleteSpaceInput(&qqtmp[2][5]));
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       exit(EXIT_FAILURE);
      }
     }

     else if((!strncmp(&qqtmp[1][0],"YES",3))&&transferFileNum==1)
     {
      transferFileNum=0;
      sprintf(buf,"%s",&qqtmp[0][0]);
      sprintf(&buf[5],"%s","READY");
      sprintf(&buf[10],"%s",tranFQQ);
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       exit(EXIT_FAILURE);
      }
      pthread_t threadNumber;
       pthread_create(&threadNumber,NULL,fileServer,(char *)tranFQQ);
     
     }
     else if((!strncmp(&qqtmp[1][0],"NO",2))&&transferFileNum==1)
     {
      transferFileNum=0;
      sprintf(buf,"%s",&qqtmp[0][0]);
      sprintf(&buf[5],"%s","NORED");
      sprintf(&buf[10],"%s",tranFQQ);
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       exit(EXIT_FAILURE);
      }
     }
     else if(!strncmp(&qqtmp[1][0],"QQLIN",5))
     {
      sprintf(buf,"%s",&qqtmp[0][0]);
      sprintf(&buf[5],"%s","QQLIN");
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       exit(EXIT_FAILURE);
      }
     }
     else if(!strncmp(&qqtmp[1][0],"CHATA",5))
     {
      sprintf(buf,"%s",&qqtmp[0][0]);
      sprintf(&buf[5],"%s","CHATA");
      sprintf(&buf[10],"%s",&qqtmp[2][0]);
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       exit(EXIT_FAILURE);
      }
     }

     else
     {
      sprintf(buf,"%s",&qqtmp[0][0]);
      sprintf(&buf[5],"%s",&qqtmp[1][0]);
      sprintf(&buf[10],"%s",&qqtmp[2][0]);
      if(send(sockFd,buf,sizeof(buf),0)<0)
      {
       perror("send");
       exit(EXIT_FAILURE);
      }
     }
    
    
    }
   
   }
  
  }
   
}
close(sockFd);
exit(EXIT_SUCCESS);

}

void * whileRead(void *arv)
{
int socketfd=*((int *)arv);
char buf[BUFFERSIZE],tmp[10];
int receives;
char fileIpaddress[20];
while(1)
{
  memset(fileIpaddress,0,sizeof(fileIpaddress));
  memset(buf,0,sizeof(buf));
  memset(tmp,0,sizeof(tmp));
  if((receives=recv(socketfd,buf,sizeof(buf),0))<0)
  {
   perror("whileRead 1 read");
   pthread_exit("NULL");
  }

  if(receives==0)
  {
   printf("\nQQ SERVER HAS BEEN QUIT NOT NORMAL PLEASE ENTER:QUIT\n");
   pthread_exit("NULL");
  }
 
  else
  {
   if(!strncmp(buf,"OLINE",5))
   {
    printf("\nQQ:%s***********ONLINE\n",&buf[5]);
   }
   else if(!strncmp(buf,"MESAG",5))
   {
    strncpy(tmp,&buf[5],5);
    printf("\nQQ%s:%s ",tmp,&buf[10]);
    timeMessageShow();
    //printf("\nMESSAGE:%s\n",&buf[10]);
   
   }
   else if(!strncmp(buf,"NONEM",5))
   {
    printf("\nQQ:%s IS ERROR OR NOT ONLINE\n",&buf[5]);
   }
   else if(!strncmp(buf,"XLINE",5))
   {
    printf("\nQQ:%s***********OFFLINE\n",&buf[5]);
   }
   else if(!strncmp(buf,"TFNON",5))
   {
    printf("\nNO FILE TO TRANSFER TO QQ:%s\n",&buf[5]);
   }
   else if(!strncmp(buf,"TFASK",5))
   {
    memset(tranFileName,0,sizeof(tranFileName));
    memset(tranFQQ,0,sizeof(tranFQQ));
    strcpy(tranFileName,&buf[10]);
    transferFileNum=1;
    strncpy(tranFQQ,&buf[5],5);
   
    printf("\nFILE:%sCOME FROM QQ:%s(DO YOU WANT TO RECEIVE?(ENTER YES OR NON))\n",tranFileName,tranFQQ);
   }
   else if(!strncmp(buf,"READY",5))
   {
    memset(tFM.fileIP,0,sizeof(tFM.fileIP));
    memset(tFM.tFileName,0,sizeof(tFM.tFileName));
   
    strcpy(tFM.fileIP,&buf[5]);
    strcpy(tFM.tFileName,sendFileName);
    pthread_t threadNumber;
     pthread_create(&threadNumber,NULL,fileClient,(struct fileMessage*)&tFM);
   }

   else if(!strncmp(buf,"NORED",5))
   {
    printf("\nQQ:%s REFUSE TO ACCEPT THE FIE YOU SEND\n",&buf[5]);
   }
  
   else if(!strncmp(buf,"QQLIN",5))
   {
   
    showQQLine(&buf[5]);
   }
  
  }
}
}

void *fileServer(void *arv)
{
int ret;
if((ret=pthread_detach(pthread_self()))<0)
{
  perror("\nfileServer pthread_detach\n");
}
struct sockaddr_in addressClient;
struct sockaddr_in addressServer;
char buf[BUFFERSIZE];
int sockFd,sockNewFd;
char tranfFQQ[10];
FILE *fs;
int addressLen= sizeof(struct sockaddr);
int receiveBytes;
int count=0;
memset(tranfFQQ,0,sizeof(tranfFQQ));
strcpy(tranfFQQ,(char *)arv);
addressServer.sin_family=AF_INET;
addressServer.sin_addr.s_addr=INADDR_ANY;
addressServer.sin_port=htons(FILESERVERPORT);
if((sockFd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  perror("socket");
  pthread_exit("NULL");
}
int i=1;
setsockopt(sockFd, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
if(bind(sockFd,(struct sockaddr *)&addressServer,sizeof(addressServer))<0)
{
  perror("bind");
  pthread_exit("NULL");
}
listen(sockFd,MAXPENDING);
if((sockNewFd=accept(sockFd,(struct sockaddr *)&addressClient,&addressLen))<0)
{
  perror("accept");
  pthread_exit("NULL");
}
printf("\nSTARTING RECEIVE FILE FROM QQ:%s.......\n",tranfFQQ);
while(1)
{
  receiveBytes=recv(sockNewFd,buf,sizeof(buf),0);
  if(receiveBytes<0)
  {
   perror("read");
   pthread_exit("NULL");
  }
  else if(receiveBytes==0)
  {
   break;
  }
  else
  {
   if(count==0)
    {
    if((fs=fopen(buf,"wb+"))==NULL)
    {
     perror("fopen");
     pthread_exit("NULL");
    }
    count++;
   }
   else
   {
    if(fwrite(buf,1,receiveBytes,fs)<0)
    {
     perror("fwrite");
     pthread_exit("NULL");
    }
   }
    
  }
 
}
printf("\nFILE RECEIVE OVER\n");
fclose(fs);
close(sockNewFd);
close(sockFd);
pthread_exit("NULL"); 
}

void *fileClient(void *arv)
{
int ret;
if((ret=pthread_detach(pthread_self()))<0)
{
  perror("\nfileClient pthread_detach\n");
}
struct fileMessage myFM;
struct sockaddr_in addressServer;
char buf[BUFFERSIZE];
int sockFd;
FILE *fs;
int sendBytes;
int num=BUFFERSIZE;
char ip[20];
char fn[100];
myFM=*(struct fileMessage *)arv;
memset(ip,0,sizeof(ip));
memset(fn,0,sizeof(fn));
strcpy(ip,myFM.fileIP);
strcpy(fn,sendFileName);
addressServer.sin_family=AF_INET;
addressServer.sin_addr.s_addr=inet_addr(ip);
addressServer.sin_port=htons(FILESERVERPORT);
if((sockFd=socket(AF_INET,SOCK_STREAM,0))<0)
{
  perror("socket");
  pthread_exit("NULL");
}

memset(buf,0,sizeof(buf));
strcpy(buf,fn);
if((fs=fopen(buf,"rb"))==NULL)
{
  perror("fopen");
  pthread_exit("NULL");
}
sleep(1);// let fileServer building firsrly
if(connect(sockFd,(struct sockaddr *)&addressServer, sizeof(struct sockaddr))<0)
{
  perror("connect");
  pthread_exit("NULL");
}
printf("\nSENDDING FILE ......\n");
while(1)

  if(send(sockFd, buf,num, 0)<0)
  {
   perror("write");
   pthread_exit("NULL");
  }
  memset(buf,0,sizeof(buf));
 
  if(feof(fs))
  {
   break;
  }
 
  if((num=fread(buf,1,sizeof(buf),fs))<0)
  {
   perror("fread");
   pthread_exit("NULL");
  }
}
printf("\nSEND FILE OVER\n");
close(sockFd);
fclose(fs);
pthread_exit("NULL");

}

void timeMessageShow()
{
char *day[]={"Sunday","Monday","Tuseday","Wednesday","Thirsday","Friday","Saturday"};
time_t timev;
struct tm *tp;
time(&timev);
tp=localtime(&timev);
printf("%d-%d-%d ",1900+tp->tm_year,1+tp->tm_mon,tp->tm_mday);
printf("%s %d:%d:%d\n",day[tp->tm_wday],tp->tm_hour,tp->tm_min,tp->tm_sec);

}

char *deleteSpaceInput(char *ch)
{
char *p=ch;
while(*p==' ')
{
  p++; 
}
return p;
}

int numberFileNameInput(char *ch)
{
int i=0;
char *p=ch;
while(*p!='\n')
{
  p++;
  i++;   
}
return i;

}
#if 0


int directContantsShow(char *address)
{
DIR *dp;
struct dirent *sdp;
if((dp=opendir("./"))<0)
{
   perror("open ./");
   return -1;
}
while((sdp=readdir(dp))!=NULL)
{
  if(!strcmp(sdp->d_name,address))
  {
   closedir(dp);
   return 1; 
  }
}
closedir(dp);
return 0;

}
#endif

void showQQLine(char * ch)
{
char *p=ch;
char buf[5]={0};
int number,i,k;
number=strlen(p)/5;
printf("\nQQ NUMBER ON LINE:%d\n",number);
for(i=0,k=0;i {
  strncpy(buf,&ch[k],5);
  printf("QQ NUMBER******%s******\n",buf);
}

}
由于QT版本的图形客户端代码是用C++框架写的,文件数量比较多,代码量比较大,所以就直接打包上传了,下面是下载地址,给需要的朋友可以参考参考!涉及到SOCKET套接字,多线程等等知识点,希望对你们有帮助!本人菜鸟一个,代码书写水平有限!!见谅!!

http://115.com/file/dpv5u2q3

你可能感兴趣的:(练手毛坯作品基于LINUX的"QQ闹眼子版本"(包括服务器与客户端))