int main (int argc, char **argv) { int c; extern char *optarg; extern int optind; int nb_message = -1; /* Nb de messages à envoyer ou à recevoir, par défaut : 10 en émission, infini en réception */ unsigned int long_message = 30; int mode = MODE_TCP; int source = -1 ; /* 0=puits, 1=source */ int port = 0; char * dest; while ((c = getopt(argc, argv, "psul:n:")) != -1) { switch (c) { case 'p': if (source == 1) { printf("usage: cmd [-p|-s][-n ##]A\n"); exit(1); } source = 0; break; case 's': if (source == 0) { printf("usage: cmd [-p|-s][-n ##]B\n"); exit(1) ; } source = 1; dest = argv[argc-2]; break; case 'n': nb_message = atoi(optarg); break; case 'l': long_message= atoi(optarg); if(long_message<6){ printf("Longueur des message insuffisante." " (Veuillez s'il vous plait recommencer avec une valeur supérieure à 5." "Respectueusement."); exit(1); } break; case 'u': mode = MODE_UDP; break; default: printf("usage: cmd [-p|-s][-n ##], %c\n",c); break; } } //Recuperation du port port = atoi(argv[argc-1]); if (source == -1) { printf("usage: cmd [-p|-s][-n ##]\n"); exit(1) ; } if (source == 1) { printf("SOURCE : lg_mesg_emis=%d, port=%d, nb_envois=%d, TP=%s dest=%s\n",long_message,port,nb_message,mode==MODE_TCP?"tcp":"udp",dest); if(mode==MODE_UDP) source_udp(long_message, nb_message, dest, port); if(mode==MODE_TCP) source_tcp(long_message, nb_message, dest, port); }else{ char numberofmessage[5]; sprintf(numberofmessage,"%d",nb_message); printf("PUIT : lg_mesg_lu=%d, port=%d, nb_reception=%s, TP=%s\n", long_message, port, nb_message==-1 ?"infini": numberofmessage ,mode==MODE_TCP?"tcp":"udp"); if(mode==MODE_UDP) puit_udp(long_message, nb_message, port); if(mode==MODE_TCP) puit_tcp(long_message, nb_message, port); } return 0; }
int main(int argc, char *argv[]) { int c, fd; char *ptr; if (argc < 2) usage(""); opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "2b:cf:g:hij:kl:n:op:q:r:st:uvw:x:y:ABCDEFG:H:IJ:KL:NO:P:Q:R:S:TU:VWX:YZ")) != -1) { switch (c) { #ifdef IP_ONESBCAST case '2': /* use 255.255.255.255 as broadcast address */ onesbcast = 1; break; #endif case 'b': bindport = atoi(optarg); break; case 'c': /* convert newline to CR/LF & vice versa */ crlf = 1; break; case 'f': /* foreign IP address and port#: a.b.c.d.p */ if ( (ptr = strrchr(optarg, '.')) == NULL) usage("invalid -f option"); *ptr++ = 0; /* null replaces final period */ foreignport = atoi(ptr); /* port number */ strcpy(foreignip, optarg); /* save dotted-decimal IP */ break; case 'g': /* loose source route */ sroute_doopt(0, optarg); break; case 'h': /* TCP half-close option */ halfclose = 1; break; case 'i': /* source/sink option */ sourcesink = 1; break; #ifdef IP_ADD_MEMBERSHIP case 'j': /* join multicast group a.b.c.d */ strcpy(joinip, optarg); /* save dotted-decimal IP */ break; #endif case 'k': /* chunk-write option */ chunkwrite = 1; break; case 'l': /* local IP address and port#: a.b.c.d.p */ if ( (ptr = strrchr(optarg, '.')) == NULL) usage("invalid -l option"); *ptr++ = 0; /* null replaces final period */ bindport = atoi(ptr); /* port number */ strcpy(localip, optarg); /* save dotted-decimal IP */ break; case 'n': /* number of buffers to write */ nbuf = atol(optarg); break; case 'o': /* do not connect UDP client */ connectudp = 0; break; case 'p': /* pause before each read or write */ pauserw = atoi(optarg); break; case 'q': /* listen queue for TCP server */ listenq = atoi(optarg); break; case 'r': /* read() length */ readlen = atoi(optarg); break; case 's': /* server */ server = 1; client = 0; break; #ifdef IP_MULTICAST_TTL case 't': /* IP_MULTICAST_TTL */ mcastttl = atoi(optarg); break; #endif case 'u': /* use UDP instead of TCP */ udp = 1; break; case 'v': /* output what's going on */ verbose++; break; case 'w': /* write() length */ writelen = atoi(optarg); break; case 'x': /* SO_RCVTIMEO socket option */ rcvtimeo = atol(optarg); break; case 'y': /* SO_SNDTIMEO socket option */ sndtimeo = atol(optarg); break; case 'A': /* SO_REUSEADDR socket option */ reuseaddr = 1; break; case 'B': /* SO_BROADCAST socket option */ broadcast = 1; break; case 'C': /* set standard input to cbreak mode */ cbreak = 1; break; case 'D': /* SO_DEBUG socket option */ debug = 1; break; case 'E': /* IP_RECVDSTADDR socket option */ recvdstaddr = 1; break; case 'F': /* concurrent server, do a fork() */ dofork = 1; break; case 'G': /* strict source route */ sroute_doopt(1, optarg); break; #ifdef IP_TOS case 'H': /* IP_TOS socket option */ iptos = atoi(optarg); break; #endif case 'I': /* SIGIO signal */ sigio = 1; break; #ifdef IP_TTL case 'J': /* IP_TTL socket option */ ipttl = atoi(optarg); break; #endif case 'K': /* SO_KEEPALIVE socket option */ keepalive = 1; break; case 'L': /* SO_LINGER socket option */ linger = atol(optarg); break; case 'N': /* SO_NODELAY socket option */ nodelay = 1; break; case 'O': /* pause before listen(), before first accept() */ pauselisten = atoi(optarg); break; case 'P': /* pause before first read() */ pauseinit = atoi(optarg); break; case 'Q': /* pause after receiving FIN, but before close() */ pauseclose = atoi(optarg); break; case 'R': /* SO_RCVBUF socket option */ rcvbuflen = atoi(optarg); break; case 'S': /* SO_SNDBUF socket option */ sndbuflen = atoi(optarg); break; #ifdef SO_REUSEPORT case 'T': /* SO_REUSEPORT socket option */ reuseport = 1; break; #endif case 'U': /* when to write urgent byte */ urgwrite = atoi(optarg); break; case 'V': /* use writev() instead of write() */ usewritev = 1; chunkwrite = 1; /* implies this option too */ break; case 'W': /* ignore write errors */ ignorewerr = 1; break; case 'X': /* TCP maximum segment size option */ maxseg = atoi(optarg); break; case 'Y': /* SO_DONTROUTE socket option */ dontroute = 1; break; case 'Z': /* MSG_PEEK option */ msgpeek = MSG_PEEK; break; case '?': usage("unrecognized option"); } } /* check for options that don't make sense */ if (udp && halfclose) usage("can't specify -h and -u"); if (udp && debug) usage("can't specify -D and -u"); if (udp && linger >= 0) usage("can't specify -L and -u"); if (udp && nodelay) usage("can't specify -N and -u"); #ifdef notdef if (udp == 0 && broadcast) usage("can't specify -B with TCP"); #endif if (udp == 0 && foreignip[0] != 0) usage("can't specify -f with TCP"); if (client) { if (optind != argc-2) usage("missing <hostname> and/or <port>"); host = argv[optind]; port = argv[optind+1]; } else { /* If server specifies host and port, then local address is bound to the "host" argument, instead of being wildcarded. */ if (optind == argc-2) { host = argv[optind]; port = argv[optind+1]; } else if (optind == argc-1) { host = NULL; port = argv[optind]; } else usage("missing <port>"); } if (client) fd = cliopen(host, port); else fd = servopen(host, port); if (sourcesink) { /* ignore stdin/stdout */ if (client) { if (udp) source_udp(fd); else source_tcp(fd); } else { if (udp) sink_udp(fd); else sink_tcp(fd); } } else { /* copy stdin/stdout to/from socket */ if (udp) loop_udp(fd); else loop_tcp(fd); } exit(0); }