Created
April 26, 2020 21:25
-
-
Save marcosllinares/68da405d33102dbf7c23bca9be420c17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <netdb.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <iostream> | |
/* | |
argv[0] ---> nombre del programa | |
argv[1] ---> primer argumento (char *) | |
./time_client 127.0.0.1 3000 t | |
argv[0] = "./time_client" | |
argv[1] = "127.0.0.1" | |
argv[2] = "3000" | |
argv[3] = "t" | |
| | |
| | |
V | |
res->ai_addr ---> (socket + bind) | |
*/ | |
int main(int argc, char **argv) { | |
time_t time_; | |
struct tm *tm_; | |
struct addrinfo hints; | |
struct addrinfo *res; | |
// ---------------------------------------------------------------------- // | |
// INICIALIZACIÓN SOCKET// | |
// ---------------------------------------------------------------------- // | |
memset(&hints, 0, sizeof(struct addrinfo)); | |
hints.ai_family = AF_INET; | |
hints.ai_socktype = SOCK_DGRAM; | |
int rc = getaddrinfo(argv[1], argv[2], &hints, &res); | |
if (rc != 0) { | |
std::cerr << "getaddrinfo: " << gai_strerror(rc) << std::endl; | |
return -1; | |
} | |
int sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
freeaddrinfo(res); | |
// ---------------------------------------------------------------------- // | |
// ENVIO MENSAJE DE CLIENTE // | |
// ---------------------------------------------------------------------- // | |
char buffer[80]; | |
char host[NI_MAXHOST]; | |
char service[NI_MAXSERV]; | |
struct sockaddr client_addr; | |
socklen_t client_len = sizeof(struct sockaddr); | |
sendto(sd, argv[3], 2, 0, &client_addr, client_len); | |
ssize_t bytes = recvfrom(sd, buffer, 79 * sizeof(char), 0, &client_addr, | |
&client_len); | |
if ( bytes == -1) | |
{ | |
std::cerr << "recvfrom: " << std::endl; | |
return -1; | |
} | |
std::cout << buffer << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment