// client.cpp : main project file.

#include "stdafx.h"
#include <iostream>
//#include <sys/types.h>
#include <winsock2.h>
#include <windows.h>

#pragma comment ( lib, "Ws2_32.lib" )

#if 0
extern "C" {
 int send(SOCKET s,const char*buf,int len,int flags);
}
#endif
using namespace System;
using namespace std;

#define MAX_SIZE_BUFF 20

int main(array<System::String ^> ^args)
//int main(int argc, char *argv[])
{
    int sock = SOCKET_ERROR;
 int family = AF_INET;
 struct sockaddr_in sockaddr;
 //static char buffer[MAX_SIZE_BUFF];
 WSAData wsaBuff;

 //memset(buffer,0,MAX_SIZE_BUFF);

 char *message = "Who are you?";

 if (WSAStartup(0x0202,&wsaBuff;) != 0) 
    {
  cout<<"Error WSAStartup: "<<WSAGetLastError()<<endl;
  cin>>sock;
  exit(1);
 }

 if((sock = socket(family, SOCK_STREAM, 0)) == SOCKET_ERROR)
 {
  cout<<"Cannot create a socket"<<endl;
  if (WSACleanup() == SOCKET_ERROR)
  {
   /*
    * Just very bad
    */
  }
  cin>>sock;
  exit(1);
 }

 sockaddr.sin_family = family;
 sockaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 sockaddr.sin_port = htons(777);

 if(connect(sock,(struct sockaddr*)&sockaddr;,sizeof(sockaddr))<0)
 {
  cout<<"Host is unavailable"<<endl;
  shutdown(sock,2);
  if(WSACleanup() == SOCKET_ERROR)
  {
   /*
    * Just very bad
    */
  }
  cin>>sock;
  exit(1);
 }
 else
 {
  cout<<"Connected to host"<<endl;
 }
#if 0
 send(sock, (const char*)message, strlen(message),0);
 recv(sock,buffer,MAX_SIZE_BUFF,0);
 cout<<"[ Server ] : "<<buffer<<endl;
 cout<<"Data from srv :"<<endl;
 cout<<"Port: "<<ntohs(sockaddr.sin_port)<<endl;
#endif
 {
   cout<<"Client has been connected to srv"<<endl;
   unsigned long long int maxAttempts = 10000000;
   unsigned long long int current = 0;

   static const unsigned int size = 64;
   static char buffer[size] = {0};
   static const char forSend[size] = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopa";
   int count = 0;
   int res = SOCKET_ERROR;
   SYSTEMTIME t_begin;
   SYSTEMTIME t_end;
   GetLocalTime(&t_begin);

   cout<<"Started at "<<t_begin.wMinute<<":"<<t_begin.wSecond<<endl;

   while (current < maxAttempts)
   {
    do
    {
     if (count)
     {
      Sleep(10);
     }

     res = send(sock,forSend,size,0);

    }while(res == SOCKET_ERROR && count < 1000);

    recv(sock,buffer,size,0);    
    
    if(!(current % 10000))
    {
     cout<<"Lost: "<<(maxAttempts - current)<<endl;
    }
    current++;
   }
   GetLocalTime(&t_end);

   cout<<"Begin "<<t_begin.wMinute<<":"<<t_begin.wSecond<<endl;
   cout<<"End "<<t_end.wMinute<<":"<<t_end.wSecond<<endl;
   /*
    * Be careful for this values. It will be correct only if app will be run and finish at the same hour.
    * Campare with value which has been mantioned above.
    */
   cout<<"Duration is "<<(t_end.wHour - t_begin.wHour)<<":"<<(t_end.wMinute - t_begin.wMinute)<<endl;
  }

 if(WSACleanup() == SOCKET_ERROR)
 {
  /*
    * Just very bad
    */
 }
 shutdown(sock,2);
 cout<<"Done"<<endl;
 cin>>sock;
    return 0;
}

Add a code snippet to your website: www.paste.org