// UDP komunikacja prsota.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <conio.h>
#include <cstdio>
#include <WinSock2.h>
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
class wyjatek
{
DWORD dwError;
public:
wyjatek(int iResult)
{
printf ("Winsock - Blad nr %d",iResult);
}
wyjatek (hostent *rH)
{
dwError = WSAGetLastError();
if (dwError == WSAHOST_NOT_FOUND)
{
printf ("host not found");
}
else if (dwError = WSANO_DATA)
{
printf ("no data record");
}
else
{
printf ("failed %d",dwError);
}
}
};
class TInicjalizacja
{
int iResult;
WSAData wsaData;
int dwError;
public:
TInicjalizacja()
{
iResult = WSAStartup (MAKEWORD (2,2), &wsaData);
if (iResult != 0)
{
throw wyjatek(iResult);
}
else
{
// printf ("Winosck jest oki\n");
}
}
~TInicjalizacja()
{
WSACleanup();
}
};
class THost
{
int iResult;
DWORD dwError;
hostent *rH;
char hostname[100];
sockaddr_in serwer;
sockaddr_in klient;
public:
THost (const char *nazwa)
{
strcpy (hostname,nazwa);
rH = gethostbyname(hostname);
if (rH == NULL) throw wyjatek (rH);
else
{
serwer.sin_family = AF_INET;
serwer.sin_port = htons (5005);
serwer.sin_addr.s_addr = inet_addr (hostname);
//
klient.sin_family = AF_INET;
klient.sin_port = htons (300);
klient.sin_addr.s_addr = (INADDR_BROADCAST);
// klient.sin_addr.s_addr = inet_addr ("255.255.255.255");
}
}
hostent ZwrocHostent()
{
return *rH;
}
sockaddr_in Zwrocserwer()
{
return serwer;
}
sockaddr_in Zwrocklient()
{
return klient;
}
};
struct TAdresyIP
{
int suma;
char* IP;
};
int _tmain(int argc, _TCHAR* argv[])
{
TInicjalizacja TW;
/////////////////////////////////////////////////////////////
SOCKET Client;
Client = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (Client == INVALID_SOCKET)
{
cout << "Nie mozna stworzyc socket" << endl;
getch();
return 1;
}
const char on = 1;
if (setsockopt (Client, SOL_SOCKET, SO_BROADCAST,&on,sizeof (on) ) < 0)
{
cout << "blad" << endl;
getch();
closesocket(Client);
return 1;
}
//////////////////////////////////////////////////////////////////
char host[128] = "127.0.0.1";
THost Th (host);
//////////////////////////////////////////////////////////////////////////////////////
char ciag [1024];
char buff2[1024];
int buffLen = 1024;
int AddrSize; //= sizeof (Addr);
int wynik;
bool koncz = false;
int Random = 0;
char wyslanymsg[1024];
bool sprawdzAdres = false;
vector <TAdresyIP> adresyIP;
TAdresyIP tempadr;
tempadr.suma = 0;
while (true)
{
Random = 1+ (rand()%20);
if (getch() == VK_ESCAPE) break;
itoa (Random,wyslanymsg,10);
int wynik = sendto (Client,ciag,strlen(wyslanymsg),0,(SOCKADDR*)&Th.Zwrocserwer(), sizeof(Th.Zwrocserwer() ) );
if (wynik == INVALID_SOCKET)
{
cout <<"Blad wysylania nr:" << WSAGetLastError();
shutdown (Client,2);
closesocket (Client);
getch();
return 1;
}
AddrSize = sizeof (Th.Zwrocklient());
wynik = recvfrom (Client,buff2,buffLen,0,(SOCKADDR*)&Th.Zwrocklient(),&AddrSize);
buff2[wynik] = 0;
if (wynik == INVALID_SOCKET)
{
cout <<"Blad odbeirania nr" << WSAGetLastError();
shutdown (Client,2);
closesocket(Client);
getch();
return 0;
}
for (int i=0; i< adresyIP.size(); i++)
{
if (adresyIP[i].IP == Th.ZwrocHostent().h_name) sprawdzAdres = true;
}
if (!sprawdzAdres)
{
tempadr.IP = Th.ZwrocHostent().h_name;
tempadr.suma = atoi (buff2);
adresyIP.push_back(tempadr);
}
else
{
for (int i = 0; i < adresyIP.size(); i++)
{
if (adresyIP[i].IP == Th.ZwrocHostent().h_name)
{
adresyIP[i].suma += atoi(buff2);
}
}
}
for (int i = 0; i < adresyIP.size(); i++)
{
cout << adresyIP[i].IP << ": " << adresyIP[i].suma << endl;
}
}
shutdown (Client,2);
closesocket (Client);
cout << "Koniec programu" << endl;
getch();
return 0;
}
Comments
0 B
|👍
/👎