Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157511
  • 博文数量: 85
  • 博客积分: 366
  • 博客等级: 一等列兵
  • 技术积分: 455
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-16 18:32
个人简介

闲下来的时候看看书

文章分类

全部博文(85)

文章存档

2016年(1)

2013年(2)

2012年(81)

2011年(1)

我的朋友

分类:

2012-09-20 17:04:16



服务器代码

/*
  TCP IPV6 Echo Server
   Author :Andrew Huang

 
 */


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>


static int listen_fd = -1;

void exit_handler(int no)
{
    close(listen_fd);
    exit(0);
}

int main(int argc,char * argv[])
{
    unsigned short port = 10002;

    char buffer[1024];
    int len,i;


    signal(SIGTERM,exit_handler);
    signal(SIGINT,exit_handler);

       /* ipv 6 地址 */
    struct sockaddr_in6 server_addr;
    struct sockaddr_in6 client_addr;
    socklen_t sockaddr_len = sizeof(struct sockaddr_in6);

    if(argc > 1)
        port = (unsigned short)atoi(argv[1]);




    /* 第一步:创建TCP 侦听SOCKET*/
    listen_fd = socket(PF_INET6,SOCK_STREAM,0);
    if(listen_fd == -1)
    {
        perror("socket");
    }


    /* 第二步:设置侦听端口 */
       memset((void *)&server_addr,0,sizeof(server_addr));
        
    server_addr.sin6_family = AF_INET6 ; /* ipv6 */
    server_addr.sin6_port = htons(port);
    server_addr.sin6_addr = in6addr_any;

    if(bind(listen_fd,(struct sockaddr *)&server_addr,sockaddr_len)!=0)
    {
        perror("bind");
        exit_handler(0);

    }

    /* 第三步:通知内核开始侦听 ,这里是非阻塞 */
    if(listen(listen_fd,20)!= 0)
    {
        perror("listen");
        exit_handler(0);
    }

    fprintf(stdout,"TCP IPV6 echo server ,listen on %d\n",port);

    /* 第四步:开始用等待客户端的联接 */
    while(1)
    {
        int new_fd ;
                memset((void *)&client_addr,0,sizeof(client_addr));
                   sockaddr_len = sizeof(client_addr);

        /* 没有客户端联接,将会这里阻塞*/
        new_fd = accept(listen_fd,(struct sockaddr *)&client_addr,&sockaddr_len);
        if(new_fd == -1)
            continue;

              

        fprintf(stdout,"client connect: ip %s,port %d\n",inet_ntop(AF_INET6, (void *)&(client_addr.sin6_addr),buffer, sockaddr_len),ntohs(client_addr.sin6_port));


            for(i = 0;i< 3 ;i++)
               {
        /* 做ECHO操作*/
        len = recv(new_fd,buffer,sizeof(buffer),0);
        if(len <=0)
            continue;

        if(strncmp("exit",buffer,4)!=0)
                 send(new_fd,buffer,len,0);
              }
             close(new_fd);


    }

    fprintf(stdout,"close TCP server\n");
    exit_handler(0);


}


ipv6 TCP客户端

/*
  TCP IPV6 Echo Client
   Author :Andrew Huang

 
 */


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <signal.h>


static int tcp_fd = -1;

void exit_handler(int no)
{
   close(tcp_fd);
    exit(0);
}

int main(int argc,char * argv[])
{
   unsigned short port = 10002;
    unsigned char ip[512] = "::1";
    char buffer[1024];
    ssize_t len;


    signal(SIGTERM,exit_handler);
    signal(SIGINT,exit_handler);

   
    struct sockaddr_in6 server_addr;
    
    socklen_t sockaddr_len = sizeof(struct sockaddr_in6);

   if(argc > 1)
      port = (unsigned short)atoi(argv[1]);

    if(argc > 2)
       strncpy(ip,argv[2],sizeof(ip)-1);



    /* 第一步:创建TCP 侦听SOCKET*/
    tcp_fd = socket(PF_INET6,SOCK_STREAM,0);
     if(tcp_fd == -1)
       {
          perror("socket");
       }

   /* 第二步:可选操作bind,如果没有bind,TCP协议栈自动选一个端口 */

    /* 第三步:联接服务器 */
    memset((void *)&server_addr,0,sizeof(server_addr));

    server_addr.sin6_family = AF_INET6 ; /* ipv4 */
      server_addr.sin6_port = htons(port);

     len =inet_pton(AF_INET6, ip, (void *)&(server_addr.sin6_addr));
    
     printf("len=%d\n",len);/* success = 1 */
    
#if 0
     if(len != 0)
        {
            perror("inet_pton");
              exit_handler(0);
        }
#endif

      fprintf(stdout,"connect TCP echo server %s:%d\n",ip,(int)port);

     if(connect(tcp_fd,(struct sockaddr *)&server_addr,sockaddr_len) != 0)
       {
          perror("connect");
            exit_handler(0);
       }

    while(1)
      {
         if(fgets(buffer,sizeof(buffer)-1,stdin)== NULL)
             {
                perror("fgets");
             continue;
             }
          
          if(strncmp(buffer,"exit",4) == 0)
               {
                 send(tcp_fd,"exit",5,0);
                    exit_handler(0);
                      break;
               }


            len = strlen(buffer)+1;

           if((len = send(tcp_fd,buffer,len,0))<= 0)
               {
                  perror("send");
                     continue;
               }
                
           len = recv(tcp_fd,buffer,len,0);
              if(len <= 0)
                   {
                      perror("recv");
                          continue;
                   }
             fprintf(stdout,"server > %s\n",buffer);

             
          }
 } /* main */
    
    


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