C:SDL net tutorials

From GDWiki

Jump to: navigation, search
Image:40px-notepad.png To comply with our quality standards, this article may need to be rewritten.
Please help improve this article. The discussion page may contain suggestions.
#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;
}

[edit] See also

Using SDL_net

Personal tools
Categories