오늘 멀티 플레이어 게임을 시작했으며 네트워킹 지연으로 심각한 문제가 발생합니다. localhost를 사용하여 한 컴퓨터에서 테스트 할 때 눈에 띄는 지연이 없습니다. 하지만 PC에서 내 노트북과 서버에서 클라이언트를 실행하려고하면 약 2 ~ 3 초가 지연됩니다.UDP 네트워킹 3 초 지연
메신저 행함은 기본적으로 무엇을 :
서버 :
는 두 개의 스레드, 포트에 패킷을 수신하고 입력이 패킷을 받으면에 때, 그는 이에 따라 gamestate을 업데이트를 실행 중입니다. 두 번째 스레드는 첫 번째 스레드로부터 gamestate를 취해서 10ms마다 클라이언트에 보냅니다.
클라이언트 :
또한개의 스레드 중 하나는 gamestate을 접수에 두 번째는 키보드 입력으로 10ms마다 패킷을 전송한다.
는 는 임 (모두 크기를 약 100 바이트가) 직렬화 된 클래스에서 들어온 된 ByteArray에 DatagramPackets의를 seding 는보내기 코드
: 코드는
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());
}
}
받으십시오 :
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());
}
}
모든 십오 이유 너무 느려? 또는 udp 네트워킹에 대해 알아야 할 사항.
'bis'와 'out'을 닫으면됩니다. 자신에게 몇 가지 코드를 저장하십시오. – EJP