linux socket编程客户端模型(用getaddrinfo)

#include "unistd.h"
#include "sys/types.h"
#include "sys/socket.h"
#include "netinet/in.h"
#include "arpa/inet.h"
#include "stdio.h"
#include "string.h"
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       void error(int status, int err,char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); if(err) fprintf(stderr,":%s(%d)/n",strerror(err),err); if(status) exit(status); } int client_connect(const char *hostname,const char *service,struct sockaddr_in *server,const char *protocol,int flag){ int sockfd,err; struct addrinfo hints,*res,*r; bzero(&hints,sizeof(struct addrinfo)); hints.ai_family=AF_INET; if(strcmp(protocol,"udp")==0) hints.ai_socktype=SOCK_DGRAM; else if(strcmp(protocol,"tcp")==0) hints.ai_socktype=SOCK_STREAM; err=getaddrinfo(hostname,service,&hints,&res); if(err){ printf("%s/n",gai_strerror(err)); exit(0); } for( r = res; r; r = r->ai_next) { if((sockfd=socket(r->ai_family,r->ai_socktype,r->ai_protocol))<0) continue; if((r->ai_socktype==SOCK_DGRAM)&&(flag==0)) break; if((((r->ai_socktype==SOCK_DGRAM)&&(flag==1))||(r->ai_socktype==SOCK_STREAM))&&(connect(sockfd,r->ai_addr,r->ai_addrlen)==0)) break; close(sockfd); } if(r==NULL) error(1,0,"connect error!!/n"); memcpy(server,(struct sockaddr_in *)r->ai_addr,r->ai_addrlen); freeaddrinfo(res); return sockfd; } //tcpclient.c #include 
      
        #include 
       
         #include 
        
          #include 
         
           #include 
          
            #include 
           
             #include 
            
              #include 
             
               void process_client(int s) { char buff[128]; int rc,maxfd; fd_set rset; FD_ZERO(&rset); while(1) { FD_SET(s,&rset); FD_SET(0,&rset); maxfd=s+1; if(select(maxfd,&rset,NULL,NULL,NULL)==-1) error(1,0,"select error!"); if(FD_ISSET(s,&rset)) { rc = recv(s,buff,sizeof(buff),0); if(rc <= 0) break; buff[rc]='/0'; write(1,buff,rc); } if(FD_ISSET(0,&rset)) { if(fgets(buff,sizeof(buff),stdin)==NULL) shutdown(s,SHUT_WR); send(s,buff,strlen(buff),0); } } /* while(fgets(buff,sizeof(buff),stdin)!=NULL) { send(s,buff,strlen(buff),0); rc = recv(s,buff,sizeof(buff),0); if(rc <= 0) break; buff[rc]='/0'; write(1,buff,rc); bzero(buff,sizeof(buff)); } */ } int main(int argc, char **argv) { struct sockaddr_in server,temp; char *hname, *sname; int s, c, len; if(argc < 2) error(1,0,"parameters less than 2/n"); else if(argc == 2) { hname = NULL; sname = argv[1]; } else if(argc == 3) { hname = argv[1]; sname = argv[2]; } s = client_connect(hname, sname, &server, "tcp",0); len=sizeof(temp); getsockname(s,(struct sockaddr *)&temp,&len); printf("local %s:%d/n",inet_ntoa(temp.sin_addr),ntohs(temp.sin_port)); getpeername(s,(struct sockaddr *)&temp,&len); printf("remote %s:%d/n",inet_ntoa(temp.sin_addr),ntohs(temp.sin_port)); process_client(s); close(s); exit(0); } 
             
            
           
          
         
        
       
      
     
    
   
  

你可能感兴趣的:(编程,linux,socket,struct,server,null)