C:SDL net tutorials

From GPWiki

Files:GUITutorial_warn.gif The Game Programming Wiki has moved! Files:GUITutorial_warn.gif

The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server.

However, the GPWiki forums are still active! Come say hello.

#include <SDL/SDL_net.h>

#include <iostream>
using namespace std;

int main()
{
	if(SDLNet_Init())
	{
		cerr << "error : SDLNet_Init()\t"
			<< SDLNet_GetError() << "\n";
		return -1;
	}

	IPaddress ip;

	SDLNet_ResolveHost(&ip,NULL,16);
	cout << "localhost = (" << SDLNet_ResolveIP(&ip) << ")\n";

	SDLNet_ResolveHost(&ip,"www.gamedev.net",80);
	cout << "GDNet = (" << SDLNet_ResolveIP(&ip) << ")\n";

	TCPsocket socket = SDLNet_TCP_Open(&ip);
	if(!socket)
	{
		cerr << "error : SDLNet_TCP_Open(&ip)\t"
			<< SDLNet_GetError() << "\n";
	}

	char *out = "hi\n";
	if(SDLNet_TCP_Send(socket,out,4) < 4)
	{
		cerr << "error : SDLNet_TCP_Send(socket,out,4)\t"
			<< SDLNet_GetError() << "\n";
	}

	char in[128];
	SDLNet_TCP_Recv(socket,(void*)in,128);
	in[127] = 0;
	cout << "GDNet reply [" << in << "]\n";
	SDLNet_TCP_Close(socket);
	SDLNet_Quit();

	return 0;
}

See also

Using SDL_net