Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [UPD3] Disconnect loads CP a lot  (Read 2778 times)

0 Members and 1 Guest are viewing this topic.

TideSofDarK

  • Newbie
  • *
  • Posts: 6
    • View Profile
[UPD3] Disconnect loads CP a lot
« on: June 09, 2013, 06:28:24 pm »
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.
« Last Edit: June 10, 2013, 08:55:41 pm by TideSofDarK »

TideSofDarK

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Cant receive/send data after using socket
« Reply #1 on: June 09, 2013, 10:56:22 pm »
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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Cant receive/send data after using socket
« Reply #2 on: June 09, 2013, 11:37:31 pm »
You don't need multiple threads with a socketSelector. You can take a look at the tutorial, but I think the usage example in the doc is more easily understandable. (at least it is to me)

TideSofDarK

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Cant receive/send data after using socket
« Reply #3 on: June 10, 2013, 05:17:36 pm »
Thanks, this example helped a lot.

TideSofDarK

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Cant receive/send data after using socket
« Reply #4 on: June 10, 2013, 08:53:58 pm »
Okay, there is another problem.
I used example from doc and when client disconnects from server, server load CP a lot. Before disconnect server load less than 1%, but after it loads about 40%.
What can i do with it?
Thanks.