Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if 0
- #!/bin/sh
- g++ -Wall `sdl-config --cflags` server.cpp -o server `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;
- }
- // Start Server
- IPaddress myip;
- if (SDLNet_ResolveHost(&myip, NULL, 14000) < 0) {
- std::cout << "SDLNet_ResolveHost: " << SDLNet_GetError() << "\n";
- return 1;
- }
- TCPsocket socket = SDLNet_TCP_Open(&myip);
- if (!socket) {
- std::cout << "SDLNet_TCP_Open: " << SDLNet_GetError() << "\n";
- return 1;
- }
- // Wait to accept Client
- TCPsocket client = NULL;
- while (client == NULL) {
- client = SDLNet_TCP_Accept(socket);
- }
- IPaddress* hisip = SDLNet_TCP_GetPeerAddress(client);
- // Open UDP Socket
- UDPsocket udp = SDLNet_UDP_Open(0);
- 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;
- int input;
- int ret;
- while (running) {
- std::cout << "Send\n> ";
- std::cin >> input;
- p->data = (Uint8*)input;
- p->address.host = hisip->host;
- p->address.port = hisip->port;
- p->len = sizeof(int);
- ret = SDLNet_UDP_Send(udp, -1, p);
- std::cout << "Success: " << ret << "\n";
- if (input == 0) {
- running = false;
- }
- }
- SDLNet_FreePacket(p);
- SDLNet_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement