Advertisement
Guest User

klient UDP

a guest
Apr 16th, 2011
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1. #include <winsock.h>
  2.  
  3. #include <iostream>
  4.  
  5. #include <string>
  6.  
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. void UDPClient(char * hostaddress, short nPort);
  12.  
  13. int main()
  14. {
  15.     WORD wVersionRequested = MAKEWORD(2,2);
  16.  
  17.     WSADATA wsadata;
  18.  
  19.     int nRet;
  20.  
  21.     short nPort;
  22.  
  23.     char host_address[256];
  24.  
  25.     cout << "Klient UDP" << endl;
  26.  
  27.     cout << "Podaj adres hosta: ";
  28.  
  29.     cin.getline(host_address, 256, '\n');
  30.  
  31.     cout << "Podaj nr portu: ";
  32.  
  33.     cin >> nPort;
  34.  
  35.     nRet = WSAStartup(wVersionRequested, &wsadata);
  36.  
  37.     if(wsadata.wVersion != wVersionRequested)
  38.     {
  39.         cout << "WSADATA error: " << WSAGetLastError() << endl;
  40.  
  41.         return -1;
  42.     }
  43.  
  44.     UDPClient(host_address, nPort);
  45.  
  46.     WSACleanup();
  47.  
  48.     cin.get();
  49.  
  50.     return 0;
  51. }
  52.  
  53. void UDPClient(char * hostaddress, short nPort)
  54. {
  55.     cout << "Klient UDP" << endl;
  56.  
  57.     LPHOSTENT lpHostEntry;
  58.  
  59.     lpHostEntry = gethostbyname(hostaddress);
  60.  
  61.     if(lpHostEntry == NULL)
  62.     {
  63.         cout << "gethostbyname() error: " << WSAGetLastError() << endl;
  64.  
  65.         return;
  66.     }
  67.  
  68.     cout << "Nawiazywanie polaczenia z serwerem: " << hostaddress << " na porcie: " << nPort << endl;
  69.  
  70.     SOCKET clientsocket;
  71.  
  72.     clientsocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  73.  
  74.     if(clientsocket == INVALID_SOCKET)
  75.     {
  76.         cout << "SOCKET error: " << WSAGetLastError() << endl;
  77.  
  78.         return;
  79.     }
  80.  
  81.     SOCKADDR_IN serveraddr;
  82.  
  83.     serveraddr.sin_family = AF_INET;
  84.  
  85.     serveraddr.sin_addr = *((LPIN_ADDR)*(lpHostEntry->h_addr_list));
  86.  
  87.     serveraddr.sin_port = htons(nPort);
  88.  
  89.     char Buffer[256];
  90.  
  91.     cout << "Wpisz komunikat: ";
  92.  
  93.     cin >> Buffer;
  94.  
  95.     int nRet;
  96.  
  97.     int nLength = sizeof(serveraddr);
  98.  
  99.     nRet = sendto(clientsocket, Buffer, strlen(Buffer), 0, (LPSOCKADDR)&serveraddr, nLength);
  100.  
  101.     if(nRet == SOCKET_ERROR)
  102.     {
  103.         cout << "sendto() error: " << WSAGetLastError() << endl;
  104.  
  105.         closesocket(clientsocket);
  106.  
  107.         return;
  108.     }
  109.  
  110.     memset(Buffer, 0, sizeof(Buffer));
  111.  
  112.     nRet = recvfrom(clientsocket, Buffer, sizeof(Buffer), 0, (LPSOCKADDR)&serveraddr, &nLength);
  113.  
  114.     if(nRet == SOCKET_ERROR)
  115.     {
  116.         cout << "recvfrom() error: " << WSAGetLastError << endl;
  117.  
  118.         closesocket(clientsocket);
  119.  
  120.         return;
  121.     }
  122.  
  123.     cout << "Komunikat zwrotny: " << Buffer << endl;
  124.  
  125.     closesocket(clientsocket);
  126.  
  127.     cin.get();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement