Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <winsock.h>
- #include <iostream>
- #include <vector>
- #pragma comment (lib,"ws2_32.lib")
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- WSADATA wsaData;
- int result = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (result != NO_ERROR)
- {
- cout << "\nBiblioteka WinSock zainicjalizowana niepoprawnie. Kod bledu: " + WSAGetLastError() << endl;
- return 0;
- }
- cout << "\nBiblioteka WinSock zainicjalizowana poprawnie." << endl;
- char sendBuff[1024];
- char receiveBuff[1024];
- int SenderAddrSize;
- int iResult;
- SOCKET _socket = INVALID_SOCKET;
- sockaddr_in sockAddr;
- _socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (_socket == INVALID_SOCKET)
- {
- cout << "\nGniazdo utworzone niepoprawnie. kod bledu: " + WSAGetLastError() << endl;
- WSACleanup();
- return 0;
- }
- cout << "\nGniazdo utworzone poprawnie." << endl;
- sockAddr.sin_family = AF_INET;
- sockAddr.sin_port = htons(7404);
- sockAddr.sin_addr.s_addr = inet_addr("192.168.0.103");
- SenderAddrSize = sizeof (sockAddr);
- while ( true )
- {
- printf("\n\n> ");
- cin >> sendBuff;
- if (strcmp(sendBuff, "exit") == 0)
- break;
- iResult = sendto(_socket, sendBuff, 1024, 0, (SOCKADDR *) & sockAddr, sizeof (sockAddr));
- if (iResult == SOCKET_ERROR)
- {
- cout << "\nNieudana proba wyslania datagramu. Kod bledu: " + WSAGetLastError() << endl;
- continue;
- }
- iResult = recvfrom(_socket, receiveBuff, 1024, 0, (SOCKADDR *) & sockAddr, &SenderAddrSize);
- if (iResult == SOCKET_ERROR)
- {
- cout << "\nNieudana proba odebrania datagramu. Kod bledu: " + WSAGetLastError() << endl;
- continue;
- }
- if (strncmp(receiveBuff, "UNKNOWN COMMAND", 15) == 0)
- {
- printf("\nNie rozpoznano komendy\n");
- continue;
- }
- vector<double> liczby;
- receiveBuff[SenderAddrSize] = ' ';
- char *buff;
- buff = strtok(receiveBuff, " ");
- do
- {
- buff = strtok(NULL, " ");
- liczby.push_back( atof(buff) );
- }while(buff != NULL);
- double min = liczby[0], max = liczby[0], all = 0;
- printf("%c", receiveBuff[0]);
- for (int i = 0; i < liczby.size(); i++)
- {
- if( max < liczby[i])
- max = liczby[i];
- else if (min > liczby[i])
- min = liczby[i];
- all += liczby[i];
- printf(" %.3lf", liczby[i]);
- }
- if (receiveBuff[0] == 'T')
- {
- printf("Temperatura minimalna: %.3lf", min);
- printf("Temperatura maksymalna: %.3lf", max);
- printf("Temperatura srednia: %.3lf", all/liczby.size());
- }
- if (receiveBuff[0] == 'P')
- {
- printf("Cisnienie minimalne: %.3lf", min);
- printf("Cisnienie maksymalne: %.3lf", max);
- printf("Cisnienie srednie: %.3lf", all/liczby.size());
- }
- }
- iResult = closesocket(_socket);
- if (iResult == SOCKET_ERROR)
- {
- cout << "Nieudana proba zamknięcia gniazda. Kod bledu: %d" + WSAGetLastError() << endl;
- }
- WSACleanup();
- cout << "\nUsunieto z pamieci biblioteke WinSock." << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment