/* server.c:
*
* Example daytime server :
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <netdb.h>
#include <fcntl.h>
#define len 1024
char filename[256];
int filesize ;
int namesize ;
char *ptr;
/*
* This function reports the error and
* exits back to the shell :
*/
static void
bail(const char *on_what) {
if ( errno != 0 ) {
fputs(strerror(errno),stderr);
fputs(": ",stderr);
}
fputs(on_what,stderr);
fputc('\n',stderr);
exit(1);
}
int resolution(char *);
int
main(int argc,char **argv) {
int z;
char *srvr_addr = NULL;
char *srvr_port = "9090";
struct sockaddr_in adr_srvr;/* AF_INET */
struct sockaddr_in adr_clnt;/* AF_INET */
int len_inet; /* length */
int s; /* Socket */
int c; /* Client socket */
int n; /* bytes */
//time_t td; /* Current date&time */
char buf[len];
int fd;
/*
* Use a server address from the command
* line, if one has been provided.
* Otherwise, this program will default
* to using the arbitrary address
* 127.0.0.1 :
*/
if ( argc >= 2 ) {
/* Addr on cmdline: */
//srvr_addr = argv[1];
srvr_addr = "192.1.1.179";
memset(filename, 0, 256);
memcpy(filename, argv[1], strlen(argv[1]));
} else {
/* Use default address: */
srvr_addr = "192.1.1.179";
memset(filename, 0, 256);
strcpy(filename, "whose");
}
/*
* If there is a second argument on the
* command line, use it as the port # :
*/
if ( argc >= 3 )
srvr_port = argv[2];
/*
* Create a TDP/IP socket to use :
*/
s = socket(PF_INET,SOCK_STREAM,0);
if ( s == -1 )
bail("socket()");
/*
* Create a server socket address:
*/
memset(&adr_srvr,0,sizeof adr_srvr);
adr_srvr.sin_family = AF_INET;
adr_srvr.sin_port = htons(atoi(srvr_port));
if ( strcmp(srvr_addr,"*") != 0 ) {
/* Normal Address */
adr_srvr.sin_addr.s_addr =
inet_addr(srvr_addr);
if ( adr_srvr.sin_addr.s_addr
== INADDR_NONE )
bail("bad address.");
} else {
/* Wild Address */
adr_srvr.sin_addr.s_addr =
INADDR_ANY;
}
/*
* Bind the server address:
*/
len_inet = sizeof adr_srvr;
z = bind(s,(struct sockaddr *)&adr_srvr,
len_inet);
if ( z == -1 )
bail("bind(2)");
/*
* Make it a listening socket:
*/
z = listen(s,10);
if ( z == -1 )
bail("listen(2)");
/*
* Start the server loop :
*/
for (;;) {
/*
* Wait for a connect :
*/
len_inet = sizeof adr_clnt;
c = accept(s,
(struct sockaddr *)&adr_clnt,
&len_inet);
if ( c == -1 )
bail("accept(2)");
/* first read from this socket ,which contains file infomation */
if ((n = read(c, buf, len)) != 0) {
resolution(buf);
ptr = buf;
ptr = ptr + 5;
ptr = ptr + namesize;
}
/**
*file to save data
*/
strcat(filename, "def.srvr");
if ((fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR)) == -1) {
bail("open");
} else {
write(fd, ptr, (n - 6) - namesize);
}
/*
* Generate a time stamp :
*/
/**
time(&td);
n = (int) strftime(dtbuf,sizeof dtbuf,
"%A %b %d %H:%M:%S %Y\n",
localtime(&td));
**/
/*
* Write result back to the client :
*/
/*
z = write(c,dtbuf,n);
if ( z == -1 )
bail("write(2)");
*/
while ((z = read(c, buf, len - 1)) != 0) { //read data from socket
//printf("==z:\t%d================\n", z);
//buf[z] = 0; //set the last byte 0
//printf("%s", buf);
if ((n = write(fd, buf, z)) == -1){
bail("write");
}
//fflush(stdout);
}
/*
* Close this client's connection:
*/
close(c);
close(fd);
}
/* Control never gets here */
return 0;
}
/**
*
* server
* resolution the first from client
* namesize|filename|filesize
* 1 x 4
*/
int resolution(char *first_line) {
/* the follow 1 line to coodinate with our protocol */
first_line = first_line + 1;
namesize = 0;
filesize = 0;
/*get namesize from the first byte */
namesize = *first_line;
/* get filename from first line */
memset(filename, 0, 256);
memcpy(filename, first_line + 1, namesize);
/* get filesize */
*((char *)&filesize) = *(first_line + 1 + namesize);
*(1 + (char *)&filesize) = *(first_line + 1 + namesize + 1);
*(2 + (char *)&filesize) = *(first_line + 1 + namesize + 2);
*(3 + (char *)&filesize) = *(first_line + 1 + namesize + 3);
//printf("filesize:\t%d\n", filesize);
return 0;
}
/**/
void set_filename() {
}
|