Hi.
I am completly newbie in network programming, so got some problems in my project.
Here is some code.
Client:
if (socket.connect("127.0.0.1", 12312) != sf::Socket::Done)
{
cout << "ERROR1" << endl;
}
sf::Packet packetSend;
packetSend << "mapNameRequest";
if (socket.send(packetSend) != sf::Socket::Done)
{
cout << "ERROR2" << endl;
}
while (mapName == "")
{
sf::Packet packetReceive;
socket.receive(packetReceive);
packetReceive >> mapName;
}
//breakpoint
packetSend << "gameSessionNameRequest";
if (socket.send(packetSend) != sf::Socket::Done)
{
cout << "ERROR2" << endl;
}
while (gameSession.getGameSessionName() == "")
{
string msg;
sf::Packet packetReceive;
socket.receive(packetReceive);
packetReceive >> msg;
gameSession.setGameSessionName(msg);
}
Server:
listener.listen(PORT);
//Check requests
sf::Packet packetReceive;
string msg;
while(!quit)
{
if (listener.accept(socket) != sf::Socket::Done)
{
cout << "ERROR2" << endl;
}
if (socket.receive(packetReceive) != sf::Socket::Done)
{
cout << "ERROR3" << endl;
}
if (packetReceive >> msg)
{
if (msg == "mapNameRequest")
{
sf::Packet packetSend;
packetSend << gameMap;
if (socket.send(packetSend) != sf::Socket::Done)
{
cout << "ERROR4" << endl;
}
}
if (msg == "gameSessionNameRequest")
{
sf::Packet packetSend;
packetSend << gameSession.getGameSessionName();
if (socket.send(packetSend) != sf::Socket::Done)
{
cout << "ERROR4" << endl;
}
}
}
}
The problem is in client code. The first request is okay,client can receive mapName.
But after "//breakpoint" there are some porblems.
Client freezes at second "while" cycle. It cant receive "gameSessionNameRequest".
Sorry if question is too stupid.
Thanks.
Got it work.
Now i use thread in server.
Current problem is multiple clients.
void doWork(void)
{
listener.listen(PORT);
listener.accept(socket);
selector.add(socket);
socketsList.push_front(socket);
while(!quit)
{
//Check requests
sf::Packet packetReceive;
string msg;
if (socket.receive(packetReceive) != sf::Socket::Done)
{
cout << "Client leaved: " + socket.getRemoteAddress().toString() << endl;
}
if (packetReceive >> msg)
{
if (msg == "mapNameRequest")
{
sf::Packet packetSend;
globalMutex.lock();
packetSend << gameMap;
globalMutex.unlock();
if (socket.send(packetSend) != sf::Socket::Done)
{
cout << "ERROR4" << endl;
}
}
if (msg == "gameSessionNameRequest")
{
sf::Packet packetSend;
globalMutex.lock();
packetSend << gameSession.getGameSessionName();
globalMutex.unlock();
if (socket.send(packetSend) != sf::Socket::Done)
{
cout << "ERROR4" << endl;
}
}
}
}
}
How to do it?
Am i need to use new thread for each client?