socket实现进程通信传输任意大小文件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
printf("\nserver closed right now!!\n"); close(sockid); close(fileopen); exit(0);
}
int main(int argc,char* argv[]) {
signal(SIGINT,(__sighandler_t)f_quit); sockid = socket(PF_UNIX,SOCK_DGRAM,0); if(sockid == -1)
}
printf("file %s writed successfully!,writelen=%d\n \n",datarecv.filesavename,writelen);
} }
/************* server code over************** */
/* data.h */
if( (datasent.filelen - datasent.oplencount ) >= ( sizeof(datasent.filebuf) - 1 ) ){ datasent.shouldoplen = sizeof(datasent.filebuf) - 1 ;
} else{
datasent.shouldoplen = datasent.filelen -datasent.oplencount; } memset(&datasent.filebuf,0,sizeof(datasent.filebuf)); readdata(fd,&datasent,datasent.shouldoplen); sentdata(datasent);
printf("socker create error!"),exit(-1); printf("sockid = %d\n",sockid); struct sockaddr_un addr; memset(&addr,0,sizeof(addr)); addr.sun_family = PF_UNIX; memcpy(addr.sun_path ,"mysock",6); int r = bind(sockid,(struct sockaddr*)&addr,sizeof(addr)); if(r<0)
datasent.filelen = lseek(fd,0,SEEK_END); lseek(fd,0,SEEK_SET); printf("open file %s success!,the fd = %d,file length = %d\n",datasent.filename,fd,datasen t.filelen); datasent.oplencount = 0; int opnumber = 1; do{ lseek(fd,datasent.oplencount,SEEK_SET);
exit(-1); } printf("file %s write success fully!,writelen=%d\n",datasent.filename,writelen); printf("%s has already sent!\n \n",datasent.filename);
} /********************** client terminal code over******************* */
/***** ***************************************************************************** 把服务端保存为 socket_serive.c 客户端:socket_client.c 编译时如下:
gcc -g socket_service.பைடு நூலகம் -lpthread -o server gcc -g socket_client.c -o client
#define DATA_H #include "data.h" #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<fcntl.h> #include<signal.h> #include<pthread.h> #include<sys/socket.h> #include<sys/un.h> typedef unsigned int uint; struct data{
datasent.oplencount += datasent.shouldoplen;
printf(" opnumber = %d,shouldoplen = %d ,datasent.oplencount = %d \n",opnumber++,da tasent.shouldoplen,datasent.oplencount);
执行时: 在一端 执行 #./server
另一端执行 #./client ./ii.c han.c
或者
#./client ./zhou.jpg
han.jpg
其中 第一个参数是源文件名, 第二个参数是要保存的文件名, 这样就可以把 client 端 “./zhou. jpg“发送给 server 端 ,保存为 han.jpg ( 可以自定义路径),但路径长度不能太长,可通过修改 data.h 中的 struct data 中的 filename,filesavename 来改变大小。
int sockid;
int main(int argc,char* argv[]) {
sockid = socket(PF_UNIX,SOCK_DGRAM,0); if(sockid==-1)printf("socket create error!"),exit(-1); struct sockaddr_un addr; memset(&addr,0,sizeof(addr)); addr.sun_family = PF_UNIX; memcpy(addr.sun_path,"mysock",6); int r = connect(sockid,(struct sockaddr*)&addr,sizeof(addr)); if(r<0)printf("connect error!\n"),exit(-1); printf("connect success! \n");
lseek(fileopen,datarecv.oplencount,SEEK_SET);
if( ( writelen += write(fileopen,datarecv.filebuf,datarecv.shouldoplen) ) <0 ){ printf("write error!\n"),exit(-1);
char filename[30]; char filesavename[30]; uint filelen; uint shouldoplen; uint oplencount; char filebuf[90001];
printf("%m\n"),exit(-1); printf("read %s successfully ! readlen=%d\n",datasent->filename,readlen);
//printf("%s\n",datasent->filebuf); }
int sentdata(struct data datasent) {
int writelen; if((writelen = write( sockid,&datasent,sizeof(datasent)-1 ))<0){
printf("write error ,writelen=%d, sizeof(datasent): %d, error: %s\n",writelen, sizeof(dat asent), strerror(errno));
printf("the filelen =%d,the readlen=%d,the filebuf = %d\n",datarecv.filelen,r,sizeof(datarec v.filebuf)-1 );
printf("the shouldoplen = %d\n",datarecv.shouldoplen); printf("the oplencount = %d\n",datarecv.oplencount);
/*****************this is the socker_server terminal*******************/
#include "data.h"
int sockid; pthread_t tid; int fileopen;
__sighandler_t f_quit(void) {
made by 韩大卫 @吉林师范大学 *********************************************************************************/
/******************* this is the socket_client terminal.******************/ #include "data.h"
说明:在 clinet 端与 server 通信时,发送太大的文件,无法一次性发送,只能多次发送,这样就涉及到 几个问题? 发送多少次? 一次发送多大? Server 保存时候怎么保存?
我的解决办法是: 根据实际文件大小和自定义的 buf 来比较,通过循环判断,来决定一次读出多少字节, 进而发送多少字节,通过 lseek 来实现 。
} while( datasent.filelen > datasent.oplencount );
close(sockid); close(fd); } int readdata(int fd,struct data* datasent,int len) { int readlen; if((readlen = read( fd,datasent->filebuf,len ) )<0)
umask(0); fileopen = open((const char*)datarecv.filesavename,O_RDWR | O_CREAT , S_IRWXU | S_IR WXG | S_IROTH ); if(fileopen<0){
printf("fileopen error!\n"),exit(-1); } printf("file %s create and open successfully!\n",datarecv.filesavename);
printf("bind error!,r=%d\n",r),exit(-1); printf("bind success!\n");
struct data datarecv; int writelen = 0; while(1){
memset(&datarecv,0,sizeof(datarecv)); r = read( sockid,&datarecv,sizeof(datarecv) ); if(r<0) printf("read error!\n"); printf("the filename from client:\n %s\n",datarecv.filename);
struct data datasent; memset(&datasent,0,sizeof(datasent)); strcpy(datasent.filename, argv[1]); strcpy(datasent.filesavename,argv[2]); int fd = open(datasent.filename,O_RDWR); if(fd<0)printf("open %s failed!\n",datasent.filename),exit(-1);
}
int main(int argc,char* argv[]) {
signal(SIGINT,(__sighandler_t)f_quit); sockid = socket(PF_UNIX,SOCK_DGRAM,0); if(sockid == -1)
}
printf("file %s writed successfully!,writelen=%d\n \n",datarecv.filesavename,writelen);
} }
/************* server code over************** */
/* data.h */
if( (datasent.filelen - datasent.oplencount ) >= ( sizeof(datasent.filebuf) - 1 ) ){ datasent.shouldoplen = sizeof(datasent.filebuf) - 1 ;
} else{
datasent.shouldoplen = datasent.filelen -datasent.oplencount; } memset(&datasent.filebuf,0,sizeof(datasent.filebuf)); readdata(fd,&datasent,datasent.shouldoplen); sentdata(datasent);
printf("socker create error!"),exit(-1); printf("sockid = %d\n",sockid); struct sockaddr_un addr; memset(&addr,0,sizeof(addr)); addr.sun_family = PF_UNIX; memcpy(addr.sun_path ,"mysock",6); int r = bind(sockid,(struct sockaddr*)&addr,sizeof(addr)); if(r<0)
datasent.filelen = lseek(fd,0,SEEK_END); lseek(fd,0,SEEK_SET); printf("open file %s success!,the fd = %d,file length = %d\n",datasent.filename,fd,datasen t.filelen); datasent.oplencount = 0; int opnumber = 1; do{ lseek(fd,datasent.oplencount,SEEK_SET);
exit(-1); } printf("file %s write success fully!,writelen=%d\n",datasent.filename,writelen); printf("%s has already sent!\n \n",datasent.filename);
} /********************** client terminal code over******************* */
/***** ***************************************************************************** 把服务端保存为 socket_serive.c 客户端:socket_client.c 编译时如下:
gcc -g socket_service.பைடு நூலகம் -lpthread -o server gcc -g socket_client.c -o client
#define DATA_H #include "data.h" #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<string.h> #include<fcntl.h> #include<signal.h> #include<pthread.h> #include<sys/socket.h> #include<sys/un.h> typedef unsigned int uint; struct data{
datasent.oplencount += datasent.shouldoplen;
printf(" opnumber = %d,shouldoplen = %d ,datasent.oplencount = %d \n",opnumber++,da tasent.shouldoplen,datasent.oplencount);
执行时: 在一端 执行 #./server
另一端执行 #./client ./ii.c han.c
或者
#./client ./zhou.jpg
han.jpg
其中 第一个参数是源文件名, 第二个参数是要保存的文件名, 这样就可以把 client 端 “./zhou. jpg“发送给 server 端 ,保存为 han.jpg ( 可以自定义路径),但路径长度不能太长,可通过修改 data.h 中的 struct data 中的 filename,filesavename 来改变大小。
int sockid;
int main(int argc,char* argv[]) {
sockid = socket(PF_UNIX,SOCK_DGRAM,0); if(sockid==-1)printf("socket create error!"),exit(-1); struct sockaddr_un addr; memset(&addr,0,sizeof(addr)); addr.sun_family = PF_UNIX; memcpy(addr.sun_path,"mysock",6); int r = connect(sockid,(struct sockaddr*)&addr,sizeof(addr)); if(r<0)printf("connect error!\n"),exit(-1); printf("connect success! \n");
lseek(fileopen,datarecv.oplencount,SEEK_SET);
if( ( writelen += write(fileopen,datarecv.filebuf,datarecv.shouldoplen) ) <0 ){ printf("write error!\n"),exit(-1);
char filename[30]; char filesavename[30]; uint filelen; uint shouldoplen; uint oplencount; char filebuf[90001];
printf("%m\n"),exit(-1); printf("read %s successfully ! readlen=%d\n",datasent->filename,readlen);
//printf("%s\n",datasent->filebuf); }
int sentdata(struct data datasent) {
int writelen; if((writelen = write( sockid,&datasent,sizeof(datasent)-1 ))<0){
printf("write error ,writelen=%d, sizeof(datasent): %d, error: %s\n",writelen, sizeof(dat asent), strerror(errno));
printf("the filelen =%d,the readlen=%d,the filebuf = %d\n",datarecv.filelen,r,sizeof(datarec v.filebuf)-1 );
printf("the shouldoplen = %d\n",datarecv.shouldoplen); printf("the oplencount = %d\n",datarecv.oplencount);
/*****************this is the socker_server terminal*******************/
#include "data.h"
int sockid; pthread_t tid; int fileopen;
__sighandler_t f_quit(void) {
made by 韩大卫 @吉林师范大学 *********************************************************************************/
/******************* this is the socket_client terminal.******************/ #include "data.h"
说明:在 clinet 端与 server 通信时,发送太大的文件,无法一次性发送,只能多次发送,这样就涉及到 几个问题? 发送多少次? 一次发送多大? Server 保存时候怎么保存?
我的解决办法是: 根据实际文件大小和自定义的 buf 来比较,通过循环判断,来决定一次读出多少字节, 进而发送多少字节,通过 lseek 来实现 。
} while( datasent.filelen > datasent.oplencount );
close(sockid); close(fd); } int readdata(int fd,struct data* datasent,int len) { int readlen; if((readlen = read( fd,datasent->filebuf,len ) )<0)
umask(0); fileopen = open((const char*)datarecv.filesavename,O_RDWR | O_CREAT , S_IRWXU | S_IR WXG | S_IROTH ); if(fileopen<0){
printf("fileopen error!\n"),exit(-1); } printf("file %s create and open successfully!\n",datarecv.filesavename);
printf("bind error!,r=%d\n",r),exit(-1); printf("bind success!\n");
struct data datarecv; int writelen = 0; while(1){
memset(&datarecv,0,sizeof(datarecv)); r = read( sockid,&datarecv,sizeof(datarecv) ); if(r<0) printf("read error!\n"); printf("the filename from client:\n %s\n",datarecv.filename);
struct data datasent; memset(&datasent,0,sizeof(datasent)); strcpy(datasent.filename, argv[1]); strcpy(datasent.filesavename,argv[2]); int fd = open(datasent.filename,O_RDWR); if(fd<0)printf("open %s failed!\n",datasent.filename),exit(-1);