2012-10-22 2 views
0

멀티 캐스트를 오랫동안 보내려고합니다. 문자열을 보낼 수 있기 때문에 연결이 작동해야합니다.UDP 멀티 캐스트를 통해 오래 보내십시오.

이 내 서버 측 코드 :

currentServerStatusId = Server.getServerStatusVersionId(); 
buf = ByteBuffer.allocate(8).putLong(currentServerStatusId).array(); //long should be 8 bytes in Java 
InetAddress group = InetAddress.getByName(multicastAddress); 
DatagramPacket packet = new DatagramPacket(buf, buf.length, group, port); 
socket.send(packet); 

이것은 (수신기) 클라이언트 측에이 나에게 BufferUnderflowException을 제공

byte[] buf = new byte[256]; 
serverIpPacket = new DatagramPacket(buf, buf.length); 
System.out.println("waiting to receive"); 
multicastSocket.receive(serverIpPacket); 
receivedIp = serverIpPacket.getAddress().getHostAddress(); 
currentServerStatusId = ByteBuffer.allocate(8).put(serverIpPacket.getData()).getLong(); 
//new String(serverIpPacket.getData(), 0, serverIpPacket.getLength()); 
System.out.println("received current ServerStatusId: " + currentServerStatusId); 

. 수신자/클라이언트 측의 할당 방법에서 크기를 8에서 16으로 두 배로 늘리면 분명히 작동합니다. 하지만 대신 내 시험 값의 0을 (68763 같은)

답변

0

좋아, 죄송합니다, 문제에 대한하지만 난 그냥 대답 나 자신을 발견 반환 내가 클라이언트 측에 넣고해야

을 :

를 의
ByteBuffer buffer = ByteBuffer.wrap(serverIpPacket.getData()); 
currentServerStatusId = buffer.getLong(); 

당신은 그것을 간단의 DataInputStream과 DataOutputStream에, 그리고 훨씬 더 효율적으로 사용하기 위해 찾을 것

+1

모든. – EJP

관련 문제