#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream>

#pragma comment(lib, "ws2_32.lib")

#define DEFAULT_PORT        5150
#define DEFAULT_BUFFER      64

using namespace std;

//	Wątek główny. Inicjacja interfejsu Winsock,
//	tworzenie gniazda, połączenie
//	z serwerem, a następnie wysyłanie i odbieranie danych.
int main(int argc, char **argv)
{
	cout << "Przyklad do kursu WinSock napisanego przez get'a " << endl;
	cout << "== Polaczenie i rozmowa synchroniczna - KLIENT ==" << endl;

    WSADATA       wsd;
    SOCKET        sClient;
    sockaddr_in server;
    hostent    *host = NULL;
	char	szServer[128];                     
	int		iPort     = DEFAULT_PORT;

	cout << "Podaj IP: ";
	cin >> szServer;

    // Load winsock
    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
    {
		cout << "Nie mozna stworzyc WinSock'a." << endl;
        return 1;
    }

    // Create socket and try to connect with server
    sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sClient == INVALID_SOCKET)
    {
		cout << "Nie mozna utworzyc gniazda (" << WSAGetLastError() << ")." << endl;
        return 1;
    }

    server.sin_family = AF_INET;
    server.sin_port = htons((u_short)iPort);
    server.sin_addr.s_addr = inet_addr(szServer);

    // if server adress is no ip, but name, find ip
    if (server.sin_addr.s_addr == INADDR_NONE)
    {
        host = gethostbyname(szServer);
        if (host == NULL)
        {
			cout << "Nie mozna przeksztalcic na ip: " << szServer << endl;
            return 1;
        }
        CopyMemory(&server.sin_addr, host->h_addr_list[0], host->h_length);
    }

    if (connect(sClient, (sockaddr *)&server, 
        sizeof(server)) == SOCKET_ERROR)
    {
		cout << "Nie mozna sie polaczyc (" << WSAGetLastError() << ")." << endl;
        return 1;
    }

	cout << "Polaczono." << endl;

	char buffer[DEFAULT_BUFFER];
	recv(sClient, buffer, DEFAULT_BUFFER, 0);
	cout << "Odebrano: " << buffer << endl;
	strcpy(buffer, "Witam serdecznie serwerka :)");
	send(sClient, buffer, strlen(buffer)+1, 0);
	cout << "Wyslano: " << buffer << endl;

	cout << "Rozlaczam." << endl;

	closesocket(sClient);
	WSACleanup();

	return 0;
}