Post by Altair LinuxI read the documentation of sdl_net but I think is maybe confused. I not
understand how to put a host, a port number and data to send, etc.
After SDL_Init(), do this:
if (SDLNet_Init() == -1) {
printf("Error initializing SDL_Net: %s\n", SDLNet_GetError());
exit(1);
}
Before SDL_Quit(), do this:
SDLNet_Quit();
To get an IP address from a host name, do this (for www.example.com:80):
IPaddress ipaddress;
if (SDLNet_ResolveHost(&ipaddress, "www.example.com", 80) == -1) {
printf("Unable to resolve host address: %s\n", SDLNet_GetError());
exit(2);
}
To open a TCP connection to an IP address, do this:
TCPsocket tcpsock;
tcpsock=SDLNet_TCP_Open(&ipaddress);
if (!tcpsock) {
printf("Unable to connect to host: %s\n", SDLNet_GetError());
exit(3);
}
To send data over a TCP connection, do this:
char* data = "hello world", tmp;
int length = strlen(data)+1;
int bytessent = 0, result;
tmp = data;
do {
result = SDLNet_TCP_Send(tcpsock, tmp, length - bytessent);
if (result == 0) {
printf("TCP connection closed unexpectedly\n");
exit(4);
}
bytessent += result;
tmp += result;
} while (bytessent < length);
To check if data is available from a TCP connection, do this:
if (SDLNet_SocketReady(tcpsock)) {
// do stuff
}
To receive data from a TCP connection, do this:
int buffer_size = 1024, bytesreceived;
char buffer[buffer_size+1];
bytesreceived = SDLNet_TCP_Recv(tcpsock, buffer, buffer_size);
if (bytesreceived < 0) {
printf("Error receiving data: %s\n", SDLNet_GetError());
exit(5);
}
if (bytesreceived == 0) {
printf("TCP connection closed unexpectedly\n");
exit(6);
}
buffer[bytesreceived] = '\0';
printf("Received data: %s\n", buffer);
To close a TCP connection, do this:
SDLNet_TCP_Close(tcpsock);
you can find additional documentation here:
http://www.libsdl.org/cgi/docwiki.cgi/SDL_net