yoghi87
26-08-2009, 14:17
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
#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 );
}
CLIENT
#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 );
}
SERVER
#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 );
}
CLIENT
#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 );
}