Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if 0
- #!/bin/sh
- g++ -Wall `sdl-config --cflags` client.cpp -o client `sdl-config --libs` -lSDL_net
- exit
- #endif
- #include <iostream>
- #include "SDL_net.h"
- int main(int argc, char **argv) {
- // Init SDL Net
- if (SDLNet_Init() < 0) {
- std::cout << "SDLNet_Init: " << SDLNet_GetError() << "\n";
- return 1;
- }
- // Connect to Server
- IPaddress serverip;
- if (SDLNet_ResolveHost(&serverip, "localhost", 14000) < 0) {
- std::cout << "SDLNet_ResolveHost: " << SDLNet_GetError() << "\n";
- return 1;
- }
- TCPsocket socket = SDLNet_TCP_Open(&serverip);
- if (!socket) {
- std::cout << "SDLNet_TCP_Open: " << SDLNet_GetError() << "\n";
- return 1;
- }
- // Open UDP Socket
- UDPsocket udp = SDLNet_UDP_Open(14000);
- if (!udp) {
- std::cout << "SDLNet_UDP_Open: " << SDLNet_GetError() << "\n";
- return 1;
- }
- // Allocate memory for the packet
- UDPpacket* p = SDLNet_AllocPacket(512);
- if (!p) {
- std::cout << "SDLNet_AllocPacket: " << SDLNet_GetError() << "\n";
- return 1;
- }
- bool running = true;
- std::cout << "Waiting for data...\n";
- while (running) {
- if (SDLNet_UDP_Recv(udp, p)) {
- std::cout << "Incomming UDP Packet:\n"
- << "\tChannel: " << p->channel << "\n"
- << "\tData: " << (int*)(p->data) << "\n"
- << "\tLen: " << p->len << "\n"
- << "\tMaxlen: " << p->maxlen << "\n"
- << "\tStatus: " << p->status << "\n"
- << "\tAddress: " << p->address.host << ":" << p->address.port << "\n";
- if ((int*)(p->data) == 0) {
- running = false;
- }
- }
- }
- SDLNet_FreePacket(p);
- SDLNet_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement