Hyluss

WinSock Prosta komunikacja sieciowa UDP

Mar 11th, 2017
355
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. // UDP komunikacja prsota.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <conio.h>
  6. #include <cstdio>
  7. #include <WinSock2.h>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12.  
  13. class wyjatek
  14. {
  15.     DWORD dwError;
  16. public:
  17.     wyjatek(int iResult)
  18.     {
  19.         printf ("Winsock - Blad nr %d",iResult);       
  20.     }
  21.  
  22.     wyjatek (hostent *rH)
  23.     {
  24.             dwError = WSAGetLastError();
  25.             if (dwError == WSAHOST_NOT_FOUND)
  26.             {
  27.                 printf ("host not found");
  28.             }
  29.             else if (dwError = WSANO_DATA)
  30.             {
  31.                 printf ("no data record");
  32.             }
  33.             else
  34.             {
  35.                 printf ("failed %d",dwError);
  36.             }
  37.     }
  38. };
  39.  
  40. class TInicjalizacja
  41. {
  42.     int iResult;
  43.     WSAData wsaData;
  44.     int dwError;
  45. public:
  46.     TInicjalizacja()
  47.     {
  48.         iResult = WSAStartup (MAKEWORD (2,2), &wsaData);
  49.         if (iResult != 0)
  50.         {
  51.            
  52.             throw wyjatek(iResult);
  53.         }
  54.         else
  55.         {
  56.         //  printf ("Winosck jest oki\n");
  57.         }
  58.     }
  59.     ~TInicjalizacja()
  60.     {
  61.         WSACleanup();
  62.     }
  63.  
  64. };
  65.  
  66. class THost
  67. {
  68.     int iResult;
  69.     DWORD dwError;
  70.     hostent *rH;
  71.     char hostname[100];
  72.     sockaddr_in serwer;
  73.     sockaddr_in klient;
  74. public:
  75.     THost (const char *nazwa)
  76.     {
  77.         strcpy (hostname,nazwa);
  78.         rH = gethostbyname(hostname);
  79.         if (rH == NULL) throw wyjatek (rH);    
  80.         else
  81.         {
  82.             serwer.sin_family = AF_INET;
  83.             serwer.sin_port = htons (7300);
  84.             serwer.sin_addr.s_addr = inet_addr (hostname);
  85.  
  86.             //
  87.             klient.sin_family = AF_INET;
  88.             klient.sin_port = htons (300);
  89.         }
  90.     }
  91.  
  92.     hostent ZwrocHostent()
  93.     {
  94.         return *rH;
  95.     }
  96.  
  97.     sockaddr_in Zwrocserwer()
  98.     {
  99.         return serwer;
  100.     }
  101.    
  102.     sockaddr_in Zwrocklient()
  103.     {
  104.         return klient;
  105.     }
  106.  
  107. };
  108.  
  109. int _tmain(int argc, _TCHAR* argv[])
  110. {
  111.    
  112.     TInicjalizacja TW;
  113.    
  114.    
  115. /////////////////////////////////////////////////////////////
  116.     SOCKET Client;
  117.     Client = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  118.     if (Client == INVALID_SOCKET)
  119.     {
  120.         cout << "Nie mozna stworzyc socket" << endl;
  121.         getch();
  122.         return 1;
  123.     }
  124.  
  125. //  sockaddr_in moj_Addr;
  126. //  moj_Addr.sin_port = htons (300); // port
  127. //////////////////////////////////////////////////////////////////
  128.     char host[128] = "127.0.0.1";
  129.  
  130.     THost Th (host);
  131. //////////////////////////////////////////////////////////////////////////////////////s
  132.  
  133. //  char buff[1024];
  134.     char ciag [1024];
  135.  
  136.     char buff2[1024];
  137.     int buffLen = 1024;
  138.     int AddrSize; //= sizeof (Addr);
  139.  
  140.     int wynik;
  141.  
  142.     do
  143.     {
  144.         cout <<"Podaj wartosc do wyslania";
  145.         gets (ciag);
  146.         if (!stricmp (ciag,"exit"))
  147.         {
  148.             shutdown (Client,2);
  149.             closesocket (Client);
  150.             break;
  151.         }
  152.        
  153.         int wynik = sendto (Client,ciag,strlen(ciag),0,(SOCKADDR*)&Th.Zwrocserwer(), sizeof(Th.Zwrocserwer() ) );
  154.         if (wynik == INVALID_SOCKET)
  155.         {
  156.             cout <<"Blad wysylania nr:" << WSAGetLastError();
  157.             shutdown (Client,2);
  158.             closesocket (Client);
  159.             getch();
  160.             return 1;
  161.         }
  162.  
  163.         AddrSize = sizeof (Th.Zwrocklient());
  164.  
  165.         wynik = recvfrom (Client,buff2,buffLen,0,(SOCKADDR*)&Th.Zwrocklient(),&AddrSize);      
  166.         buff2[wynik] = 0;
  167.  
  168.         if (wynik == INVALID_SOCKET)
  169.         {
  170.             cout <<"Blad odbeirania nr" << WSAGetLastError();
  171.             shutdown (Client,2);
  172.             closesocket(Client);
  173.             getch();
  174.             return 0;
  175.         }
  176.  
  177.         cout << "Tresc odeslana: " << buff2 << endl;
  178.         cout << "Adres" << Th.ZwrocHostent().h_name << endl;
  179.         cout << "Port odbiorcy: " << ntohs (Th.Zwrocklient().sin_port) <<endl;
  180.         cout << "Port nadawcy: " << ntohs (Th.Zwrocserwer().sin_port) << endl;
  181.     }while (true);
  182.  
  183.     cout << "Koniec programu" << endl;
  184.     getch();
  185.     return 0;
  186.  
  187. }
Advertisement
Comments
  • User was banned
Add Comment
Please, Sign In to add comment