X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;f=listati%2FTCP_echo1.c;fp=listati%2FTCP_echo1.c;h=d09d0470161612584dec2eb4262e713995ac90b9;hb=7444bbe1f4d1e9858693bfcb41921fa601450a89;hp=0000000000000000000000000000000000000000;hpb=626fee850ef998ab730a6da711494d6a5ace1be5;p=gapil.git diff --git a/listati/TCP_echo1.c b/listati/TCP_echo1.c new file mode 100644 index 0000000..d09d047 --- /dev/null +++ b/listati/TCP_echo1.c @@ -0,0 +1,32 @@ +int main(int argc, char *argv[]) +{ +/* + * Variables definition + */ + int sock_fd, i; + struct sockaddr_in serv_add; + ... + /* create socket */ + if ( (sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + 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_fd, (struct sockaddr *)&serv_add, sizeof(serv_add)) < 0) { + perror("Connection error"); + return 1; + } + /* read daytime from server */ + ClientEcho(stdin, sock_fd); + /* normal exit */ + return 0; +}