Linux下网络编程文件传送问题
发布网友
发布时间:2022-05-05 23:06
我来回答
共3个回答
热心网友
时间:2022-06-28 06:50
我用的就是下面的代码,就是不行,这就是我组合的,不知道哪里出问题了,请高手指点。谢谢!foreverxun.
//server.c
#include<stdlib.h>#include<stdio.h>#include<sys/types.h>#include<netdb.h>#include<errno.h>
#define MYPORT 12000#define BACKLOG 2#define MAXSIZE 4096
int b_link(int port){
int sockfd,ret,len,con_fd; struct sockaddr_in ser_addr;int nsize;
sockfd=socket(AF_INET,SOCK_STREAM,0);//建立一个ip/tcp套接口
if(sockfd<0){ printf("error in building a socket\n\a");return -1;}
ser_addr.sin_family=AF_INET;
ser_addr.sin_port=htons(port);
ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);
bzero(&(ser_addr.sin_zero),8);
ret=bind(sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));//端口绑定
if(ret<0) {printf("error in binding port\n\a");return -1;}
ret=listen(sockfd,10);//端口监听.简单起见,只接受一个请求
if(ret<0){printf("error in listening \n\a");return -1;}
return sockfd;}
int b_access(int sockfd,char *buffn)//接受服务请求,接收文件
{
int con_fd;struct sockaddr_in cli_addr;int sin_size;sin_size=sizeof(struct sockaddr_in);
con_fd=accept(sockfd,(struct sockadd*)&cli_addr,&sin_size);//接受请求
if(con_fd<0){printf("error in accepting \n\a");close(sockfd);return -1;}
printf("client %s connect.......\n",inet_ntoa(*(struct sockaddr_in *)&cli_addr.sin_addr.s_addr));
int numbytes;
if((numbytes=recv(con_fd, buffn, 50, 0)) == -1){perror ( " recv " ) ;exit ( 1 ) ;return 0;}
printf("buffn content is %s .......\n",buffn);
char buf[MAXSIZE];FILE *File;int nsize;
File=fopen(buffn,"w");//打开一个文件用来保存传送的文件
if(File==NULL){ printf("error in creating conserve file %s \n\a",buf);close(sockfd); return -1;}
else printf("the file is stored in %s.....\n",buffn);
fflush(NULL);
while((nsize=recv(con_fd,buf,MAXSIZE,0))!=0)//接收文件
fwrite(buf,1,nsize,File); close(sockfd);//关闭套接口
return 1; }
int main(){
int sockfd,sockfdfn;
char buffn[50];
sockfd=b_link(MYPORT);
if(b_access(sockfd,buffn)>0)printf("file accept end \n\a");
else { printf("file accept failed \n\a");return 0;}
return 1;}
//client.c
#include<sys/types.h>#include<sys/socket.h>#include<netdb.h>#include<string.h>
#include<stdlib.h>#include<stdio.h>#define MAXSIZE 8096#define MYPORT 12000
int b_connect(char *hostname,int mp){
int sockfd,ret,len;struct sockaddr_in ser_addr;char *myname;struct hostent *sh;struct in_addr *addrs;
if((sh=gethostbyname(hostname))==NULL){
if((sh=gethostbyaddr(hostname,4,AF_INET))==NULL){printf("the host name couldn't be expressed \n\a");return -1;}}
printf("connecting to %s ......\n",inet_ntoa(*(struct sockaddr *)sh->h_addr_list[0]));
addrs=(struct in_addr *)sh->h_addr_list[0];
sockfd=socket(AF_INET,SOCK_STREAM,0);//创建一个套接口
if(sockfd<0){printf("error in building socket \n\a");return -1;}
ser_addr.sin_family=AF_INET;//协议ip/tcp
ser_addr.sin_port=htons(mp);//主机监听端口,转换成网络数字形式
memcpy(&(ser_addr.sin_addr.s_addr),addrs,sizeof(struct in_addr));//主机ip地址,网络数字形式
bzero(&(ser_addr.sin_zero),8);
ret=connect(sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));//与主机建立连接
if(ret<0){printf("error in conneting \n\a");return -1;}
return sockfd;};
int f_send(int sockfd,char *fs){
if (send(sockfd,fs,50,0) == -1){perror( " send fname error " );return 0;}
else printf("file name transfers succussly! \n\a");
FILE *File;int nsize;int i=0;char temp[MAXSIZE];char buf[MAXSIZE];
File=fopen(fs,"r");//打开要传送的文件
if(File==NULL){printf("file: %s open failed!.....\n",fs);return -1; }
while((nsize=fread(buf,1,MAXSIZE,File))>0){send(sockfd,buf,nsize,0);}
if(feof(File)!=0)printf("file transfers succussly!\n%d",strlen(buf));
else printf("error in reading file \n\a");
fclose(File);close(sockfd);return 1;
};
int main(void){
int sockfd,sockfdfn;char hostname[100];char file[100];
printf("please input the hostname(ip) you want to connect:");
fflush(NULL);gets(hostname);
printf("connect success!\nplease input the filename you want to transfer:");
gets(file);fflush(NULL);
sockfd=b_connect(hostname,12000);
if(sockfd<=0) return -1;
return f_send(sockfd,file);}
热心网友
时间:2022-06-28 06:50
问题描述不清楚,无法回答。
热心网友
时间:2022-06-28 06:51
说清楚两者组合是怎么组合的才能知道问题在哪