2011-09-08 4 views
1

남자!자바 전송 UDP 패킷 문제

0xff, 0xff, 0xff, 0xff, 0x54, 0x53, 0x6f, 0x75, 
0x72, 0x63, 0x65, 0x20, 0x45, 0x6e, 0x67, 0x69, 
0x6e, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 
0x00 

내 자바 프로그램 :

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 
import java.net.InetAddress; 

public class Test { 

    private static DatagramSocket ds; 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     try { 
      ds = new DatagramSocket(27022); 
      byte[] data; 
      // TSource Engine Query 
      char peer0_0[] = { 
       0xff, 0xff, 0xff, 0xff, 
       0x54, 0x53, 0x6f, 0x75, 
       0x72, 0x63, 0x65, 0x20, 
       0x45, 0x6e, 0x67, 0x69, 
       0x6e, 0x65, 0x20, 0x51, 
       0x75, 0x65, 0x72, 0x79, 0x00 
      }; 
      data = new String(peer0_0).getBytes(); 

      System.out.println("send: " + new String(data)); 

      DatagramPacket dp = new DatagramPacket(data, 0, data.length, InetAddress.getByName("219.133.59.20"), 27021); 

      ds.send(dp); 
      byte[] rec = new byte[1024]; 
      DatagramPacket dp2 = new DatagramPacket(rec, 1024); 
      ds.receive(dp2); 

      System.out.println("receive: " + new String(rec)); 

      ds.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      if(ds != null) ds.close(); 
     } 
    } 

} 
나는 게임 서버 (카운터 스트라이크)에 메시지를 보낼 수있는 간단한 프로그램을 작성 오전, 해당 메시지는 서버 정보를 조회하는 데 사용됩니다, 그것은 고정 된 형식이 하지 0xff를, 그래서 문제가 무엇 0x3F입니다 있습니다

enter image description here 처음 4 바이트 :

하지만 난 그것을 실행할 때, 나는 패킷을 캡처 와이어 샤크를 사용하여, 나는이있어? Windows 7 중국어 판에서 Java 6을 실행 중입니다.

답변

4

char[]에서 byte[]까지 String을 통한 변환은 문자 집합 변환을 포함하기 때문에 무손실이 보장되지 않습니다.

peer0_0[]byte 배열로 선언하고 직접 작업 해보십시오.

+0

감사합니다. aix! 0xff = 255이지만, java의 최대 바이트는 127입니다. – CaiNiaoCoder

+0

@YAMaiDie :'(byte) 0xff' – NPE

+0

+1 : chars와 bytes는 Java에서 매우 다른 두 가지입니다. –

관련 문제