/*
 *	sock_ex_cliente.c
 *	exemplo de cliente de rotinas de comunicacoes usando sockets
 *
 *  98.03.24
 *
 */

/*
============================== C O P Y R I G H T ==============================

(C) 1995-98 Paulo Sousa

This software was written by Paulo Sousa, Researcher of Instituto Superior de 
Engenharia do Porto (ISEP/IPP), Portugal. Use of this software, for any 
purpose, is granted subject to the following conditions: 

1.	This copyright notice must be included with the software or any software
	derived from it;

2.	The risk of using this software is accepted by the user. No warranty as to
	its usefulness or functionality is provided or implied. ISEP provides no 
	support;

3.	You may redistribute this and other files or routines as long as you are
	not compensated for it. You may not use the released source codes in anyway
	commercial without permission.

It'd be nice if you share your modifications with me and everyone else.
email:	psousa@dei.isep.ipp.pt
WWW:	http://www.dei.isep.ipp.pt/~psousa

===============================================================================
*/

/*
 * antes de compilar copiar macros apropriadas
 *
#define TP_OS_WINDOWS
#define TP_OSVER_WIN32
#define TP_OS_OS400
#define TP_HWD_ALPHA
 *
 */

/* compilacao para Win95/NT em maquina Intel */
#define TP_OS_WINDOWS
#define TP_OSVER_WIN32


#include "tc_sock.h"


main(int argc, char* argv[])
{
SOCKET s;
char host[255];
char service[255];
char msg[255];
char buffer_msg[255 + sizeof(short)];
char buffer_dest[255 + sizeof(short)];
short ctlen, msg_len;
short r_ctlen, r_msg_len;
short r;

	if (argc < 3)
	{
		puts("usage: sock_ex_cliente host service mensagem\n");
		puts("       host - nome ou IP da maquina destino\n");
		puts("       service - nome de servico ou port number\n");
		exit(1);
	}

	/* inicializar variaveis */
	strncpy(host, argv[1], sizeof(host)-1);
	host[sizeof(host)-1] = '\0';
	
	strncpy(service, argv[2], sizeof(service)-1);
	service[sizeof(service)-1] = '\0';

	strncpy(msg, argv[3], sizeof(msg)-1);
	msg[sizeof(msg)-1] = '\0';

	/*
	 * aqui e' que comeca o exemplo propriamente dito
	 */

	/* inicializacoes */
	if (!sock_startup())
	{
		puts("erro ao inicializar comunicacoes\n");
		exit(2);
	}

	/* conectar */
	s = sock_open_stream(host, service);
	if (s == SOCKET_ERROR)
	{
		puts("erro ao criar o socket\n");
		exit(3);
	}

	/* construir mensagem */
	ctlen = htons(strlen(msg));	/* htons - converte um inteiro de 16 bits da representacao
								 * usada no processador do programa cliente para o formato
								 * canonico da rede
								 */
	msg_len = strlen(msg)+sizeof(short);
	memcpy(buffer_msg, &ctlen, sizeof(short));
	memcpy(buffer_msg+sizeof(short), msg, strlen(msg));

	/* enviar */
	r = sock_send_n(s, buffer_msg, msg_len);
	if (r == SOCKET_ERROR)
	{
		puts("erro ao enviar mensagem");
		exit(4);
	}
	else if (r == 0)
	{
		puts("o servidor fechou a conexao");
		exit(5);
	}

	/* receber resposta do servidor */
	r = sock_recv_n(s, &r_ctlen, sizeof(short));
	if (r == SOCKET_ERROR)
	{
		puts("erro ao receber mensagem");
		exit(6);
	}
	else if (r == 0)
	{
		puts("o servidor fechou a conexao");
		exit(5);
	}

	r_ctlen = ntohs(r_ctlen);	/* ntohs - converte um inteiro de 16 bits da representacao
								 * usada na rede para o formato deste processador 
								 */
	sock_recv_n(s, buffer_dest, r_ct_len);
	if (r == SOCKET_ERROR)
	{
		puts("erro ao receber mensagem");
		exit(6);
	}
	else if (r == 0)
	{
		puts("o servidor fechou a conexao");
		exit(5);
	}

	printf("resposta: %s", buffer_dest);

	/* fechar conexao */
	sock_close(s);

	/* limpar a casa */
	sock_cleanup();
}
