然后是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***<<<<<<
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**<<<<<<<<<<<<
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