zoukankan      html  css  js  c++  java
  • 《UNIX网络编程》 1

    1、安装 daytime 服务:

    apt-get install xinetd

    chmod 777 /etc/xinetd.d/daytime

    vim /etc/xinetd.d/daytime

    把两个 disable = yes ,改成 disable = no

    /etc/init.d/xinetd restart

    2、timeserver.cpp:

    #include <time.h>
    #include <netinet/in.h>
    #include <string.h>
    #include <stdio.h>
    #include <unistd.h>
    
    #define LISTENQ 1024
    int main(int argc, char** argv)
    {
        int listenfd,connfd;
        sockaddr_in servaddr;
        char buff[1024];
        time_t ticks;
    
        listenfd = socket(AF_INET,SOCK_STREAM,0);
        bzero(&servaddr,sizeof(servaddr));
        servaddr.sin_family = AF_INET;
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
        servaddr.sin_port = htons(2013);
    
        bind(listenfd,(sockaddr*)&servaddr,sizeof(servaddr));
        listen(listenfd,LISTENQ);
        for(;;)
        {   
            connfd = accept(listenfd,(sockaddr*)NULL,NULL);
            ticks = time(NULL);
            snprintf(buff,sizeof(buff),"%.24s\r\n",ctime(&ticks));
            write(connfd,buff,strlen(buff));    //#include <unistd.h>
            close(connfd);
        }   
    }

    3、timeclient.cpp:

    #include <iostream>
    #include <netinet/in.h>
    #include <string.h>
    #include <arpa/inet.h>
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        int     sockfd,n;
        char    recvline[1024];
        sockaddr_in servaddr;       //#include <netinet/in.h>
    
        if(argc != 2)
        {   
            std::cout<<"usage: a.out <IP>"<<std::endl;
            return -1; 
        }   
        if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)    //#include <sys/socket.h>
        {   
            std::cout<<"socket error."<<std::endl;
            return -1; 
        }   
        bzero(&servaddr, sizeof(servaddr)); //#include <cstring>
        servaddr.sin_family = AF_INET;
        servaddr.sin_port = htons(2013);
        if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr) <= 0)  // #include <arpa/inet.h>
        {   
            std::cout<<"inet_pton error"<<std::endl;
            return -1; 
        }   
        if(connect(sockfd, (sockaddr*)&servaddr,sizeof(servaddr)) < 0)  
        {   
            std::cout<<"connect error"<<std::endl;
            return -1; 
        }   
        while( (n = read(sockfd, recvline, 1023)) > 0)
        {   
            recvline[n] = 0;
            if(fputs(recvline, stdout) == EOF)  //#include <stdio.h>
            {   
                std::cout<<"fputs error"<<std::endl;
            }   
        }   
        if(n < 0)
        {   
            std::cout<<"read error"<<std::endl;
        }  
    }
  • 相关阅读:
    Kappa Architecture: A Different Way to Process Data
    Lambda architecture and Kappa architecture
    Questioning the lambda architecure
    Lambda Architecture: Achieving Velocity and Volume with Big Data
    beego 参数配置
    hadoop 3.1.1 安装
    Idea 切换git账号
    IntelliJ IDEA 开发git多模块项目
    打印1到1亿的平方
    IDEA 逆向工程
  • 原文地址:https://www.cnblogs.com/tianyajuanke/p/3096991.html
Copyright © 2011-2022 走看看