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

Author Topic: UDP server problems. =(  (Read 4722 times)

0 Members and 3 Guests are viewing this topic.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
UDP server problems. =(
« on: August 07, 2011, 07:21:46 pm »
So... This is exactly what is happening with me:

It runs perfectly fine, no problems or issues.
I invite my friend to join to see if it works properly.
He joins, and it instantly ignores me, and focuses on him.
He disconnects.
Server focuses on me again.

How do I fix this? Also, if anyone has any suggestions on how to make my code more efficient, that would also be appreciated.
By the way, this is only the loop part of my server. Everything else works fine.

Code: [Select]

while(running){
    IPAddress Address;
    unsigned short port;
    if(Listener.Receive(Received, Address, port) != Socket::Done){
        cout << "Error receiving information from " << Address << " ( Message ) " << endl;
    }
    string message;
    message.clear();
    Received >> message;
    if(message == "New character"){
        float x,y;
        bool exists = false;
        int character;
        string name;
        Received >> name >> x >> y >> character;
        cout << name << endl;
        for(int a = 0; a < connected_players; a++){
            if(playersc[a].getIP() == Address){
                exists = true;
            }
        }
        if(exists == false){
            players[connected_players].SetX(x);
            players[connected_players].SetY(y);
            playersc[connected_players].setName(name);
            playersc[connected_players].setCharacter(character);
            playersc[connected_players].setIP(Address);
            connected_players++;
            cout << "There are now " << connected_players << " playing. " << Address << " has connected!" << endl;
        }
    }
    if(message == "Remove character"){
        for(int a = 0; a < connected_players; a++){
            if(playersc[a].getIP() == Address){
                for(int b = a; b < connected_players; b++){
                    playersc[b] = playersc[b + 1];
                    players[b] = players[b + 1];
                }
                cout << "now there are " << connected_players - 1 << " players playing. " << Address << " has disconnected!" << endl;
                connected_players--;
            }
        }
    }
    if(message == "Update"){
        float x, y;
        string name;
        int character;
        Received >> name >> x >> y >> character;
        for(int a = 0; a < connected_players; a++){
            if(playersc[a].getIP() == Address){
                playersc[a].setCharacter(character);
                players[a].SetX(x);
                players[a].SetY(y);
            }
        }
        ToSend << connected_players - 1;
        if(Listener.Send(ToSend, Address, port) != Socket::Done){
            cout <<"Error sending information to "<< Address << " ( Update ) " << endl;
        }
        ToSend.Clear();
        for(int a = 0; a < connected_players - 1; a++){
            if(playersc[a].getIP() != Address){
                ToSend << playersc[a].getName() << players[a].GetPosition().x <<players[a].GetPosition().y << playersc[a].getCharacter();
                if(Listener.Send(ToSend, Address, port) != Socket::Done){
                    cout << "Error sending information to "<< Address << " ( Update ) " << endl;
                }
                ToSend.Clear();
            }
        }
    }
}
[/code]

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
UDP server problems. =(
« Reply #1 on: August 08, 2011, 11:44:08 pm »
Code: [Select]
ToSend << connected_players - 1;
        if(Listener.Send(ToSend, Address, port) != Socket::Done){
            cout <<"Error sending information to "<< Address << " ( Update ) " << endl;
        }


You're not sending it to anyone or you're just sending it to the last index in the array. You might want to cycle the array[connected_players] instead.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
UDP server problems. =(
« Reply #2 on: August 10, 2011, 12:11:21 am »
It is? I thought it sent information to the most recent player who sent a packet to the server.

Also, because I'm planning on making a multiplayer game, could anyone help with with ideas on how I should do it? You don't have to write the whole thing, pseudo-code or an outline would be fine. I'm thinking about re-writing it, so the client only listens for keypresses, displays information, and sends/receives information to and from the server, and the server does all the calculations, and updates the players positions instead of the client. Is this a good, or bad method?

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
UDP server problems. =(
« Reply #3 on: August 10, 2011, 12:56:45 am »
1. I don't know how to use your objects -- you're the one coding them, so you should know better than me.

Look at the snippet of code I took from the code you gave. That is the only thing I can see that doesn't make any sense.

Why are you putting the last index into the output stream?

2. Your second question is out of the scope of the original topic.

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
UDP server problems. =(
« Reply #4 on: August 10, 2011, 01:17:48 am »
Actually, now that I look at this again, here is another problem:

Code: [Select]
while(running){
    IPAddress Address;
    unsigned short port;
    if(Listener.Receive(Received, Address, port) != Socket::Done){
        cout << "Error receiving information from " << Address << " ( Message ) " << endl;
    }


What are you initializing your instance of IPAddress to?
What are you setting port to?
This local object and local variable look empty to me.

If you're trying to keep track of connections, have an array store each connection, then cycle through each connection to check its status (whether connected or not) and then call DoWhatever(); according to the status.

Also, simplify this loop. It's doing too many things at once. It's trying to establish connections, update clients, and disconnect clients. I'm not sure what its main function is.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
UDP server problems. =(
« Reply #5 on: August 10, 2011, 02:22:50 am »
Well, my while loop just waits for someone to send information to it, and the packet's "header" tells the server what to do with the other information in it.
i.e. "New player" goes into the if(message == "New player"){ section, and uses whatever is left with the packet to make a new player.
My local variables don't need anything assigned to them... they get things assigned to them after that Socket.Receive(packet, IPAddress, port) is called. That's the only way you're able to get information about the client in the server.
Simplify the loop? Do you mean just add more functions to call it it? Like.. Instead of adding all that stuff in the middle, just check on what the client's intentions are, and then pushing it into a new function?

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
UDP server problems. =(
« Reply #6 on: August 10, 2011, 03:24:08 am »
I can't tell what your problem is with what you've supplied.

You say the server responds to your client. Then when another client connects it responds only to that client. Then when that client disconnects, the server responds to your client again.

Like you said, the snippet of code you provided "sent information to the most recent player who sent a packet to the server.

It is one of two problems:

1. If the client is sending information to the server, then the server will respond to THAT CLIENT ONLY.

If Client A sends information to Server, Client B will not be aware of it; if Client B sends information to Server, Client A will not be aware of it.

2. If it is the most recent packet, then if Client A sends a packet to Server, then Server will always respond to Client A, until Client B sends a packet to Server, and then Server will always respond to Client B, until Client A sends a packet to Server.

Is this the source of your server ingoring your client problem?

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
UDP server problems. =(
« Reply #7 on: August 10, 2011, 04:22:58 am »
Yeah, the server only replies back when the client requests data, which is when it updates it's player. The game will be running at 60 FPS, so I figured it shouldn't be that big of a deal if I do that, or update periodically. Is it?
I guess this is my first server/client stuff, and I am kinda lost on what methods to use.

This is the whole server code, that's relatively relevant at all. It's the loop. Initialization is fine. Just, something is happening inside the loop that makes things... confusing.

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
UDP server problems. =(
« Reply #8 on: August 10, 2011, 04:51:54 am »
You still haven't explained this line:

Code: [Select]
ToSend << connected_players - 1;



Don't have the server immediately update the client as soon as it gets information from the client. Have the server do something like...

1. get input from client
2. queue input
3. process queue
4. do whatever else, like spawn mobs, tick timer, etc.
5. send client update information

This way, you can get multiple inputs from different clients but still process them according to specific time intervals. You'll need this structure especially if you're getting spammed commands from multiple players.

Also, you might want to use TCP instead of UDP for better connection integrity. With TCP, you'll be sure the client gets its information from the server and vice versa.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
UDP server problems. =(
« Reply #9 on: August 10, 2011, 07:52:22 pm »
That is supposed to show how many players are in the game, minus the client it's sending to. So... I should have my server set up as:
1.) Receive information.
2.) Que information.
--New Thread--
1.) Process information
2.) Update AI, extras, ect.
3.) Send information to all the clients
? Would that be more productive?

Use TCP? Isn't that really slow because of its error checking? This is going to be running 60 FPS, and I don't want it to look laggy..

Omega

  • Newbie
  • *
  • Posts: 29
    • View Profile
UDP server problems. =(
« Reply #10 on: August 11, 2011, 12:11:53 am »
I doubt TCP will affect your FRAMES PER SECOND as one is a network function and the other is a video function. Connection speed should not affect your drawing speed.

UDP is a spam socket and IS NOT RELIABLE for data transmission.

What if your server is sending data and the client isn't recieving it? The server won't know, it will just keep sending data to the destination, and vice versa.

What if the server is down? The client could get hung up waiting for data to draw and never know that the server is down because there is no established connection to check against.

You don't need to make multiple threads. This technique can be used with one thread and multiple socket objects. Multiple threads is just not necessary or even relevant. This why I'm recommending TCP over UDP.

Have a socket dedicated to listening ONLY.

Have a socket array for established connections ONLY.

Established connections can then manage their own Recieve and Send functions when you cycle through connection array for input/output processing.

sknnywhiteman

  • Newbie
  • *
  • Posts: 38
    • View Profile
    • Email
UDP server problems. =(
« Reply #11 on: August 11, 2011, 02:24:17 am »
The networking isn't going to affect the FPS, but it's going to be very laggy when it updates if something happens to the speed of the packets. The game might be running at 60 FPS, but the updating might not be that fast.
UDP does make it so you can loose data, but it doesn't mean all of it is lost. I'm using packets, and when I get variables from the received packets, the variables might not get the right values for it because a previous one might be missing. But, everything will be fine the very next time it sends data. UDP doesn't loose data that often to where I should switch to TCP. And UDP is standard for games that run fast and require lots of data per second. TCP is used for turn based games, and slower paced games, or things that require exact precision for things, like FTP. If I miss one frame of information, it's not going to ruin everything.
Also, If I did switch to TCP, I know how to do all of that. Just, doing the equivalent in UDP is a bit more challenging.