Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1682971
  • 博文数量: 311
  • 博客积分: 7778
  • 博客等级: 少将
  • 技术积分: 4186
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-09 19:59
个人简介

蓝点工坊(http://www.bluedrum.cn) 创始人,App和嵌入式产品开发。同时也做相应培训和外包工作。 详细介绍 http://pan.baidu.com/s/1y2g88

文章存档

2012年(3)

2011年(115)

2010年(170)

2009年(23)

分类: 系统运维

2010-10-13 01:22:12


服务器代码


/*
  UDP 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 = 9000;
   
     char addr_buf[1024];
      char buf[1024];
      int len;


    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]);

 


   /* 第一步:创建UDP SOCKET*/
    listen_fd = socket(PF_INET6,SOCK_DGRAM,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);
             
         }

     

       fprintf(stdout,"UDP ipv6 echo server ,listen on %d,fd%d\n",port,listen_fd);

     /* 第四步:开始用等待客户端的联接 */
      while(1)
          {
             
                  memset((void *)&client_addr,0,sizeof(client_addr));
                 sockaddr_len = sizeof(struct sockaddr_in6);
               len = recvfrom(listen_fd,buf,sizeof(buf),0,(struct sockaddr * )&client_addr,&sockaddr_len);
          
 fprintf(stdout,"client connect: ip %s,port %d,len=%d\n",inet_ntop(AF_INET6, (void *)&(client_addr.sin6_addr),addr_buf, sockaddr_len),ntohs(client_addr.sin6_port),len);

      fprintf(stdout,"client buffer=%s\n",buf);
                
             
                     if(len <=0)
                           continue;

                      if(strncmp("exit",buf,4)==0)
                            break;

                     sendto(listen_fd,buf,len,0,(struct sockaddr * )&client_addr,sockaddr_len);


           }

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


客户端代码

/*
  UDP 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 udp_fd = -1;

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

int main(int argc,char * argv[])
{
   unsigned short port = 9000;
    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);



    /* 第一步:创建UDP SOCKET*/
    udp_fd = socket(PF_INET6,SOCK_DGRAM,0);
     if(udp_fd == -1)
       {
          perror("socket");
       }

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

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

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

     inet_pton(AF_INET6, ip, (void *)&(server_addr.sin6_addr));

    fprintf(stdout,"Remote UDP echo server %s:%d\n",ip,port);

    

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


            len = strlen(buffer)+1;

           if((len = sendto(udp_fd,buffer,len,0,(struct sockaddr *)&server_addr,sockaddr_len))<= 0)
               {
                  perror("send");
                     continue;
               }
                
           len = recvfrom(udp_fd,buffer,len,0,(struct sockaddr *)&server_addr,&sockaddr_len);
              if(len <= 0)
                   {
                      perror("recv");
                          continue;
                   }
             fprintf(stdout,"server > %s\n",buffer);

             
          }
 } /* main */
    
    


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