/*
  Name:      POST test
  Copyright: Tomasz Ostrowski
  Author:    Tomasz Ostrowski
  Date: 01-12-05 16:35
  Description:   
*/

#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <string.h>

#define NETWORK_ERROR -1
#define NETWORK_OK     0


void ReportError(int errorCode, const char *whichFunc) {
   char errorMsg[200];					   
   ZeroMemory(errorMsg, 200);				//NULL-terminate the string
   sprintf(errorMsg, "Wywołanie funkcji %s zwróciło błąd %d.", (char *)whichFunc, errorCode);
   //MessageBox(NULL, errorMsg, "Błąd", MB_OK);
   printf(errorMsg);
}



int main(int argc, char *argv[])
{
    printf("Dev-C++ POST test\n(C) Tomasz Ostrowski 2005\n"); 
  
  	WORD sockVersion;
	WSADATA wsaData;
	int nret;
    
    char Request[500];
   	char buffer[500];		
    
	sockVersion = MAKEWORD(1, 1);

	// Initialize Winsock
	WSAStartup(sockVersion, &wsaData);

	// Store information about the server
	LPHOSTENT hostEntry;

	hostEntry = gethostbyname("ostrytomasz.ds.pg.gda.pl");	// Specifying the server by its name;						
	//int d=htonl(0x9913d302);
	//hostEntry = gethostbyaddr((char *)&d,4,PF_INET);	
	
	if (!hostEntry) {
		nret = WSAGetLastError();
		ReportError(nret, "gethostbyname()");	
		//ReportError(nret, "gethostbyaddr()");

		WSACleanup();
		return NETWORK_ERROR;
	}

	// Create the socket
	SOCKET theSocket;
	theSocket = socket(AF_INET,		// Go over TCP/IP
			   SOCK_STREAM,			// stream-oriented socket
			   IPPROTO_TCP);		// Use TCP rather than UDP

	if (theSocket == INVALID_SOCKET) {
		nret = WSAGetLastError();
		ReportError(nret, "socket()");
		WSACleanup();
		return NETWORK_ERROR;
	}

	// Fill a SOCKADDR_IN struct with address information
	SOCKADDR_IN serverInfo;

	serverInfo.sin_family = AF_INET;

	// At this point, we've successfully retrieved vital information about the server,
	// including its hostname, aliases, and IP addresses.  Wait; how could a single
	// computer have multiple addresses, and exactly what is the following line doing?
	// See the explanation below.

	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

	serverInfo.sin_port = htons(7777);		// Change to network-byte order and

	// Connect to the server
	nret = connect(theSocket,
		       (LPSOCKADDR)&serverInfo,
		       sizeof(struct sockaddr));

	if (nret == SOCKET_ERROR) {
		nret = WSAGetLastError();
		ReportError(nret, "connect()");

		WSACleanup();
		return NETWORK_ERROR;
	}

	// Successfully connected!
	                 
 
	nret = recv(theSocket,
		buffer,
		64,		// Complete size of buffer
		0);
    char* Cpos = strchr(buffer, (int)('C') );
    *Cpos=0;   //null-terminate po znaku C
    
	//MessageBox(NULL, buffer, "temperatura", MB_OK);
	printf("\ntemperatura: %s\n\n",buffer);

	if (nret == SOCKET_ERROR)
		{
		return NETWORK_ERROR;
		}
	else
		{
		// nret contains the number of bytes received
		}

	// Send/receive, then cleanup:
	closesocket(theSocket);
	WSACleanup();

    system("PAUSE");	
    return 0;
}
