/* NAME: TCPClient SYNOPSIS: TCPClient host_id port_number DESCRIPTION: The program creates a stream socket in the inet domain, Connects to TCPServer, Gets messsages typed by a user and Sends them to TCPServer running on hostid Then it waits for a reply from the TCPServer and shows it back to the user, with a message indicating if there is an error during the round trip. */ #include /* socket, connect, recv, send */ #include /* htons */ #include /* inet_addr */ #include /* inet_addr */ #include /* memcpy */ #include /*printf */ #include /* close */ #include /* exit */ #include /* gethostbyaddr */ #define MAXHOSTNAME 80 #define BUFSIZE 1024 char buf[BUFSIZE]; char rbuf[BUFSIZE]; int cleanup(char * ); int main( int argc, char * argv[] ) { struct addrinfo hints, *result, *rp; struct addrinfo hints_local, *result_local; int sfd, s, s2, rc, cc; struct sockaddr_storage peer_addr; socklen_t peer_addr_len; ssize_t nread; char ThisHost[MAXHOSTNAME]; if (argc < 2) { fprintf(stderr, "Usage: %s host port \n", argv[0]); exit(EXIT_FAILURE); } gethostname(ThisHost, MAXHOSTNAME); /*Obtain address(es) matching localhost/localport*/ memset(&hints_local, 0, sizeof(hints_local)); hints_local.ai_family = AF_INET; //IPv4 hints_local.ai_socktype = SOCK_STREAM; //TCP hints_local.ai_flags = AI_PASSIVE; if (getaddrinfo(ThisHost, argv[2], &hints_local, &result_local) != 0) { fprintf(stderr, "getaddrinfo error\n"); exit(EXIT_FAILURE); } printf("----EchoClient/TCP running at host NAME: %s\n", ThisHost); printf("(EchoClient/TCP INET ADDRESS is: %s )\n", inet_ntoa(((struct sockaddr_in*)result_local->ai_addr)->sin_addr)); /*Obtain address(es) matching host/port*/ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; //IPv4 hints.ai_socktype = SOCK_STREAM; //TCP hints.ai_flags = AI_PASSIVE; if ((getaddrinfo(argv[1], argv[2], &hints, &result)) != 0) { fprintf(stderr, "Could not connect\n"); exit(EXIT_FAILURE); } /* getaddrinfo() returns a list of address structures. Try each address until we successfully connect. If socket (or connect) fails, we (close the socket and) try the next address. */ for ( rp = result; rp != NULL; rp = rp->ai_next) { s = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); if (s == -1) { continue; } if (connect(s, rp->ai_addr, rp->ai_addrlen) != 1) { break; //conexion satisfactoria } close(s); } freeaddrinfo(result); /* No longer needed */ if (rp == NULL) { // No hubo exito con ninguna direccion fprintf(stderr, "No se pudo realizar conexion\n"); exit(EXIT_FAILURE); } printf("----EchoServer/TCP running at host NAME: %s\n", argv[1]); printf("(EchoServer/TCP INET ADDRESS is: %s )\n", inet_ntoa(((struct sockaddr_in*)rp->ai_addr)->sin_addr)); /* get data from USER, send it SERVER, receive it from SERVER, display it back to USER */ for(;;) { printf("\nType anything followed by RETURN, or type CTRL-D to exit\n"); cleanup(buf); cleanup(rbuf); rc=read(0,buf, sizeof(buf)); if (rc == 0) break; if (send(s, buf, rc, 0) <0 ) perror("sending stream message"); if ((cc=recv(s, rbuf, sizeof(rbuf), 0)) < 0) perror("receiving echo"); if (cc == rc) if (strcmp(buf, rbuf) == 0){ printf("Echo is good\n"); printf(" Received: %s", rbuf); } else printf("Echo bad -- strings unequal: %s\n", rbuf); else printf("Echo bad -- lengths unequal: %s\n", rbuf); } printf ("EOF... exit\n"); close(s); exit (0); } int cleanup(char * buf) { int i; for(i=0; i