(In the beginning, sorry for my english
)
I'm new on this forum, so I would like to greet you all
OK, I have some experience with SFML. About 3 months ago I became interested in sockets (and then threads). I have made simple chat with sockets (with port forwarding)... And it works, yey, it's so easy and I understand it
So now I want to make some network game. The first step is listen to connection. OK, here is some code:
Function which displays text and launching listening thread:
void Game::playOnlineServerMenu()
{
//setting texts
int textsNumber=3;
Text texts[textsNumber];
//string strings[textsNumber]={"Play","Options","Credits","Exit"};
for(int i=0;i<textsNumber;i++)
{
texts[i].setFont(font);
texts[i].setCharacterSize(150);
if(i==0)
texts[i].setString("Waiting for connection...");
else if(i==1)
{
string ip="Your IP is: "+IpAddress::getPublicAddress().toString();
texts[i].setString(ip);
}
if(i<1)
texts[i].setPosition(window.getSize().x/2-texts[i].getGlobalBounds().width/2,250+i*120);
else if(i==1)
texts[i].setPosition(window.getSize().x/2-texts[i].getGlobalBounds().width/2,250+i*120);
texts[i].setColor(Color(175,175,175));
}
//listening to client
thread.launch();
//main loop
while(menuState==playOnlineServer)
{
cout << "Main loop" << endl;
Event event;
while(window.pollEvent(event))
{
//pressed X button
if(event.type==Event::Closed)
{
thread.terminate();
menuState=exit;
}
//released escape button
if(event.type==Event::KeyPressed&&event.key.code==Keyboard::Escape)
{
thread.terminate();
menuState=menu;
}
}
window.setMouseCursorVisible(true);
window.setView(window.getDefaultView());
window.clear();
for(int i=0;i<textsNumber;i++)
window.draw(texts[i]);
window.display();
}
}
Listening and accepting connection function:
void Game::listening()
{
Lock lock(mutex);
cout << "Thread" << endl;
//listening to connection
if(listener.listen(port)!=Socket::Done)
cout << "!ERROR! Server didn't find any connection." << endl;
else
cout << "Connection found" << endl;
//accepting connection
if(listener.accept(socket)!=Socket::Done)
cout << "!ERROR! Server cannot accept connection." << endl;
else
{
cout << "Connection accepted" << endl;
listener.close();
menuState=gameOnlineServer;
}
}
Thread is class member, so it's initialized correctly:
Game::Game():thread(&Game::listening,this)
{
//...
}
I'm gonna transform my code to something better (polymorphism, virtual functions and generally better code design), but it has to work.
OK, so I choose "Server" option, then I see my IP and some text, then I press Escape key and leave whole program. That's what I see in the console output:
Main loop
Thread
Connection found
Main loop
Main loop
Main loop
Main loop
Main loop
...What?! Listener immediately found connection?
But there's not "!ERROR! Server cannot accept connection." or "Connection accepted". I don't understand. I removed thread and just called listening function after draw everything:
Main loop
Thread
Connection foundI don't know what's going on :/ Situation didn't change, so it's not thread fault. My previous network programs work, so I don't know what's wrong with this :/ I solved previous problems, but this is problem with SFML sockets, so I have no idea what's wrong.
Thanks for help.