本帖最后由 qixing0918 于 2013-11-1 14:08 编辑
我按你说的做还是不行 我有想了想 既然是用的是UTF-8编码 GBK解码 出错 而且我不能改变解码方式 那么我就改变编码 用GBK
发送端
- public class UDPSend2 {
- public static void main(String[] args) throws Exception {
- DatagramSocket socket = new DatagramSocket(7777);
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String line = null;
- byte[] buf = new byte[8192];
- DatagramPacket packet = null;
- while ((line = br.readLine()) != null) {
- if("88".equals(line))
- break;
- buf = line.getBytes("GBK");
- packet = new DatagramPacket(buf, buf.length, InetAddress.getByName("127.0.0.1"), 10000);
- socket.send(packet);
- }
- br.close();
- socket.close();
- }
- }
复制代码 竟然通过了 我分析
1.buf = line.getBytes("GBK"); 你好俩字变成字节 而DatagramPacket packet = new DatagramPacket(buf, buf.length); 是默认去查GBK码表 所以出来了
2.你的那种在我的机器试过了 不行 不知道怎么回事
3.我用你那种方法 代码- public class UDPReceive {
- /*
- * 1.定义socket在接收端的socket要有端口号 2.定义packet 接收数据 3.取出来打印
- */
- public static void main(String[] args) throws Exception, UnknownHostException {
- DatagramSocket socket = new DatagramSocket(10000);
- while (true) {
- byte[] buf = new byte[40];
- DatagramPacket packet = new DatagramPacket(buf, buf.length);
- socket.receive(packet);
- System.out.println(Arrays.toString(packet.getData()));
- System.out.println(new String(packet.getData(),"UTF-8"));
- }
- }
- }
复制代码 结果
[-26, -75, -93, -25, -118, -78, -29, -126, -67, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
浣犲ソ
解码出现问题了
System.out.println(new String(packet.getData(),"UTF-8")); 这句话为什么不能按UTF-8解码 求解!!!!!!
|