#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAX_DESCRIPTORS 2500
int i, fd[MAX_DESCRIPTORS];
void CatchTERM()
{
printf("\nCaught sig TERM or INT! Cleaning up.\n");
for( ; i>=0; i--) {
if( shutdown( fd[i], 2 ) < 0 ) perror("shutdown");
printf("Closing %i\n", i);
if( close( fd[i] ) ) perror("close");
}
printf("Done. Committing suicide. ARRGH!\n");
exit (1);
}
main(argc,argv)
int argc;
char *argv[];
{
int opt,pid;
struct sockaddr_in sin[MAX_DESCRIPTORS];
char buf[2];
if( argc < 2 ) {
printf("Usage:\t%s address [port]\n", argv[0] );
printf("\twhere address is a numeric internet address\n");
printf("\tand port is an optional port number
(default=25)\n");
exit (0);
}
pid = getpid();
opt = 1;
signal( SIGTERM, CatchTERM);
signal( SIGINT, CatchTERM);
for ( i=0; i
fd[i] = socket(AF_INET, SOCK_STREAM, 0);
if ( fd[i] < 0 ) { printf("socket %i failed\n",i);
perror("socket");
}
else {
/* Someday, the following call will be used to allow socket reuse ... */
/* if ( setsockopt( fd[i], SOL_SOCKET, SO_REUSEADDR, ( char *) &opt,
* sizeof(opt)) < 0 ) {
* printf("setsockopt %i failed\n",i); sleep(10); }
*/
bzero((char *)&sin[i], sizeof(sin[0]));
sin[i].sin_family = AF_INET;
sin[i].sin_addr.s_addr = inet_addr(argv[1]);
sin[i].sin_port = htons((argc > 2) ? atoi(argv[2]) : 25);
if( connect(fd[i], &sin[i], sizeof(sin[0])) < 0) {
printf("connect %i failed.\n",i);
perror("connect");
break;
}
read(fd[i], buf, 1);
printf("pid: %i, desc %i\n", pid, i);
}
}
i--;
printf("closing connection.\n");
for ( ; i>=0; i-- ) { if( shutdown( fd[i], 2) <0) perror("shutdown");
if( close(fd[i]) ) perror("close");
else printf("closed %i\n", i);
}
}




Posting Komentar
Idihgo.blogspot.com