java - UDP networking 3 sec delay -
so, started working on multiplayer game today , run serious problem delay in networking. when test things on 1 machine using localhost, theres no noticable delay. when tried running client on laptop , server on pc im experiencing 2-3 sec delay.
basically im doing is:
server:
is running 2 threads, 1 listens packets on port , when recieves packet input, updates gamestate accordingly. second thread takes gamestate first , every 10ms sends client.
client:
also 2 threads, 1 recieves gamestate , second sends packets keyboard input every 10ms.
im seding datagrampackets bytearray came serialized class (both have size 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()); } { 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()); } { 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 slow? or should know udp networking.
how achieving such high frequency 2 threads (10ms)? sure running @ such high frequency? why use different threads receiving , sending - take longer sending after receiving on same thread. of course have accommodate latency on internet games have tolerate 200ms between each peer - in client server 400ms.
Comments
Post a Comment