Demo entry 6348452

RCDA

   

Submitted by Álvaro Vera on Feb 20, 2017 at 05:21
Language: C. Code size: 3.6 kB.

/**
		* Redes de comunicaciones de datos. 
		* Este software crea una conexión TCP con un servidor, es decir, comienza la comunicación con el servidor, es decir, es el cliente TCP.
		* Datos de entrada: dirección IPv4 de servidor TCP y puerto destino (del servidor)
		*/
		
		#include <stdio.h>
		#include <sys/socket.h> /* socket(), connect(), send() and recv() */
		#include <arpa/inet.h>  /* sockaddr_in and inet_addr() */
		#include <stdlib.h> 	/* atoi() */
		#include <string.h> 	/* memset() */
		#include <unistd.h> 	/* close() */
		#include <errno.h>		/* perror()*/
		
		#define RCVBUFSIZE  12000   /* Size of receive buffer. */
		
		void main(int argc, char *argv[])
		{
		unsigned int sock,len;		 					/* Socket descriptor */
		struct sockaddr_in Servdata; 		/* TCP server address, port, family (IP) */
		unsigned short ServPort;         	/* TCP server port */
		char *servIP;			 	/* TCP Server IP address*/
		char *paq;					/* Cadena de caracteres en la cual se guarda el encabezado HTTP*/
		
		paq="GET /prueba.html HTTP/1.1\r\nHost:LOCALHOST\r\nConnection: Close\r\nUser-agent: Mozilla/5.0\r\nAccept-language: es\r\n\r\n"; /*encabezado http*/
		char buffer[RCVBUFSIZE];	/* Buffer for the received string */
		memset(buffer, 0, RCVBUFSIZE);
		unsigned int  DataStringLen;	 	/* Length of string to echo */
		int bytesRcvd, totalBytesRcvd;	 	/* Bytes read in recv() function and total bytes read */
		int err;
		len=strlen(paq);
		printf("Longitud del request: %d Bytes", len);
		printf("\r\nPresione enter para mostrar la respuesta a su request");
		if (argc != 3) 						/* Test for correct number of arguments */
		{
		fprintf(stderr, "\nError.\n  Usage: %s <Server IP> <TCP server port>\t For example, \n\n./TCPclientconn 192.168.0.237 15812 \n\n",argv[0]);
		exit(1);
		}
		servIP = argv[1];					/* First argument with server IP address */
		ServPort = atoi(argv[2]); /* port. atoi() converts char argument (port) into an integer*/
		
		sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);	/* Create a socket using TCP */
		if (sock < 0)							/*connect() returns -1 if cannot stablish connection*/
		perror(" socket () failed") ;
		
		/* Construct the server address structure */
		memset(&Servdata, 0, sizeof(Servdata)); 			/* Reserve memory space for structure */
		Servdata.sin_family = AF_INET; 						/* Internet address family */
		Servdata.sin_addr.s_addr = inet_addr(servIP); 		/* Server IP address */
		Servdata.sin_port = htons(ServPort); 				/* Server port */
		
		err = connect(sock, (struct sockaddr *) &Servdata, sizeof(Servdata)); /* 3-Way Hand-shake. Establish the connection to the TCP server*/
		if ( err < 0)							/*connect() returns -1 if cannot stablish connection*/
		perror(" connect () failed") ;
		
		getchar(); 		/*Press enter to send*/
		err = send(sock, paq, len, 0);	/*Send data. sock is the socket created by the socket() function above. 
		Third parameter is the lenght of the DataToSend*/
		
		if (err != len)							/*send() returns de number of bytes sent*/
		perror("send() sent a different number of bytes than expected");
		
		bytesRcvd=recv(sock, buffer, RCVBUFSIZE, 0); /*Se recibe en la cadena buffer la información de la respuesta al request hecho previamente*/
		
		if (bytesRcvd <= 0)
		
		perror("recv() failed or connection closed prematurely");
		printf("%s", buffer); /* Imprime los datos recibidos en buffer*/
		printf("Bytes recibidos: %d Bytes" , bytesRcvd);
		printf("\r\nPresione enter para terminar");
		getchar();
		
		close(sock);	/*terminate conection by closing file descriptor sock*/
		
		exit(0);
		}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).