|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Feb 2008
Messaggi: 84
|
[RISOLTO] problema invio file socket...
Salve, ho questi 2 codici un client e un server, dovrebbe mandare il file che gli dò quando lancio il client "./client localhost filename" me lo manda ma la risposta del server è un errore di apertura file... perchè???
SERVER Codice:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/socket.h>
#include <netdb.h>
#define SERVER_PORT 12345 /* arbitrary, but client and server must agree */
#define BUF_SIZE 4096 /* block transfer size */
#define QUEUE_SIZE 10
int main ( int argc , char *argv[] )
{
int s, b, l, fd, sa, bytes, on = 1;
char buf[BUF_SIZE]; /* buffer for outgoing file */
struct sockaddr_in channel; /* hold's IP address */
/* Build address structure to bind to socket. */
memset ( &channel , 0 , sizeof ( channel ) ); /* zero channel */
channel.sin_family = AF_INET;
channel.sin_addr.s_addr = htonl ( INADDR_ANY );
channel.sin_port = htons ( SERVER_PORT );
/* Passive open. Wait for connection. */
s = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP ); /* create socket */
if ( s < 0 ) fatal ( "socket failed" );
b = bind ( s, (struct sockaddr *) &channel , sizeof ( channel ) );
if ( b < 0 ) fatal ( "bind failed" );
l = listen ( s , QUEUE_SIZE ); /* specify queue size */
if ( l < 0 ) fatal ( "listen failed" );
/* Socket is now set up and bound. Wait for connection and process it. */
while ( 1 )
{
sa = accept ( s , 0 , 0 ); /* block for connection request */
if ( sa < 0 ) fatal ( "accept failed" );
read ( sa , buf , BUF_SIZE ); /* read file name from socket */
/* Get and return the file. */
fd = open ( buf , O_RDONLY ); /* open the file to be sent back */
if ( fd < 0 ) fatal ( "open failed" );
while ( 1 )
{
bytes = read ( fd , buf , BUF_SIZE ); /* read from file */
if ( bytes <= 0 ) break; /* check for end of file */
write ( sa , buf , bytes ); /* write bytes to socket */
}
close ( fd ); /* close file */
close ( sa ); /* close connection */
}
}
fatal ( char *string )
{
printf ( "%s\n" , string );
exit ( 1 );
}
Codice:
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define SERVER_PORT 12345 /* arbitrary, but client and server must agree */
#define BUF_SIZE 4096 /* block transfer size */
int main ( int argc , char **argv )
{
int c, s, bytes;
char buf[BUF_SIZE]; /* buffer for incoming file */
struct hostent *h; /* info about server */
struct sockaddr_in channel; /* holds IP address */
if ( argc != 3 ) fatal ( "Usage: client server-name file-name" );
h = gethostbyname ( argv[1] ); /* look up host's IP address */
if ( ! h ) fatal ( "gethostbyname failed" );
s = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP );
if (s < 0) fatal("socket");
memset ( &channel , 0 , sizeof ( channel ) );
channel.sin_family = AF_INET;
memcpy ( &channel.sin_addr.s_addr , h->h_addr , h->h_length );
channel.sin_port = htons ( SERVER_PORT );
c = connect ( s, ( struct sockaddr * ) &channel , sizeof ( channel ) );
if ( c < 0 ) fatal ( "connect failed" );
/* Connection is now established. Send file name including 0 byte at end. */
write ( s , argv[2] , strlen ( argv[2] ) + 1 );
/* Go get the file and write it to standard output. */
while ( 1 )
{
bytes = read ( s , buf , BUF_SIZE ); /* read from socket */
if ( bytes <= 0 ) exit ( 0 ); /* check for end of file */
write ( 1 , buf , bytes ); /* write to standard output */
}
}
fatal ( char *string )
{
printf ( "%s\n" , string );
exit ( 1 );
}
Ultima modifica di yoghi87 : 29-08-2009 alle 19:02. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2780
|
Prova a tenere memoria del valore restituito da read e con questo aggiungi in fondo al buffer un null character '\0'
|
|
|
|
|
|
#3 |
|
Member
Iscritto dal: Feb 2008
Messaggi: 84
|
cioè??
|
|
|
|
|
|
#4 | |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2780
|
Deduco che lo hai copia-incollato...
Dove c'è scritto: Codice:
read ( sa , buf , BUF_SIZE ); /* read file name from socket */ /* Get and return the file. */ fd = open ( buf , O_RDONLY ); /* open the file to be sent back */ Codice:
bytes=read ( sa , buf , BUF_SIZE ); /* read file name from socket */
buf[bytes]='\0';
printf("SERVER: Provo ad aprire %s\n",buf);
/* Get and return the file. */
fd = open ( buf , O_RDONLY ); /* open the file to be sent back */
Un altro problema potrebbe essere il nome del file, usi un nome che contiene spazi? Se sì ne devi fare l'escape quando li digiti. E ancora: il file che richiedi deve essere visibile al server, il server dove lo hai lanciato? Se mi vengono in mente altri possibili problemi ti faccio sapere, intanto vedi se è uno di questi. PS: cosa vuol dire che Quote:
|
|
|
|
|
|
|
#5 |
|
Member
Iscritto dal: Feb 2008
Messaggi: 84
|
allora, se provo a lanciare il server e il client con il file che si trovano tutti nella stessa cartella ricevo una risposta dal client in cui c'è scritto il contenuto del file.
se lancio il server e il client in 2 directory differenti, con il file di testo che si trova insieme al client succede che il client termina e il server dopo la tua modifica mi dice: SERVER: Provo ad aprire open failed ecco, io volevo che il client mandasse il file e il server lo visualizzasse o creava un file nella sua directory con il contenuto del file inviato... |
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2780
|
Allora devi modificare il programma perché questo fa il contrario: il client richiede un file al server e questo glielo invia. Per questo il test che hai fatto non funziona.
Inoltre puoi richiedere al server solo un file ad esso visibile. |
|
|
|
|
|
#7 |
|
Member
Iscritto dal: Feb 2008
Messaggi: 84
|
cioè quindi diciamo che se metto il file nella cartella del server ed eseguo il server, poi eseguo il client chiedendo quel file il risultato sarà la visualizzazione del file nel client..???
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Nov 2005
Messaggi: 2780
|
Esatto.
|
|
|
|
|
|
#9 |
|
Member
Iscritto dal: Feb 2008
Messaggi: 84
|
ok grazie di tutto..!!!!
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 10:17.



















