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

Author Topic: Socket stops receiving packets  (Read 1903 times)

0 Members and 1 Guest are viewing this topic.

OliO

  • Newbie
  • *
  • Posts: 1
    • View Profile
Socket stops receiving packets
« on: February 05, 2013, 05:01:42 pm »
I have been making my (M)MO project, but a problem has sopped me from progressing. When i use non-blocking UDP sockets for receiving data from players it works at first but after a while the server stops receiving data. I test both the server and the client same time on the same computer, so that could cause some problems.
This is smaller version of the server's code:

Uint16 unused_port = 1026;
struct client
{
    SocketUDP In;
    IPAddress ClientIP;
    client(IPAddress IP)
    {
        In.Bind(unused_port);
        unused_port++;
        In.SetBlocking(false);
        ClientIP=IP;
    }
};


int main()
{

    vector<client> clients;
    SocketTCP Connector;
    Connector.SetBlocking(false);
    Connector.Listen(1025);
    unsigned short port;
    Packet p;
    IPAddress IP("127.0.0.1");

    for(;;)
    {
        //check for new connections
        IPAddress IncomingIP;
        SocketTCP NewClient;
        if(Connector.Accept(NewClient, &IncomingIP) == Socket::Done)
        {
            Packet clients_port;
            clients_port<<unused_port;
            NewClient.Send(clients_port);
            clients.push_back(client(IncomingIP));
            NewClient.Close();
        }
        //go trough clients
        for(vector<client>::iterator i=clients.begin(); i!=clients.end(); i++)
        {
            //get all packets
            while(i->In.Receive(p,i->ClientIP, port)==Socket::Done)
            {
                int a;
                p>>a;
                cout << a << endl;
                p.Clear();
            }
            Sleep(0.001f);
        }
    }
    return 0;
}
 
But with this code the server stops receiving data only if i have once closed and restarted the client program.

The client's code is somewhat smaller:
int main()
{
    int a=0;
    SocketUDP Out;
    SocketTCP Connector;
    Connector.Connect(1025,"127.0.0.1");
    Connector.SetBlocking(true);
    Int16 port;
    Packet p;
    Connector.Receive(p);
    p>>port;
    cout << port << endl;
    p.Clear();

    IPAddress IP("127.0.0.1");
    for(;;)
    {
        a++;
        cout << a << endl;
        p<<a;
        Out.Send(p,IP,port);
        Sleep(0.1f);
        p.Clear();
    }
    return 0;
}
 

It's also interesting that if I change the sleeping time of the server to be smaller (like 0.0001 sec) the freezing happens more often.

I haven't been able to solve this problem myself and this caused me to quit my project for almost half a year.
Any help would be greatly appreciated.
« Last Edit: February 05, 2013, 05:05:41 pm by OliO »