|
| 1 | +#include<sys/ioctl.h> |
| 2 | +#include<fcntl.h> |
| 3 | +#include<time.h> |
| 4 | +#include<sys/time.h> |
| 5 | +#include<sys/types.h> |
| 6 | +#include<sys/socket.h> |
| 7 | +#include<sys/utsname.h> |
| 8 | +#include<sys/select.h> |
| 9 | +#include<netinet/in.h> |
| 10 | +#include<netinet/tcp.h> |
| 11 | +#include<arpa/inet.h> |
| 12 | +#include<stdio.h> |
| 13 | +#include<netdb.h> |
| 14 | +#include<stdlib.h> |
| 15 | +#include<unistd.h> |
| 16 | +#include<string.h> |
| 17 | +#include<errno.h> |
| 18 | +#include<stddef.h> |
| 19 | +#include<assert.h> |
| 20 | + |
| 21 | +#include"sockhub.h" |
| 22 | + |
| 23 | +#defineMAX_CONNECT_ATTEMPTS 10 |
| 24 | + |
| 25 | +typedefstruct |
| 26 | +{ |
| 27 | +ShubMessageHdrhdr; |
| 28 | +intdata; |
| 29 | +}Message; |
| 30 | + |
| 31 | +staticintresolve_host_by_name(constchar*hostname,unsigned*addrs,unsigned*n_addrs) |
| 32 | +{ |
| 33 | +structsockaddr_insin; |
| 34 | +structhostent*hp; |
| 35 | +unsignedi; |
| 36 | + |
| 37 | +sin.sin_addr.s_addr=inet_addr(hostname); |
| 38 | +if (sin.sin_addr.s_addr!=INADDR_NONE) { |
| 39 | +memcpy(&addrs[0],&sin.sin_addr.s_addr,sizeof(sin.sin_addr.s_addr)); |
| 40 | +*n_addrs=1; |
| 41 | +return1; |
| 42 | + } |
| 43 | + |
| 44 | +hp=gethostbyname(hostname); |
| 45 | +if (hp==NULL||hp->h_addrtype!=AF_INET) { |
| 46 | +return0; |
| 47 | + } |
| 48 | +for (i=0;hp->h_addr_list[i]!=NULL&&i<*n_addrs;i++) { |
| 49 | +memcpy(&addrs[i],hp->h_addr_list[i],sizeof(addrs[i])); |
| 50 | + } |
| 51 | +*n_addrs=i; |
| 52 | +return1; |
| 53 | +} |
| 54 | + |
| 55 | +intconnect_to_server(charconst*host,intport,intmax_attempts) |
| 56 | +{ |
| 57 | +structsockaddr_insock_inet; |
| 58 | +unsignedaddrs[128]; |
| 59 | +unsignedi,n_addrs=sizeof(addrs) /sizeof(addrs[0]); |
| 60 | +intrc; |
| 61 | +intsd; |
| 62 | + |
| 63 | +if (strcmp(host,"localhost")==0) { |
| 64 | +structsockaddrsock; |
| 65 | +intlen= offsetof(structsockaddr,sa_data)+sprintf(sock.sa_data,"/tmp/p%u",port); |
| 66 | +sock.sa_family=AF_UNIX; |
| 67 | +sd=socket(sock.sa_family,SOCK_STREAM,0); |
| 68 | +if (sd<0) { |
| 69 | +return-1; |
| 70 | + } |
| 71 | + |
| 72 | +while (1) { |
| 73 | +do { |
| 74 | +rc=connect(sd,&sock,len); |
| 75 | + }while (rc<0&&EINTR); |
| 76 | + |
| 77 | +if (rc<0) { |
| 78 | +if (errno!=ENOENT&&errno!=ECONNREFUSED&&errno!=EINPROGRESS) { |
| 79 | +return-1; |
| 80 | + } |
| 81 | +if (max_attempts--!=0) { |
| 82 | +sleep(1); |
| 83 | + }else { |
| 84 | +return-1; |
| 85 | + } |
| 86 | + }else { |
| 87 | +break; |
| 88 | + } |
| 89 | + } |
| 90 | + }else { |
| 91 | +sock_inet.sin_family=AF_INET; |
| 92 | +sock_inet.sin_port=htons(port); |
| 93 | +if (!resolve_host_by_name(host,addrs,&n_addrs)) { |
| 94 | +return-1; |
| 95 | + } |
| 96 | +sd=socket(AF_INET,SOCK_STREAM,0); |
| 97 | +if (sd<0) { |
| 98 | +return-1; |
| 99 | + } |
| 100 | +while (1) { |
| 101 | +intrc=-1; |
| 102 | +for (i=0;i<n_addrs;++i) { |
| 103 | +memcpy(&sock_inet.sin_addr,&addrs[i],sizeofsock_inet.sin_addr); |
| 104 | +do { |
| 105 | +rc=connect(sd, (structsockaddr*)&sock_inet,sizeof(sock_inet)); |
| 106 | + }while (rc<0&&errno==EINTR); |
| 107 | + |
| 108 | +if (rc >=0||errno==EINPROGRESS) { |
| 109 | +break; |
| 110 | + } |
| 111 | + } |
| 112 | +if (rc<0) { |
| 113 | +if (errno!=ENOENT&&errno!=ECONNREFUSED&&errno!=EINPROGRESS) { |
| 114 | +return-1; |
| 115 | + } |
| 116 | +if (max_attempts--!=0) { |
| 117 | +sleep(1); |
| 118 | + }else { |
| 119 | +return-1; |
| 120 | + } |
| 121 | + }else { |
| 122 | +intoptval=1; |
| 123 | +setsockopt(sd,IPPROTO_TCP,TCP_NODELAY, (charconst*)&optval,sizeof(int)); |
| 124 | +break; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +returnsd; |
| 129 | +} |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +intmain(intargc,char*argv[]) |
| 134 | +{ |
| 135 | +intsd; |
| 136 | +intrc; |
| 137 | +inti; |
| 138 | +intn_iter=10000; |
| 139 | +time_tstart; |
| 140 | + |
| 141 | +if (argc<3) { |
| 142 | +fprintf(stderr,"Usage: ./test-client HOST PORT [N_ITERATIONS]\n"); |
| 143 | +return1; |
| 144 | + } |
| 145 | + |
| 146 | +sd=connect_to_server(argv[1],atoi(argv[2]),MAX_CONNECT_ATTEMPTS); |
| 147 | +if (sd<0) { |
| 148 | +perror("Failed to connect to socket"); |
| 149 | +return1; |
| 150 | + } |
| 151 | + |
| 152 | +if (argc >=3) { |
| 153 | +n_iter=atoi(argv[3]); |
| 154 | + } |
| 155 | + |
| 156 | +start=time(NULL); |
| 157 | + |
| 158 | +for (i=0;i<n_iter;i++) { |
| 159 | +Messagemsg; |
| 160 | +msg.data=i; |
| 161 | +msg.hdr.size=sizeof(Message)-sizeof(ShubMessageHdr); |
| 162 | +rc=send(sd,&msg,sizeof(msg),0); |
| 163 | +assert(rc==sizeof(msg)); |
| 164 | +rc=recv(sd,&msg,sizeof(msg),0); |
| 165 | +assert(rc==sizeof(msg)&&msg.data==i+1); |
| 166 | + } |
| 167 | + |
| 168 | +printf("Elapsed time for %d iterations: %d\n",n_iter, (int)(time(NULL)-start)); |
| 169 | +return0; |
| 170 | +} |
| 171 | + |
| 172 | + |
| 173 | + |