linux-c-tcpサービス帯域タイムアウト処理のc/s


1.server
deepfuture@deepfuture-laptop:~/private/mytest$ ./testtcps
server wait....
............................
server read :deepfuture.iteye.com
 
server send :hello
.....
server read :deepfuture.iteye.com
 
server send :hello
2.client
deepfuture@deepfuture-laptop:~/private/mytest$ ./testtcpc
client send....
client send :deepfuture.iteye.com
client read :hello
deepfuture@deepfuture-laptop:~/private/mytest$ ./testtcpc
client send....
client send :deepfuture.iteye.com
client read :hello
deepfuture@deepfuture-laptop:~/private/mytest$ 
3.source
1)server
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
//deepfuture.iteye.com
ssize_t readn(int fd,void *ptr,size_t maxcn){//  n   ,maxc      
    size_t noreadcn,readcn;
    char *buf=ptr;

    
    
    noreadcn=maxcn;
    while(noreadcn>0){
       if ( (readcn=read(fd,buf,noreadcn))<0){//   
    
          if (errno==EINTR) {//     ,       
             perror("    ");
             readcn=0;           
          }
          else {return -1;}//      ,      
       }
       else if(readcn==0) break;//EOF
       
       
       noreadcn-=readcn;//    ,            maxc,    ,                     
       buf+=readcn;  
       if (*buf==0) break;//              ,     ,      
       }  


     
    return (maxcn-noreadcn);
}

ssize_t  writen(int fd,void *ptr,size_t maxcn){//  n   
    size_t nowritecn,writecn;
    char *buf=ptr;

    
    nowritecn=maxcn;
    while(nowritecn>0){
       if((writecn=write(fd,buf,nowritecn))<=0){//   
       
          if (errno==EINTR) {//    ,       
             perror("    ");
             writecn=0;             
          }
          else {return -1;}//      ,      
       }

      
       nowritecn-=writecn;
       buf+=writecn;  

       } 

       return (maxcn-nowritecn);
}

int main(void){
    int fd;
    int addresslen;
    struct sockaddr_in address;//      
    int pid;
    int rc;
    fd_set fdset;
    


                  
    //  socket
    fd=socket(AF_INET,SOCK_STREAM,0);//fd socket
    if (fd==-1){//  ,   errno  
        perror("error");//perror     ,  ":"   ,   errno        (      ),        。        
    }
    
    //bind  socket fd    
    address.sin_family=AF_INET;//IPV4  ,AF_INET6 IPV6
    address.sin_addr.s_addr=htonl(INADDR_ANY);//l  32 ,htonl      CPU      
    address.sin_port=htons(1253);//   ,s  16 
    addresslen=sizeof(address);
    

    bind(fd,(struct sockaddr *)&address,addresslen);//bind
    
           //  socket  ,          
           rc=listen(fd,32);//    32   ,    
           //int listen(int sockfd, int backlog)  :0──  , -1──  
           //                                                 
           if (rc==-1) {
              perror("listen error");//    
              exit(1);
           }
           printf("server wait....
"); while(1){ struct sockaddr_in clientaddress; int address_len; int client_sockfd; char mybuf[100]; char *buf="hello
"; struct timeval timeout;// // 2 timeout.tv_sec=1; timeout.tv_usec=0; // fdset FD_ZERO(&fdset);// fdset FD_CLR(fd,&fdset);// fd FD_SET(fd,&fdset);// //select if ((select(fd+1,&fdset,NULL,NULL,&timeout))<0){ perror("select error"); fflush(stdout); } // , fflush(stdout); address_len=sizeof(clientaddress); if(FD_ISSET(fd,&fdset)){ // client_sockfd=accept(fd,(struct sockaddr *)&clientaddress,&address_len);//client_sockfd , read write 。client_address deepfuture.iteye.com //fork pid=fork(); if (pid<0){// printf("error:%s
",strerror(errno));//strerror errno deepfuture.iteye.com close(client_sockfd); exit(1); } if (pid==0){ // close(fd);// // deepfuture.iteye.com bzero(mybuf,100); readn(client_sockfd,(void *)mybuf,100); printf("
server read :%s",mybuf); // writen(client_sockfd,(void *)buf,strlen(buf)+1); printf("
server send :%s",buf); close(client_sockfd); exit(0); } else {// close(client_sockfd);// , , , client_sockfd deepfuture.iteye.com } }else{ printf("."); fflush(stdout); } } }

 2) client
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
//deepfuture.iteye.com
ssize_t readn(int fd,void *ptr,size_t maxcn){//  n   ,maxc      
    size_t noreadcn,readcn;
    char *buf=ptr;
    
    
    noreadcn=maxcn;
    while(noreadcn>0){
       if ( (readcn=read(fd,buf,noreadcn))<0){//   
    
          if (errno==EINTR) {//     ,        deepfuture.iteye.com
             perror("    ");
             readcn=0;            
          }
          else {return -1;}//      ,      
       }
       else if(readcn==0) break;//EOF deepfuture.iteye.com
           
       noreadcn-=readcn;//    ,            maxc,    ,                     
       buf+=readcn;   
        if (*buf==0) break;    //              ,     ,        deepfuture.iteye.com
       }   

    return (maxcn-noreadcn);
}

ssize_t writen(int fd,void *ptr,size_t maxcn){//  n   
    size_t nowritecn,writecn;
    char *buf=ptr;
    
    nowritecn=maxcn;
    while(nowritecn>0){
       if((writecn=write(fd,buf,nowritecn))<=0){//   
          if (errno==EINTR) {//    ,       
             perror("    ");
             writecn=0;             
          }
          else {return -1;}//      ,      
       }
    
       nowritecn-=writecn;
       buf+=writecn; 

       }  
       return (maxcn-nowritecn);
}

int main(void){
    int fd;
    int addresslen;
    struct sockaddr_in address;//       deepfuture.iteye.com
    int pid;
    char mybuf[100];        
    char *buf="deepfuture.iteye.com
"; int rc; fd=socket(AF_INET,SOCK_STREAM,0);// socket if (fd==-1){// , errno perror("error");//perror , ":" , errno ( ), 。 deepfuture.iteye.com } printf("client send....
"); fflush(stdout); // address.sin_family=AF_INET;//IPV4 ,AF_INET6 IPV6 deepfuture.iteye.com address.sin_addr.s_addr=inet_addr("127.0.0.1");//l 32 ,htonl CPU address.sin_port=htons(1253);// ,s 16 deepfuture.iteye.com addresslen=sizeof(address); rc=connect(fd,(struct sockaddr *)&address,addresslen);// deepfuture.iteye.com if (rc==-1){//rc=0 ,rc=-1 deepfuture.iteye.com perror(" "); exit(1); } // writen(fd,(void *)buf,strlen(buf)+1); printf("client send :%s
",buf); // bzero(mybuf,100); readn(fd,(void *)mybuf,100); printf("client read :%s
",mybuf); close(fd); exit(0); }