• chap 1:time server


    client:

    #include <stdio.h>

    #include "unp.h"

    int main(int argc,char** argv)

    {

           int sockfd,n;

           char recvline[MAXLINE+1];

           struct sockaddr_in servaddr;

           if(argc !=2)

           { 

                  printf("usage: a.out <IP address>\n");

                  exit(0);

          }

         if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)

          {

               printf("socket erro\n"); 

               exit(0);

           } 

           bzero(&servaddr,sizeof(servaddr)); 

           servaddr.sin_family = AF_INET; 

           servaddr.sin_port =htons(3000);

           if(inet_pton(AF_INET, argv[1],&servaddr.sin_addr)<=0)

           { 

              printf("inet_pton error for %s\n", argv[1]);

              exit(0);

            }

          if(connect(sockfd,(SA *)&servaddr,sizeof(servaddr))<0)

          { 

              printf("sockfd = %d\n", sockfd); 

              printf("addr = %s\n", argv[1]); 

              printf("connect erro\n");

              exit(0);

          }

        while((n = read(sockfd, recvline, MAXLINE))>0)

        {

              recvline[n]=0; 

              printf("read length = %d\n", n);

              if(fputs(recvline,stdout)==EOF) 

                   printf("fputs error");

         }

         if(n <0) 

       printf("read error\n"); 

        exit(0);

    }

    server:

    #include "unp.h"

    #include <time.h>

    int   main(int argc,char** argv)

    {

         int listenfd, connfd;

         struct sockaddr_in servaddr;

         char buffer[MAXLINE]; 

         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(3000); 

         printf("linstenfd = %d\n", listenfd); 

         Bind(listenfd,(SA *)&servaddr,sizeof(servaddr)); 

         Listen(listenfd, LISTENQ); 

         printf("listening...\n");

         for(;;)

         { 

               connfd = Accept(listenfd,(SA *)NULL,NULL); 

               printf("accept...\n"); ticks=time(NULL); 

               snprintf(buffer,sizeof(buffer),"%.24s\r\n", ctime(&ticks)); 

               printf("time = %s", buffer); 

               Write(connfd, buffer, strlen(buffer)); 

                Close(connfd);

           }

    }

    compile on ubuntu 10.10

    use some lib file from the resources dowload from the author's website, need change something....


  • 相关阅读:
    python课堂练习
    Python爬虫学习笔记(九)——Selenium的使用
    Python爬虫学习笔记(八)——智高考数据爬取
    Python爬虫学习笔记(七)——Ajax
    Python爬虫学习笔记(六)——BeautifulSoup和pyquery的使用
    Python爬虫学习笔记(五)——XPath的使用
    Python爬虫学习笔记(四)——猫眼电影Top100
    Python爬虫学习笔记(三)——正则表达式
    Python爬虫学习笔记(二)——requests库的使用
    Python爬虫学习笔记(一)——urllib库的使用
  • 原文地址:https://www.cnblogs.com/macula7/p/1960373.html
Copyright © 2020-2023  润新知