
Und zwar bekomme ich beim Versuch mit SDLNet_TCP_Open einen lokalen Server aufzumachen die Fehlermeldung "Couldn't bind to local port". Hier der Konstruktor der Netzwerk-Klasse mit der Methode zur SDL-Initialisierung:
Code: Alles auswählen
Network::Network (Uint16 port)
{
QString errormsg; // string to store an error message
guiwidget = NULL; // there is no GUI yet
if (StartSDL ()) // try to initialize SDL and SDLNet
exit (0); // exit if something went wrong
if ((SDLNet_ResolveHost (&(local.ip), NULL, port)) != 0) // resolve local host
{
errormsg = SDLNet_GetError (); // store the error message
QMessageBox::critical (guiwidget, "Error", // show a dialog box containing the error message
"Unable to resolve local host:\n" + errormsg,
QMessageBox::Ok);
exit (0); // exit the program
}
if ((local.socket = SDLNet_TCP_Open (&(local.ip))) == NULL) // try to open a local server --> BUG
{
errormsg = SDLNet_GetError (); // store the error message
QMessageBox::critical (guiwidget, "Error", // show a dialog box containing the error message
"Unable to start local server:\n" + errormsg,
QMessageBox::Ok);
exit (0); // exit the program
}
if ((sockets = SDLNet_AllocSocketSet (maxclients)) == NULL) // try to allocate memory for the sockets
{
errormsg = SDLNet_GetError (); // store the error message
QMessageBox::critical (guiwidget, "Error", // show a dialog box containing the error message
"Couldn't allocate socket set:\n" + errormsg,
QMessageBox::Ok);
exit (0); // exit the program
}
numclients = 0; // no clients are connected
local.name = "Unknown"; // set default name
}
int Network::StartSDL ()
{
QString errormsg; // string to store error message
if (SDL_WasInit (SDL_INIT_EVERYTHING) == 0) // if SDL is not initialized
{
if (SDL_Init (SDL_INIT_EVERYTHING) != 0) // initialize SDL and check for errors
{
errormsg = SDLNet_GetError (); // store the error message
QMessageBox::critical (guiwidget, "Error", // show a dialog box containing the error message
"Error initializing SDL: \n" + errormsg,
QMessageBox::Ok);
return 1; // indicating that an error occurred
}
atexit (&SDL_Quit); // shutdown SDL when the program quits
if (SDLNet_Init () != 0) // initialize SDLNet and check for errors
{
errormsg = SDLNet_GetError (); // store the error message
QMessageBox::critical (guiwidget, "Error", // show a dialog box containing the error message
"Error initializing SDLNet: \n" + errormsg,
QMessageBox::Ok);
return 1; // indicating that an error occurred
}
atexit (&SDLNet_Quit); // shutdown SDLNet when the program quits
}
return 0; // indicating that no error occurred
}
Als IP bekomme ich 0, was für eine lokale Verbindung glaube ich stimmt. Der Socket ist aber ebenfalls NULL, was nicht so sein soll. Die SDL-Methode wird durch Vererbung der Klasse mehrfach aufgerufen (unter anderem in einem zweiten Thread), was aber aufgrund des ersten ifs nicht stören sollte. Der zweite Thread lauscht auf Datenpakete, da ich ja sonst nur mit Qt-Slots arbeiten kann. Er ist von QThread abgleitet und ich habe nur "run" überschrieben und Qt-Signale hinzugefügt. Das GUI allein reicht zwar zum Verschicken, aber nicht zum Empfangen.
Ein Neustart hat nix gebracht, Vista zeigt den selben Fehler wie Ubuntu (habs auf zwei verschiedenen Ubuntu-Rechnern probiert) an. Auch der Wechsel der Portnummer bringt nichts.
Ich weiß, dass es vielleicht nicht so eine gute Idee ist SDLNet mit Qt zu verwenden, da ja Qt selbst auch Klassen für Netzwerkprogrammierung bereitstellt, aber ich verstehe trotzdem nicht warum das nicht funktioniert.
Ich bin jetzt echt ratlos...