socket实现进程通信传输任意大小文件

  • 格式:pdf
  • 大小:45.35 KB
  • 文档页数:6

lseek(fileopen,datarecv.oplencount,SEEK_SET);
if( ( writelen += write(fileopen,datarecv.filebuf,datarecv.shouldoplen) ) <0 ){ printf("write error!\n"),exit(-1);
说明:在 clinet 端与 server 通信时,发送太大的文件,无法一次性发送,只能多次发送,这样就涉及到 几个问题? 发送多少次? 一次发送多大? Server 保存时候怎么保存?
我的解决办法是: 根据实际文件大小和自定义的 buf 来比较,通过循环判断,来决定一次读出多少字节, 进而发送多少字节,通过 lseek 来实现 。
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("%m\n"),exit(-1); printf("read %s successfully ! readlen=%d\n",datasent->filename,readlen);
//printf("%s\n",datasent->filebuf); }
int sentdata(struct data datasent) {
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);
datasent.oplencount += datasent.shouldoplen;
printf(" opnumber = %d,shouldoplen = %d ,datasent.oplencount = %d \n",opnumber++,da tasent.shouldoplen,datasent.oplencount);
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)
/client ./ii.c han.c
或者
#./client ./zhou.jpg
han.jpg
其中 第一个参数是源文件名, 第二个参数是要保存的文件名, 这样就可以把 client 端 “./zhou. jpg“发送给 server 端 ,保存为 han.jpg ( 可以自定义路径),但路径长度不能太长,可通过修改 data.h 中的 struct data 中的 filename,filesavename 来改变大小。
}
printf("file %s writed successfully!,writelen=%d\n \n",datarecv.filesavename,writelen);
} }
/************* server code over************** */
/* data.h */
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);
} 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)
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);
/*****************this is the socker_server terminal*******************/
#include "data.h"
int sockid; pthread_t tid; int fileopen;
__sighandler_t f_quit(void) {
#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{
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");
/***** ***************************************************************************** 把服务端保存为 socket_serive.c 客户端:socket_client.c 编译时如下:
gcc -g socket_service.c -lpthread -o server gcc -g socket_client.c -o client
made by 韩大卫 @吉林师范大学 *********************************************************************************/
/******************* this is the socket_client terminal.******************/ #include "data.h"
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));
if( (datasent.filelen - datasent.oplencount ) >= ( sizeof(datasent.filebuf) - 1 ) ){ datasent.shouldoplen = sizeof(datasent.filebuf) - 1 ;