Linux下IP v6 tcp客户端源码示例

/***********************************
*
*client.c
*
*
**********************************/
   
#include<stdio.h>  
#include<stdlib.h>  
#include<errno.h>  
#include<string.h>  
#include<sys/types.h>  
#include<netinet/in.h>  
#include<sys/socket.h>  
#include<sys/wait.h>  
#include<unistd.h>  
#include<arpa/inet.h>  
#include<resolv.h>  
   
#define MAXSIZE 1024  
//**************main()**************  
int main(int argc,char *argv[])  
{  
       int sockfd;  
       int n;  
       char recv_buf[MAXSIZE+1];  
       char send_buf[MAXSIZE+1];  
       struct timeval tv;  
       fd_set readfd;  
         
       // 参数判断  
       if ( argc != 3 )  
       {  
              printf("USAGE: %s [serv_address][serv_port]\n", argv[0]);  
              exit(1);  
       }  
         
       // 创建IPV6型的socket  
       if ( (sockfd = socket(PF_INET6, SOCK_STREAM, 0)) < 0 )  
       {  
              perror("socket");  
              exit(1);  
       }  
         
       // 转换服务器IP地址  
       struct sockaddr_in6 serv_addr;  
       bzero(&serv_addr,sizeof(serv_addr));  
       serv_addr.sin6_family = AF_INET6;  
       serv_addr.sin6_port = htons( atoi(argv[2]) );  
       if ( inet_pton(AF_INET6, argv[1], &serv_addr.sin6_addr ) < 0 )  
       {  
              perror(argv[1]);  
              exit(1);  
       }  
         
       // 与服务器建立连接  
       if ( connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr) ) < 0 )  
       {  
              perror("connect");  
              exit(1);  
       }  
       printf("Connected to server\n");  

while (1)  
       {  
              //----------set last time---------  
              tv.tv_sec=600;  
              tv.tv_usec=0;  
                
              FD_ZERO(&readfd);  
              // 将标准输入和与服务连接的文件句柄读事件加入SET  
              FD_SET(0,&readfd);  
              FD_SET(sockfd,&readfd);  
                
              // 检查是否有读事件发生  
              if ( select( sockfd+1, &readfd, NULL, NULL, &tv) <= 0 )  
              {  
                     fprintf(stderr,"\n time out\n");  
                     break;  
              }  
                
              // 如果标准输入中有数据可读,则从其中读取数据,并向服务器发送  
              if ( FD_ISSET(0,&readfd) )  
              {  
                     if( (n = read(0, send_buf, MAXSIZE-1)) <= 0 )  
                     {  
                            break;  
                     }  
                       
                     send_buf[n]='\n';  
                     if ( send( sockfd, send_buf, n, 0) <= 0)  
                     {  
                            perror("send");  
                            break;  
                     }  
              }  
                
              // 如果有从服务器发送过来的数据,则将其读取出来,并输出到标准输出  
              if ( FD_ISSET(sockfd, &readfd) )  
              {  
                     if ( ( n = recv(sockfd, recv_buf, MAXSIZE-1, 0)) <= 0 )  
                     {  
                            fprintf(stderr,"connection closed.\n");  
                            exit(1);  
                     }  
                       
                     recv_buf[n]='\0';  
                     printf("recved:%s", recv_buf);  
                     fflush(stdout);  
              }  
       }  
       close(sockfd);  
       return 0;  
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwsxgg.html