简体   繁体   中英

UDP networking 3 sec delay

So, i started working on my multiplayer game today and i run into a serious problem with delay in my networking. When i test things on one machine using localhost, theres no noticable delay. But when i tried running client on my laptop and server on PC im experiencing about 2-3 sec delay.

Basically what im doing is:

Server:

Is running two threads, one that listens for packets on a port and when recieves a packet with input, he updates the gamestate accordingly. The second thread takes the gamestate from the first and every 10ms he sends it to the client.

Client:

Also two threads, one recieves the gamestate and the second sends packets with keyboard input every 10ms.

Im seding datagrampackets with bytearray which came from serialized class (Both have size about 100 bytes)

Send code:

    ServerPacket testPacket = new ServerPacket(player.getX(),player.getY());

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutput out = null;
    try 
    {
      out = new ObjectOutputStream(bos);   
      out.writeObject(testPacket);
      byte[] Bytes = bos.toByteArray();

      DatagramPacket packet = new DatagramPacket(Bytes,Bytes.length,ip,port);

      socket.send(packet);
      //System.out.println("SERVER:SentUpdate");
    } 
    catch (IOException ex) 
    { 
        System.out.println(ex.getMessage());
    } 
    finally 
    {
        try 
        {
            if (out != null) 
            {
              out.close();
            }
        } 
        catch (IOException ex) 
        {
            System.out.println(ex.getMessage());
        }

        try 
        {
          bos.close();
        } 
        catch (IOException ex) 
        {
          System.out.println(ex.getMessage());
        }
    }

Recieve code:

        byte[] data = new byte[packetLength];
        DatagramPacket packet = new DatagramPacket(data, data.length);

        try 
        {    
            socket.receive(packet);
        } 
        catch (IOException ex) 
        {
            System.out.println(ex.getMessage());
        }

     ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData());
    ObjectInput in = null;
    try 
    {
      in = new ObjectInputStream(bis);
      ServerPacket res = (ServerPacket)in.readObject();
      return res;
    } 
    catch (IOException ex) 
    { 
        System.out.println(ex.getMessage());
    } 
    catch (ClassNotFoundException ex) 
    {
        System.out.println(ex.getMessage());
    }        
    finally 
    {
        try 
        {
            bis.close();
        } catch (IOException ex) 
        {
            System.out.println(ex.getMessage());
        }

        try 
        {
            if (in != null) 
            {
              in.close();
            }
        } 
        catch (IOException ex) 
        {
            System.out.println(ex.getMessage());
        }
    }

Any ides why is it so slow? Or anything i should know about udp networking.

How are you achieving such high frequency with your two threads (10ms)? Are you sure they are running at such high frequency? Why use different threads for receiving and sending - that will take longer than sending after receiving on the same thread. Of course you have to accommodate for some latency over the Internet games often have to tolerate up to 200ms between each peer - in client server that could be up to 400ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM