zoukankan      html  css  js  c++  java
  • UNIX网络编程daytime服务端和客户端的实现过程

    Server.c

    #include "unp.h"
    //solid for server
    #include <time.h>

    int main(int argc,char **argv)
    {
    int listenfd,connfd;
    struct sockaddr_in servaddr;
    //header file contain the struct sockaddr_in
    /*
    struct sockaddr_in{
    short sin_family; //2bytes e.g AF_INET AF_INET6
    unsigned short sin_port;2bytes for short
    struct in_addr sin_addr;
    char sin_zero[8]; // 8bytes zero this if you want to
    };

    struct in_addr{
    unsigned long s_addr;//4 bytes load with inet_pton()
    }
    */
    char buff[MAXLINE];

    time_t ticks;

    listenfd = socket(AF_INET,SOCK_STREAM,0);
    //listenfd is token as a desicribale symbol
    //SOCK_STREAM used for reliable communications
    //third parameter is the implicitly 0 based on what you choose the previous two parameters or explicitly IPPROTO_TCP/IPPROTO_UDP
    bzero(&servaddr,sizeof(servaddr));
    // set the n bits to zero in the memory

    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
    //host to network long conversion to the net long from host long 32 bits data
    //listen to the any port and receive the default port.0.0.0.0
    //daytime 13 reserved port for the client requested for the daytime
    servaddr.sin_port = htonl(13);
    /*daytime server*/
    //bind listenfd with the servaddr
    for ( ; ; )
    {
    connfd = accept(listenfd,(SA*)NULL,NULL);
    ticks = time(NULL);
    snprintf(buff,sizeof(buff),"%.24s ",ctime(&ticks));
    //convert the parameters to the string with the format "%.24s "
    //char* ctime(const time_t *time);----->gelinweizhi standard time
    write(connfd,buff,strlen(buff));
    //write(int fd,const void*buf,size_t count)--->fd need to describe the key words/buf----->the buffer/count---->the number of the maxium
    close(connfd);
    }
    }

    这个是客户端代码,和注释,Ubuntu下sublime 的中文注释配起来好烦,干脆练习了英语,哈哈哈哈哈!

    这个是服务端代码。

    //echo server
    #include "unp.h"
    #include "my_err.h"

    int main(int argc,char ** argv)
    //argc 是 argv[]的参数
    {
    int sockfd,n;
    //?scheme for what
    char recvline[MAXLINE+1];
    //MAXLINE is a #define?
    struct sockaddr_in servaddr;
    if (argc!=2)
    err_quit("usage:a.out<IP address>");
    if((sockfd = socket(AF_INET,SOCK_STREAM,0))<0)
    err_sys("socket error");
    bzero(&servaddr,sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(13);

    if( inet_pton(AF_INET,argv[1], &servaddr.sin_addr) <= 0 )//inet_pton(addr,点分十进制,二进制整数)
    err_quit("inet_pton error for %s",argv[1]);
    if(connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) < 0 )
    err_sys("connect error");
    while((n = read(sockfd,recvline,MAXLINE)) > 0 )
    {
    recvline[n] = 0; //fputS()没有自动写入字符串结束'',补上recvline[n]=''因为0转换为char
    if(fputs(recvline,stdout) == EOF)//EOF==-1
    err_sys("fputs error");
    }
    if(n<0)
    err_sys("read error");
    exit(0);
    }

    配置时,注意如下引入这个头文件,下载unpv13e这个文件夹并且按步骤配置好,网上搜教程

    #include <errno.h> /* for definition of errno */
    #include <stdarg.h> /* ISO C variable aruments */

    static void err_doit(int, int, const char *, va_list);

    /*
    * Nonfatal error related to a system call.
    * Print a message and return.
    */
    void
    err_ret(const char *fmt, ...)
    {
    va_list ap;

    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    }


    /*
    * Fatal error related to a system call.
    * Print a message and terminate.
    */
    void
    err_sys(const char *fmt, ...)
    {
    va_list ap;

    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    exit(1);
    }


    /*
    * Fatal error unrelated to a system call.
    * Error code passed as explict parameter.
    * Print a message and terminate.
    */
    void
    err_exit(int error, const char *fmt, ...)
    {
    va_list ap;

    va_start(ap, fmt);
    err_doit(1, error, fmt, ap);
    va_end(ap);
    exit(1);
    }


    /*
    * Fatal error related to a system call.
    * Print a message, dump core, and terminate.
    */
    void
    err_dump(const char *fmt, ...)
    {
    va_list ap;

    va_start(ap, fmt);
    err_doit(1, errno, fmt, ap);
    va_end(ap);
    abort(); /* dump core and terminate */
    exit(1); /* shouldn't get here */
    }


    /*
    * Nonfatal error unrelated to a system call.
    * Print a message and return.
    */
    void
    err_msg(const char *fmt, ...)
    {
    va_list ap;

    va_start(ap, fmt);
    err_doit(0, 0, fmt, ap);
    va_end(ap);
    }


    /*
    * Fatal error unrelated to a system call.
    * Print a message and terminate.
    */
    void
    err_quit(const char *fmt, ...)
    {
    va_list ap;

    va_start(ap, fmt);
    err_doit(0, 0, fmt, ap);
    va_end(ap);
    exit(1);
    }


    /*
    * Print a message and return to caller.
    * Caller specifies "errnoflag".
    */
    static void
    err_doit(int errnoflag, int error, const char *fmt, va_list ap)
    {
    char buf[MAXLINE];
    vsnprintf(buf, MAXLINE, fmt, ap);
    if (errnoflag)
    snprintf(buf+strlen(buf), MAXLINE-strlen(buf), ": %s",
    strerror(error));
    strcat(buf, " ");
    fflush(stdout); /* in case stdout and stderr are the same */
    fputs(buf, stderr);
    fflush(NULL); /* flushes all stdio output streams */
    }

  • 相关阅读:
    微信jssdk
    php读取大文件
    PHP工程师突破
    TCP/IP
    基于scrapy-redis的分布式爬虫
    pymongodb的使用和一个腾讯招聘爬取的案例
    中间件使用之(UA,IP,selenium)的使用
    windos下redis服务的后台启动
    mongodb的初步使用
    windos下安装mongodb
  • 原文地址:https://www.cnblogs.com/fenglongyu/p/7455871.html
Copyright © 2011-2022 走看看