Red de conocimiento informático - Aprendizaje de programación - Problemas en la transferencia de archivos en la programación de red bajo Linux

Problemas en la transferencia de archivos en la programación de red bajo Linux

Estoy usando el siguiente código, pero no funciona. Esto es lo que armé. No sé qué salió mal. Por favor, dame algún consejo.

¡Gracias! siemprexun.

//server.c

#include#include#include#include#include

#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);//Crear un socket ip/tcp

if( sockfd <0){ printf("error al construir un socket\n\a");return -1;}

ser_addr.sin_family=AF_INET;

ser_addr.sin_port=htons ( puerto);

ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);

bzero(&(ser_addr.sin_zero),8);

ret= enlazar (sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));//Enlace de puerto

if(ret<0) {printf("error en el puerto de enlace\n\a") ;return -1;}

ret=listen(sockfd,10);//Escucha de puerto Para simplificar, solo se acepta una solicitud

if(ret<0){printf( " error al escuchar \n\a");return -1;}

return sockfd;}

int b_access(int sockfd,char *buffn)//Aceptar solicitud de servicio, Recibir archivos

{

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);//Aceptar solicitud

if(con_fd<0){printf("error al aceptar \n\a");close(sockfd);return -1;}< / p>

printf("cliente %s conectar....\n",inet_ntoa(*(struct sockaddr_in *)&cli_addr.sin_addr.s_addr));

int numbytes

if((numbytes=recv(con_fd, buffn, 50, 0)) == -1){error ( " r

ecv " ); salir ( 1 ); regresar 0;}

printf("el contenido de buffn es %s .......\n",buffn);

char buf[MAXSIZE];FILE *File;int nsize;

File=fopen(buffn,"w");//Abra un archivo para guardar el archivo transferido

if( Archivo ==NULL){ printf("error al crear el archivo de conservación %s \n\a",buf);close(sockfd return -1;}

else printf("el archivo está almacenado en %s....\n",buffn);

fflush(NULL);

while((nsize=recv(con_fd,buf,MAXSIZE,0)) != 0)//Recibir archivos

fwrite(buf,1,nsize,File); close(sockfd);//Cerrar el socket

return }

int main(){

int sockfd,sockfdfn;

char buffn[50];

sockfd=b_link(MYPORT);

p>

if(b_access(sockfd,buffn)>0)printf("archivo aceptado fin \n\a");

else { printf("archivo aceptado fallido \n\a") ;return 0;}

return 1;}

//client.c

#include#include#include#include

#include#include#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("el nombre del host no se pudo expresar \n\a");return -1;}}

printf("conectando a %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);//Crear un socket

if(sockfd<0){printf("error en el socket de construcción \n\a" ) ;return -1;}

ser_addr.sin_family=AF_INET;//Protocolo ip/tcp

ser_addr.sin_port=htons(mp);//Puerto de escucha del host, convertido a red forma numérica

memcpy(&(ser_addr.sin_addr.s_addr),addrs,sizeof(struct in_addr));//Dirección IP del host, forma numérica de red

bzero(&( ser_addr .sin_zero),8);

ret=connect(sockfd,(struct sockaddr *)&ser_addr,sizeof(struct sockaddr));//Establecer una conexión con el host

si ( ret<0){printf("error al conectar \n\a");return -1;}

return sockfd;};

int f_send(int sockfd,char * fs){

if (send(sockfd,fs,50,0) == -1){perror( " enviar error de nombre " );return 0;}

else printf ("¡el nombre del archivo se transfiere correctamente! \n\a");

ARCHIVO *Archivo;int nsize;int i=0;char temp[MAXSIZE];char buf[MAXSIZE];

File=fopen(fs,"r");//Abra el archivo a transferir

if(File==NULL){printf("file: ¡%s error al abrir!... . \n",fs);return -1; }

while((nsize=fread(buf,1,MAXSIZE,File))>0){send(sockfd,buf,nsize,0) }

if(feof(File)!=0)printf("¡el archivo se transfiere correctamente!\n%d",strlen(buf));

else printf("error en la lectura del archivo \n\a");

fclose(Archivo);close(sockfd);return 1;

};

int main(void ) {

int sockfd,sockfdfn;char hostname[100];char file[100];

printf("ingrese el nombre de host (ip) al que desea conectarse:") ;

fflush(NULL);gets(hostname);

printf("¡conexión exitosa!\ningrese el nombre del archivo que desea

transferencia:");

gets(file);fflush(NULL);

sockfd=b_connect(hostname,12000);

if(sockfd<= 0) devolver -1;

devolver f_send(sockfd,file);}