Advertisement
Glocke

client.cpp

Jan 4th, 2013
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #if 0
  2. #!/bin/sh
  3. g++ -Wall `sdl-config --cflags` client.cpp -o client `sdl-config --libs` -lSDL_net
  4.  
  5. exit
  6. #endif
  7.  
  8. #include <iostream>
  9.  
  10. #include "SDL_net.h"
  11.  
  12. int main(int argc, char **argv) {
  13.     // Init SDL Net
  14.     if (SDLNet_Init() < 0) {
  15.         std::cout << "SDLNet_Init: " << SDLNet_GetError() << "\n";
  16.         return 1;
  17.     }
  18.     // Connect to Server
  19.     IPaddress serverip;
  20.     if (SDLNet_ResolveHost(&serverip, "localhost", 14000) < 0) {
  21.         std::cout << "SDLNet_ResolveHost: " << SDLNet_GetError() << "\n";
  22.         return 1;
  23.     }
  24.     TCPsocket socket = SDLNet_TCP_Open(&serverip);
  25.     if (!socket) {
  26.         std::cout << "SDLNet_TCP_Open: " << SDLNet_GetError() << "\n";
  27.         return 1;
  28.     }
  29.     // Open UDP Socket
  30.     UDPsocket udp = SDLNet_UDP_Open(14000);
  31.     if (!udp) {
  32.         std::cout << "SDLNet_UDP_Open: " << SDLNet_GetError() << "\n";
  33.         return 1;
  34.     }
  35.     // Allocate memory for the packet
  36.     UDPpacket* p = SDLNet_AllocPacket(512);
  37.     if (!p) {
  38.         std::cout << "SDLNet_AllocPacket: " << SDLNet_GetError() << "\n";
  39.         return 1;
  40.     }
  41.    
  42.     bool running = true;
  43.     std::cout << "Waiting for data...\n";
  44.     while (running) {
  45.         if (SDLNet_UDP_Recv(udp, p)) {
  46.             std::cout << "Incomming UDP Packet:\n"
  47.                       << "\tChannel: " << p->channel << "\n"
  48.                       << "\tData:    " << (int*)(p->data) << "\n"
  49.                       << "\tLen:     " << p->len << "\n"
  50.                       << "\tMaxlen:  " << p->maxlen << "\n"
  51.                       << "\tStatus:  " << p->status << "\n"
  52.                       << "\tAddress: " << p->address.host << ":" << p->address.port << "\n";
  53.             if ((int*)(p->data) == 0) {
  54.                 running = false;
  55.             }
  56.         }
  57.     }
  58.    
  59.     SDLNet_FreePacket(p);
  60.     SDLNet_Quit();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement