Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188614
  • 博文数量: 45
  • 博客积分: 1657
  • 博客等级: 上尉
  • 技术积分: 765
  • 用 户 组: 普通用户
  • 注册时间: 2007-06-13 12:42
文章分类

全部博文(45)

文章存档

2012年(1)

2011年(4)

2010年(6)

2009年(3)

2008年(31)

分类: IT职场

2008-08-25 20:48:50

  其实是刘工给我们留的作业。

  我写的是server端,按照很传统的思路实现,使用tcp协议监听连接,杨朔写client。我们约定的协议非常简单,即最初的数据是这样的:

|文件名长|文件名|文件长|(紧跟文件)数据。由于文件最长不过255,所以我们规定文件名长1个字节,文件名x个字节,根据文件名长,文件长4个字节。不包括'|'符号。我们写完成程序,做了不到三次小的改动,就成功了。测试过程中我这边使用1k的缓冲,杨朔使用8k缓冲,传输速度达到最快大约9.5mb/s吧。不是特别满足,最好下个周能实现1传多个文件2多线程传送3传送文件夹。


以下是server.c,client.c在杨朔那,明天要过来再发上来。



/* 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() {
    
}

阅读(727) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~