Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207420
  • 博文数量: 20
  • 博客积分: 1380
  • 博客等级: 中尉
  • 技术积分: 280
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-28 14:52
文章分类
文章存档

2017年(2)

2014年(3)

2013年(1)

2009年(1)

2008年(1)

2007年(12)

我的朋友

分类: C/C++

2007-10-17 12:52:59

   第一篇
   A simple Daytime Client
   #include "unp.h"
   int main(int argc, char **argv)
   {
       int sockfd, n;
       char recvline[MAXLINE +1 ];
       struct sockaddr_in servaddr;
      
       if (argc != 2)
           err_quit("usage: a.out ");
       /*Create an Internet(AF_INET) stream(SOCK_STREAM) socket */
       if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
           err_sys("socket error");
       /*Fill in  an  Internet  socket  address  structure with 
         the server's IP address and port number.
         The IP address and port number fields in the structure
         must be in specific formats:we call the library function
         "htons" and "inet_pton" to convert into the proper format*/
       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)
           err_quit("inet_pton error for %s", argv[1]);
 
       /*Connect with serve*/
       if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) <0)
           err_sys("connect error");
       /*Read and display serve's reply*/
       while((n=read(sockfd, recvline, MAXLINE)) >0) {
           recvline[n] = 0;
           if (fputs(recvline, stdout) == EOF)
               err_sys("fputs error");
       }
       if (n<0)
           err_sys("read error");
 
       exit(0);
   }
  
  A simple Daytime Server
 #include "unp.h"
  #include
  int main(int argc, char **argv)
  {
      int listenfd, connfd;
      struct sockaddr_in servaddr;
      char buff[MAXLINE];
      time_t ticks;
 
      /*Create a TCP socket*/

      listenfd = Socket(AF_INET, SOCK_SEREAM, 0);

      /*The serve's well-known port is bound to the socket

        by filling in an Internet socket address structure

        and calling "bind".We specify the IP address as

        INADDR_ANY,which allows the server to accept a client

        connection on any interface, in case the server host

        has multiple interfaces;*/

      bzero(&servaddr, sizeof(servaddr));

      servaddr.sin_family = AF_INET;

      servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

      servaddr.sin_port = htons(13);

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

      /*By calling listen the socket is converted into a listening

        socket ,on which incoming connections from clients will be

        accepted by  the kernel.The contant LISTENQ is from our

        unp.h header. It specifies the maximum number of client

        connections that the kernel will queue for this listening

        descriptor.*/

      Listen(listenfd, LISTENQ);

      /*The server process is put to sleep in the call to "accept",

        waiting for a client connection to arrive and be accepted.*/

      for(;;) {

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

          ticks = time(NULL);

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

          Write(connfd, buff, strlen(buff));

          Close(connfd);

      }

   }

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