传输时UDP数据包内容更改 [英] UDP Packet content changes while transmitting

查看:111
本文介绍了传输时UDP数据包内容更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编程一个服务器-客户端软件,其中客户端通过端口请求连接到服务器,服务器打开一个新端口并发回新的端口号.然后,客户端与RSA共享AES密钥加密和端口请求中的端口进行通信.

I'm programming an Server-Client Software, where the client connects to the server with an port request, the server opens a new port and sends back the new port number. Then the client communicates with an RSA shared AES key encryption and the port from the port request.

嗯,应该是这样.

我已经运行了该程序,只有一个客户端可以连接到服务器,并且一切正常.但是现在我正在向服务器发送"portreq"字符串,该字符串应该给出端口回复.但是,当我检查时,如果传入的请求是"portreq",它将给我错误.如果我对.contains做同样的事情,它将给我真实的感觉.那是第一个问题,其次:

I had the program already running, only one client could connect to the server, and everything worked fine. But now I'm sending an "portreq" String to the server which should give a port reply. But when i check, if the incoming request is a "portreq" it gives me false. If i do same with .contains it gives me true. That's the first problem, and secondly:

当服务器将port-Integer转换为String并将其发送时,我无法在客户端再次将其转换为int:

When the server converts the port-Integer into an String and sends it, i can't transform it into a int again on the client-side:

java.lang.NumberFormatException: For input string: "6002������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at application.PowerPanelController.addDataToCpu(PowerPanelController.java:360)
    at application.PowerPanelController.lambda$0(PowerPanelController.java:143)
    at application.PowerPanelController$$Lambda$200/357277047.handle(Unknown Source)
    at com.sun.scenario.animation.shared.TimelineClipCore.visitKeyFrame(TimelineClipCore.java:226)
    at com.sun.scenario.animation.shared.TimelineClipCore.playTo(TimelineClipCore.java:167)
    at javafx.animation.Timeline.impl_playTo(Timeline.java:176)
    at javafx.animation.AnimationAccessorImpl.playTo(AnimationAccessorImpl.java:39)
    at com.sun.scenario.animation.shared.InfiniteClipEnvelope.timePulse(InfiniteClipEnvelope.java:110)
    at javafx.animation.Animation.impl_timePulse(Animation.java:1102)
    at javafx.animation.Animation$1.lambda$timePulse$25(Animation.java:186)
    at javafx.animation.Animation$1$$Lambda$186/1977464318.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at javafx.animation.Animation$1.timePulse(Animation.java:185)
    at com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(AbstractMasterTimer.java:344)
    at com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(AbstractMasterTimer.java:267)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:447)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:431)
    at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$363(QuantumToolkit.java:298)
    at com.sun.javafx.tk.quantum.QuantumToolkit$$Lambda$42/317723766.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)

我不明白,因为字符串看起来和我发送的字符串几乎一样.

Which i don't get because the String looks pretty much the same as i send it.

这是我的客户端发送和接收功能

Here's my client side send and receive function

private String sendCommandNoAES(String command)
{
    byte[] outData = new byte[1024];
    byte[] inD = new byte[1024];
    String message = "";
    try
    {
        // create Socket
        DatagramSocket socket = new DatagramSocket();
        // build Packet
        InetAddress serverIP = InetAddress.getByName(PowerPanelMain.ip);
        outData = command.getBytes();
        DatagramPacket out = new DatagramPacket(outData,outData.length, serverIP, PowerPanelMain.port); // send packet
        socket.send(out);
        logger.info("Sent an " + command + " Request non encrypted to " + PowerPanelMain.ip + ":" + PowerPanelMain.port);
        // Receive answer
        byte[] inData = new byte[1024];
        DatagramPacket in = new DatagramPacket(inData,inData.length);
        socket.receive(in);
        inD = in.getData();
        message = new String(inD,0,inD.length);
        // Close Socket
        socket.close();

        logger.info("Go answer " + message + " from " + in.getAddress().toString() + ":" + in.getPort());
    }
    catch(Exception ee)
    {
        logger.error("Error while requesting data from server \n" + getStackTrace(ee));
        return "offline";
    }
    return message;
}

这是我发送portreq的地方:

This is where i send the portreq:

String portString = sendCommandNoAES("portreq");
int port = Integer.parseInt(portString);

那是服务器代码:

byte[] inData = new byte[1024];
        byte[] outData = new byte[1024];
        String message;

        DatagramSocket socket = null;
        try
        {
            socket = new DatagramSocket(port);
            System.out.println("Bound to " + port);
            //JOptionPane.showMessageDialog(null, "Bound to " + String.valueOf(port), "Yeay", JOptionPane.OK_CANCEL_OPTION);
        }
        catch(Exception ee)
        {
            JOptionPane.showMessageDialog(null, "Error occured! \n#002", "Error #002", JOptionPane.OK_CANCEL_OPTION);
        }

...

DatagramPacket in = new DatagramPacket(inData,inData.length);
                socket.receive(in);
                InetAddress senderIP = in.getAddress();
                int senderPort = in.getPort();
                byte[] inc = in.getData();
                message = new String(inc,0,inc.length);

                System.out.println("Got " + message + " from " + senderIP + ":" + senderPort + " (byte array: " + inc.toString());

...

if(message.contains("portreq"))
                    {
                        System.out.println("Creatinfg answer");
                        outData = String.valueOf(portcount).getBytes();
                        DatagramPacket out = new DatagramPacket(outData,outData.length, senderIP, senderPort);
                        socket.send(out);

                        System.out.println("Sent " + String.valueOf(portcount));

                        UDPTraffic udpt = new UDPTraffic(portcount, this.mode);
                        udpt.start();
                        portcount++;
                    }

我真的很感谢能解决/解释我为什么这样的人:D

I'm really thankful to anyone who can solve/explain me why this is happening :D

推荐答案

inD = in.getData();

这只会重新声明相同的值.删除.

This merely reasserts the same value. Remove.

message = new String(inD,0,inD.length);

错了.您将忽略接收到的数据报的实际长度.更改为:

Wrong. You're ignoring the actual length of the received datagram. Change to:

message = new String(in.getData(), in.getOffset(), in.getLength());

这篇关于传输时UDP数据包内容更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆