Skip to content
Snippets Groups Projects
Tarea2-RC-Grupo25-Cliente.cpp 1.28 KiB
Newer Older
using namespace std;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#define PORT "9999"
#define HOST "localhost"
#define MAXLEN 1024
int main() {
	   int client_socket = socket(AF_INET, SOCK_STREAM, 0);
	   //obtenemos la direccion con getaddrinfo
	   struct addrinfo hints, *res;
	   memset(&hints, 0, sizeof hints);
	   hints.ai_family = AF_INET;
	   hints.ai_socktype = SOCK_STREAM;
	   getaddrinfo(HOST, PORT, &hints, &res);
	   //primitiva CONNECT
	   connect(client_socket, res->ai_addr, res->ai_addrlen);

	   //primitiva SEND
	   char *msg = "TCP";
	   int msg_size = strlen(msg);
	   int sent_msg_size = send(client_socket, msg, msg_size, 0);

	   printf("intentando conexion con localhost:9999\n");


	   char* data = (char*)malloc(MAXLEN);
	   int data_size = MAXLEN;

	   while (true){

		   int received_data_size = recv(client_socket, data, data_size, 0);
		   printf("%s\n", data);
		   free (data);

		   data = (char*)malloc(MAXLEN);
		   data_size = MAXLEN;

		   sleep(1);
	   }

	   close(client_socket);
	   freeaddrinfo(res);