/* * * Tue Sep 23 19:34:20 DST 2003 * Wed Feb 24 17:27:17 CET 2010 * Sample tcp client * * :set tabstop=4 * */ static char __ident[] = "@(#)SPDsoft, Tue Sep 23 19:34:48 DST 2003"; #include #include /* atoi */ #if defined(__MACH__) && defined(__APPLE__) #include #endif #include /* getpid */ #include /* getpid */ #include #include #include #include #include #include #include #include #include #include #include #ifndef INADDR_NONE # define INADDR_NONE (in_addr_t)-1 #endif struct prefs { char *hostname; unsigned int port; char *string; int crlf; int bcast; int answers; unsigned int timeout; }; int open_tcp_server( char *host, struct sockaddr_in *server_addr, unsigned int port, unsigned timeout, int bcast, char *data ); int close_tcp_server(int fd); void g_init(int argc,char **argv,struct prefs*); int recv_bcast(int s, struct sockaddr_in server_addr, int answers, unsigned timeout); int main(int argc, char *argv[]) { struct prefs pref; int server; struct sockaddr_in server_addr; int l; char buffer[256]; g_init(argc,argv,&pref); fprintf(stderr, "Trying: %s:%d...\n",pref.hostname, pref.port); if (-1 == (server=open_tcp_server( pref.hostname, &server_addr, pref.port, pref.timeout, pref.bcast, pref.string))) { fprintf(stderr, "Conection can't be established\n"); } else { fprintf(stderr, "Conection established\n"); if (!pref.bcast) { write(server,pref.string,strlen(pref.string)); while (0 < ( l=recv(server,buffer,256,0))) { buffer[l]='\0'; fprintf(stdout, "%s", buffer); } } else { recv_bcast(server, server_addr, pref.answers, pref.timeout); } close_tcp_server(server); } } void g_init(int argc,char **argv,struct prefs *pref) { extern char *optarg; extern int optind,opterr,optopt; int option; unsigned char err=0; pref->hostname="localhost"; pref->port=8001; pref->string=""; pref->crlf=0; pref->bcast=0; pref->timeout=5; pref->answers=1; while((option=getopt(argc,argv,"H:p:s:bct:a:")) != EOF ) { switch(option) { case 'H': pref->hostname=optarg; break; case 'p': pref->port =(unsigned short) atoi(optarg); break; case 'b': pref->bcast=1; break; case 't': pref->timeout =(unsigned short) atoi(optarg); break; case 's': pref->string=optarg; break; case 'c': pref->crlf=1; break; case 'a': pref->answers =(unsigned short) atoi(optarg); break; default: err++; break; } } if ( pref->crlf ) { char *s; s = malloc(strlen(pref->string)+2); strcpy(s,pref->string); strcat(s, "\x0d\x0a"); pref->string=s; } if ( err != 0 ) { fprintf( stderr, "Use: %s -H host -p port -b -a answers -t timeout -s string -c\n", argv[0]); exit(-1); } } /*****************************************************************************/ int open_tcp_server( char *host, struct sockaddr_in *server_addr, unsigned int port, unsigned timeout, int bcast, char *data ) { int sockfd; int i=1, n; unsigned long addr; struct hostent *he; struct sockaddr_in peer; struct sockaddr_in local; alarm(timeout); /* * Broadcast: we need a datagram UDP socket */ if ( 0 > (sockfd = socket(AF_INET,bcast?SOCK_DGRAM:SOCK_STREAM,0))) { perror("client: can't open stream socket"); alarm(0); return(-1); } addr = inet_addr(host); if (addr == INADDR_NONE) { he = gethostbyname(host); if (he == NULL) { perror("gethostbyname: no IP address found"); alarm(0); return(-1); } addr = ((struct in_addr *)(he->h_addr))->s_addr; } memset(&peer,0,sizeof(peer)); peer.sin_family = AF_INET; peer.sin_addr.s_addr = addr; peer.sin_port = htons(port); memset(&local,0,sizeof(local)); local.sin_family = AF_INET; local.sin_addr.s_addr = INADDR_ANY; local.sin_port = 0; if (bind(sockfd, (struct sockaddr *) &local, sizeof(local))) { perror("client: can't bind socket"); alarm(0); close(sockfd); return(-1); } if (bcast) { if ( 0 > ( setsockopt( sockfd, SOL_SOCKET,SO_BROADCAST, (void *)&i,sizeof(i)))) { perror("client: failure to set SO_BROADCAST"); close(sockfd); alarm(0); return(-1); } if ( 0 >= ( n=sendto( sockfd, data, strlen(data), 0, (struct sockaddr *) &peer, sizeof(struct sockaddr_in)))) { perror("client: sendto failed"); } } else { if ( 0 > connect(sockfd,(struct sockaddr *)&peer, sizeof(peer))) { perror("client: can't connect to server"); close(sockfd); alarm(0); return(-1); } } memcpy(server_addr,&peer,sizeof(peer)); alarm(0); return sockfd; } /*****************************************************************************/ int close_tcp_server(int fd) { return(close(fd)); } /*****************************************************************************/ int recv_bcast(int s, struct sockaddr_in server_addr, int answers, unsigned timeout) { #define BS 1000 double n=0; unsigned long n_p=0; long nbytes; struct sockaddr_in server, client; int clientlen=sizeof(client); int fd; unsigned char buf[BS]; int done; uint32_t sum; unsigned char *req; int err; struct timeval tv; fd_set mask, rmask; int nfound; int maxfd; int reuse=1; tv.tv_sec = 2; tv.tv_usec = 500000; done=0; FD_ZERO(&mask); FD_SET(s, &mask); maxfd=s; for(;done= ( nbytes = sendto( s, "ACK ", 6, 0, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in)))) { perror("client: sendto failed"); } #endif done++; FD_ZERO(&mask); FD_SET(s, &mask); } } if ( -1 == close(s)) perror("close"); else fprintf(stderr,"- %d: closed\n", s); return(errno); /* Keep compiler happy */ }