23,115
社区成员
发帖
与我相关
我的任务
分享
/* server.c - a socket-based file downloaded server
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <time.h>
#include <strings.h>
#define PORTNUM 15000 /* our service number */
#define HOSTLEN 256
#define oops(msg) { perror(msg) ; exit(1) ; }
int main(int ac, char *av[])
{
struct sockaddr_in saddr; /* build our address here */
struct hostent *hp; /* this is part of our */
char hostname[HOSTLEN]; /* address */
int sock_id,sock_fd; /* line id, file desc */
char buf[BUFSIZ];
char filename[256];
int fd; /* downloaded file */
int nchar;
char *tty;
/*
* Step 1: ask kernel for a socket
*/
sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a socket */
if ( sock_id == -1 )
oops( "socket" );
/*
* Step 2: bind address to socket. Address is host,port
*/
bzero( (void *)&saddr, sizeof(saddr) ); /* clear out struct */
gethostname( hostname, HOSTLEN ); /* where am I ? */
hp = gethostbyname( hostname ); /* get info about host */
/* fill in host part */
bcopy( (void *)hp->h_addr, (void *)&saddr.sin_addr, hp->h_length);
saddr.sin_port = htons(PORTNUM); /* fill in socket port */
saddr.sin_family = AF_INET ; /* fill in addr family */
if ( bind(sock_id, (struct sockaddr *)&saddr, sizeof(saddr)) != 0 )
oops( "bind" );
/*
* Step 3: allow incoming calls with Qsize=1 on socket
*/
if ( listen(sock_id, 1) != 0 )
oops( "listen" );
/*
* main loop: accept(), write(), close()
*/
while ( 1 ){
sock_fd = accept(sock_id, NULL, NULL); /* wait for call */
printf("Wow! got a call!\n");
if ( sock_fd == -1 )
oops( "accept" ); /* error getting calls */
memset( filename, 0, 256);
read( sock_fd, filename, 256 );
printf( "filename:%s\n", filename);
/* send data to clint */
if( (fd = open( filename, O_RDONLY )) == -1 ){
if( dup2( sock_fd, 2 ) == -1 ) /* redirect stderr to sock_fd */
oops("dup2");
perror("");
tty = ttyname(1);
close(2);
if( open( tty, O_RDONLY ) == -1 ){
printf("recover stderr error\n");
exit(1);
}
close(sock_fd); /*release the connection between target server and clint */
continue;
}
/* if no error happen */
write( sock_fd, "ok\0", 3 );
while( (nchar = read( fd, buf, BUFSIZ )) > 0 ){
if( write( sock_fd, buf, nchar ) != nchar )
oops("write");
}
printf("server finished\n");
close(sock_fd); /*release the connection between target server and clint */
close(fd);
}
}
/* clint.c - a client
* usage: clint servername port file path_to_save
* build: cc clint.c -o clint
*/
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#define oops(msg) { perror(msg); exit(1); }
main(int ac, char *av[])
{
struct sockaddr_in servadd; /* the number to call */
struct hostent *hp; /* used to get number */
int sock_id, sock_fd; /* the socket and fd */
char buf[BUFSIZ]; /* to receive message */
char info[BUFSIZ];
int messlen; /* for message length */
int fd;
char *filename;
int nchar;
if( ac != 5 )
oops("usage:clint servername port file path_to_save");
if( strlen(av[3]) >256 || strlen(av[4]) >256 ){
printf("error:file name is too long\n");
exit(1);
}
/*
* Step 1: Get a socket
*/
sock_id = socket( AF_INET, SOCK_STREAM, 0 ); /* get a line */
if ( sock_id == -1 )
oops( "socket" ); /* or fail */
/*
* Step 2: connect to server
* need to build address (host,port) of server first
*/
bzero( &servadd, sizeof( servadd ) ); /* zero the address */
hp = gethostbyname( av[1] ); /* lookup host's ip # */
if (hp == NULL)
oops(av[1]); /* or die */
bcopy(hp->h_addr, (struct sockaddr *)&servadd.sin_addr, hp->h_length);
servadd.sin_port = htons(atoi(av[2])); /* fill in port number */
servadd.sin_family = AF_INET ; /* fill in socket type */
/* now dial */
if ( connect(sock_id,(struct sockaddr *)&servadd, sizeof(servadd)) !=0)
oops( "connect" );
/*
* Step 3: transfer data
*/
filename = av[3];
if( write( sock_id, filename,strlen(filename) +1) != strlen( filename ) + 1) /* note:'\0' */
oops("send file request fail");
read( sock_id, info, BUFSIZ );
if( strcmp( info, "ok") != 0 ){
printf("%s\n",info );
close(sock_id );
exit(1);
}
if( (fd = open( av[4],O_WRONLY|O_CREAT, 0755 )) == -1 )
oops("open filename" );
while( ( nchar = read(sock_id, buf, BUFSIZ)) > 0 ){ /* read stuff */
if( write( fd, buf, nchar )!= nchar )
oops("write");
}
printf("done\n");
close( sock_id );
close( fd );
}
/* server */
write(server_sock, buffer1, len_buffer1 );
write( server_sock, buffer2, len_buffer2);