X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2FTCP_echo.c;h=2d41e936f247cece2222a1795570be13b1bfd7d3;hp=691911e0c57c0c23b38ac742d7fa4a3d707d9ff0;hb=374461977afeaf3ec285f3d4f0f1d3b47f7e4043;hpb=d25090faca15102552d77c38161a8a34b0bac41e diff --git a/sources/TCP_echo.c b/sources/TCP_echo.c index 691911e..2d41e93 100644 --- a/sources/TCP_echo.c +++ b/sources/TCP_echo.c @@ -26,20 +26,26 @@ * * Usage: echo -h give all info's * - * $Id: TCP_echo.c,v 1.11 2003/10/20 22:44:16 piccardi Exp $ - * ****************************************************************/ /* * Include needed headers */ -#include /* predefined types */ -#include /* include unix standard library */ -#include /* IP addresses conversion utiliites */ -#include /* socket library */ -#include /* include standard I/O library */ -#include /* include error codes */ -#include /* include erroro strings definitions */ +#include /* primitive system data types */ +#include /* unix standard library */ +#include /* IP addresses conversion utilities */ +#include /* socket constants, types and functions */ +#include /* standard I/O library */ +#include /* error definitions and routines */ +#include /* C strings library */ +#include /* C standard library */ +#include /* TCP constants and types */ + +/* still not defined in some include, because too new ... */ +#ifndef TCP_CONGESTION +#define TCP_CONGESTION 13 +#endif +#include "Gapil.h" #include "macros.h" #define MAXLINE 256 @@ -54,15 +60,17 @@ int main(int argc, char *argv[]) * Variables definition */ int sock, i; + socklen_t len; int reset = 0; - struct sockaddr_in serv_add; + int verbosity = 0; + char buffer[MAXLINE]; struct linger ling; /* * Input section: decode parameters passed in the calling * Use getopt function */ opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "hr")) != -1) { + while ( (i = getopt(argc, argv, "hrv")) != -1) { switch (i) { /* * Handling options @@ -72,6 +80,9 @@ int main(int argc, char *argv[]) usage(); return(1); break; + case 'v': + verbosity = 1; + break; case 'r': reset = 1; break; @@ -89,24 +100,20 @@ int main(int argc, char *argv[]) * Main code beginning * * ***********************************************************/ - /* create socket */ - if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { - perror("Socket creation error"); + /* call sockconn to get a connected socket */ + if ( (sock = sockconn(argv[optind], "echo", 6, SOCK_STREAM)) < 0) { + if (errno) perror("Socket creation error"); return 1; } - /* initialize address */ - memset((void *) &serv_add, 0, sizeof(serv_add)); /* clear server address */ - serv_add.sin_family = AF_INET; /* address type is INET */ - serv_add.sin_port = htons(7); /* echo port is 7 */ - /* build address using inet_pton */ - if ( (inet_pton(AF_INET, argv[optind], &serv_add.sin_addr)) <= 0) { - perror("Address creation error"); - return 1; - } - /* extablish connection */ - if (connect(sock, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) { - perror("Connection error"); - return 1; + /* print some info about the socket, used to test some TCP_* options */ + if (verbosity) { + len = sizeof(buffer); + if (getsockopt(sock, SOL_TCP, TCP_CONGESTION, buffer, &len) < 0) { + perror("Cannot read congestion algorithm"); + } else { + buffer[len]=0; + printf("Congestion algorithm %s\n", buffer); + } } /* check if resetting on close is required */ if (reset) {