- 论坛徽章:
- 0
|
发送端代码:
void* SendThread(void*)
{
cout<<"create thread"<<endl;
Send sendfile;
for(;
{
if(sendqueue.empty())
continue;
pthread_mutex_lock(&sendMutex);
if(!sendqueue.empty())
{
sendJob newsendjob = sendqueue.pop();
pthread_mutex_unlock(&sendMutex);
sendfile.SendFile(newsendjob);
cout<<"client main one new sendjob has been send !"<<endl;
}
}
}
int Send::SendFile(sendJob newjob) {
cout<<"client send.cpp " <<newjob.PRname<<endl;
char Filepath[50];
int localsock,connectret,Filefd;
strcpy(Filepath,Client_FILE_PATH);
strcat(Filepath,newjob.PRname);
std::cout<<"Filepath is : "<<Filepath<<endl;
struct sockaddr_in client;
client.sin_family=AF_INET;
client.sin_port=htons(DP_LISTEN_PORT);
client.sin_addr.s_addr=inet_addr("127.0.0.1" ;
localsock=socket(AF_INET,SOCK_STREAM,0);
if(localsock<0)
{
perror("create socket failed!" ;
exit(1);
}
cout<<"create socket successfull"<<endl;
connectret=connect(localsock,(struct sockaddr*)&client,sizeof(client));
if(connectret<0)
{
perror("connect failed!" ;
exit(1);
}
cout<<"connect successfull"<<endl;
Filefd=open(Filepath,O_RDONLY);
int readline;
char buf[1024];
long t = lseek(Filefd,0,SEEK_SET);
cout<<"return value of lseek is : "<<t<<endl;
write(localsock,newjob.PRname,sizeof(newjob.PRname));
cout<<"1111 client send.cpp last : " <<newjob.PRname<<endl;
while((readline=read(Filefd,buf,sizeof(buf)))>0) {
write(localsock,buf,readline);
}
close(localsock);
cout<<"a File is transfered"<<endl;
return 1;
}
接收端代码:
void* getRequestThread(void*)
{
cout<<"start getRequestThread"<<endl;
struct sockaddr_in server_addr;
int listensock=socket(AF_INET,SOCK_STREAM,0);
if (listensock<0)
{
perror("create listen socket failed!" ;
exit(1);
}
cout<<"create socket successfull"<<endl;
bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family=AF_INET;
server_addr.sin_port=htons(DP_LISTEN_PORT);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1" ;
if(bind(listensock,(struct sockaddr*)&server_addr,sizeof(server_addr))<0)
{
perror("bind failed!" ;
close(listensock);
exit(1);
}
cout<<"bind successfull"<<endl;
if(listen(listensock,5)<0)
{
perror("listen failed" ;
exit(1);
}
cout<<"server is listen..."<<endl;
int sockIn;
struct sockaddr_in sock_in;
socklen_t sin_size=sizeof(struct sockaddr_in);
for(; {
if((sockIn=accept(listensock,(struct sockaddr*)&sock_in,&sin_size))<0)
{
perror("server accept failed!" ;
exit(1);
}
cout<<"get connection"<<endl;
requestQueue.push_back(sockIn);
cout<<"server push back is : "<<sockIn<<endl;
}
}
void* ReceiveThread(void*)
{
cout<<"receive thread"<<endl;
Receive receive;
while(1)
{
if(requestQueue.empty())
continue;
pthread_mutex_lock(&receivemutex);
if(!requestQueue.empty())
{
char filename[10];
int localsockIn=requestQueue.front();
cout<<"got request"<<endl;
requestQueue.pop_front();
pthread_mutex_unlock(&receivemutex);
cout<<"servermain receivethread requestQueue.front is : "<<localsockIn<<" and file name is "<<filename<<endl;
receive.receiveMessage(localsockIn,filename);
cout<<"one file is received"<<endl;
}
pthread_mutex_unlock(&receivemutex);
}
}
receiveMessage部分代码:
int Receive::receiveMessage(int sockIn,char FILEName[10])
{
int readline,filefd;
char buf[1024];
char FileName[50];
char PathName[30];
cout<<"server sockIn is : "<<sockIn<<endl;
readline = read(sockIn,FileName,sizeof(FileName));
cout<<"FileName is : "<<FileName<<endl;
char str[20];
int i=0;
sprintf(str,"%d",i);
strcpy(PathName,Server_FILE_PATH);
strcat(PathName,str);
strcat(PathName,FileName);
cout<<"server pathname is "<< athName<<" and FileName is "<<FileName<<endl;
if((filefd=open(PathName,O_WRONLY|O_CREAT|O_TRUNC,0644))<0)
{
perror("open file failed!");
exit(1);
}
while((readline=read(sockIn,buf,sizeof(buf)))>0)
write(filefd,buf,readline);
i++;
close(filefd);
close(sockIn);
return 1;
}
运行结果:
发送端:
create thread
client send.cpp tcl_grammer.pdf
Filepath is : /home/transferFile/client/tcl_grammer.pdf
create socket successfull
connect successfull
return value of lseek is : 0
1111 client send.cpp last : tcl_grammer.pdf
end ---------------
a File is transfered
client main one new sendjob has been send !
client send.cpp tcl_grammer.pdf
Filepath is : /home/transferFile/client/tcl_grammer.pdf
create socket successfull
connect successfull
return value of lseek is : 0
1111 client send.cpp last : tcl_grammer.pdf
接收端:
start getRequestThread
create socket successfull
bind successfull
server is listen...
receive thread
get connection
server push back is : 4
got request
servermain receivethread requestQueue.front is : 4 and file name is
server sockIn is : 4
FileName is : tcl_grammer.pdf
server pathname is /home/transferFile/server/0tcl_grammer.pdf and FileName is tcl_grammer.pdf
one file is received
get connection
server push back is : 5
got request
servermain receivethread requestQueue.front is : -1219105904 and file name is 釻 |
|