1.client.c
#define _GNU_SOURCE
#include <func.h>
int recvCycle(int sfd,void *buf,int dateLen);
int main(int argc,char *argv[])
{
//申请套接字文件描述符
int sfd = socket(AF_INET,SOCK_STREAM,0);
ERROR_CHECK(sfd,-1,"socket");
//链接套接字和IP端口
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(argv[1]);
saddr.sin_port = htons(atoi(argv[2]));
//建立连接
int ret = connect(sfd,(struct sockaddr *)&saddr,sizeof(struct sockaddr));
ERROR_CHECK(ret,-1,"connect");
client_Screen(sfd);
char buf[1024]={0};
//接收文件名
int dateLen = 0;
recv(sfd,&dateLen,4,0);
recv(sfd,buf,dateLen,0);
int fd = open(buf,O_RDWR|O_CREAT,0666);
//接收文件总长度
off_t fileSize = 0;
off_t recvLen = 0;
recv(sfd,&dateLen,4,0);
recv(sfd,&fileSize,dateLen,0);
printf("dateLen = %d,fileSize = %ld\n",dateLen,fileSize);
//定义文件传输开始结束时间
struct timeval begin,end;
gettimeofday(&begin,NULL);
int count = 0;
//mmap接收数据
/* ftruncate(fd,fileSize);//先给文件占据文件大小的空间 */
/* char *pMap = (char*)mmap(0,fileSize,PROT_WRITE|PROT_READ,MAP_SHARED,fd,0); */
/* ERROR_CHECK(pMap,(char *)-1,"pMap"); */
/* recv(sfd,pMap,fileSize,MSG_WAITALL); */
/* munmap(pMap,fileSize); */
//splice接收数据
/* int splicefd[2]; */
/* pipe(splicefd); */
/* while(recvLen < fileSize){ */
/* int ret = splice(sfd,0,splicefd[1],0,fileSize,0); */
/* ret = splice(splicefd[0],0,fd,0,ret,0); */
/* recvLen += ret; */
/* } */
//定义进度条变量
float rate = 0;
//send和recv函数收发数据
while(1)
{
//接收数据并存入文件
memset(buf,0,sizeof(buf));
recv(sfd,&dateLen,4,0);
int ret = recvCycle(sfd,buf,dateLen);
/* if(0 == dateLen){ */
/* break; */
/* } */
/* ret = recv(sfd,buf,dateLen,MSG_WAITALL); */
/* printf("datelen = %d\n",dateLen); */
recvLen += ret;
++count;
//打印进度条
rate = (float)recvLen/fileSize*100;
if(50000 == count){
count = 0;
fflush(stdout);
for(int j = 0;j < 50*(int)rate/100;++j)
{
printf(">");
}
printf("rate = %5.2f%%\r",rate);
}
if(ret == 0)
{
break;
}
write(fd,buf,ret);
}
for(int j = 0;j < 70;++j)
{
printf(" ");
}
printf("\r");
gettimeofday(&end,NULL);
printf("totaltime = %ld\n",(end.tv_sec - begin.tv_sec)*1000000 + end.tv_usec - begin.tv_usec);
close(sfd);
return 0;
}
2.Client_Screen.c
#include <func.h>
int pause();
int pause()
{
puts("after inback Press any key to continue");
system("stty raw");
getchar();
system("stty cooked");
return 0;
}
int client_Screen(int sfd)
{
while(1)
{
char ch;
printf("Do you want to use this app?(y/n):");
scanf("%c",&ch);
putchar(ch);
if(0 == memcmp(&ch,"y",1))
{
send(sfd,&ch,1,0);
pause();
system("clear");
break;
}
else if(0 == memcmp(&ch,"n",1))
{
send(sfd,&ch,1,0);
return 0;
}
else
{
printf("You put wrong message, please try again\n");
pause();
system("clear");
}
}
char salt[20] = {0};
char buf[1024] = {0};
char *passwd;
char *password;
while(1)
{
printf("------------------------------------------------------------\n");
printf("\n");
printf(" Welcome to Wangdao SP\n");
printf("\n");
printf("------------------------------------------------------------\n");
int messageFlag = 0;
char messageBuf[128];
memset(buf,0,sizeof(buf));
recv(sfd,buf,sizeof(buf),0);
sscanf(buf,"%d%s",&messageFlag,messageBuf);
//messageFlag接收的数值用来表示接收的信息类型,不同类型进入下面不同的入口
if(1 == messageFlag)
{
printf("%s",buf);
memset(buf,0,sizeof(buf));
scanf("%s",buf);
send(sfd,buf,strlen(buf),0);
getchar();
}
else if(0 == messageFlag)
{
printf("%s\n",buf);
send(sfd,"ok",2,0);
/* getchar(); */
}
else if(2 == messageFlag)
{
printf("%s",buf);
memset(buf,0,sizeof(buf));
/* scanf("%s",buf); */
password = getpass("Input your password");
passwd = crypt(password,(const char*)salt);
memset(buf,0,sizeof(buf));
sprintf(buf,"%s",passwd);
send(sfd,buf,strlen(buf),0);
}
else if(3 == messageFlag)
{
memset(salt,0,sizeof(salt));
strcpy(salt,messageBuf);
send(sfd,"ok",2,0);
system("clear");
continue;
}
else if(4 == messageFlag)
{
printf("%s\n",buf);
return 0;
}
else
{
printf("recv message error,please check\n");
getchar();
}
pause();
system("clear");
}
}