简体   繁体   English

在udp套接字上设置源端口

[英]Setting source port on udp socket

We've developed a Java application that sends several UDP datagrams to the very same destination IP address. 我们已经开发了一个Java应用程序,该应用程序将多个UDP数据报发送到相同的目标IP地址。 Obviously the destination port is always the same one, but we can't seem to be able to set the source port to remain the same on each datagram. 显然,目标端口始终是相同的端口,但是我们似乎无法设置源端口在每个数据报上保持相同。
If the data package we send is bigger than MTU the package is properly splitted over the same source port, but once we send a new data package the source port changes, generating a new firewall session (which network admin has warned us to be very bad due to the amount of sessions the application is generating). 如果我们发送的数据包大于MTU,则该包会在相同的源端口上正确拆分,但是一旦我们发送了新的数据包,源端口就会更改,从而生成新的防火墙会话(网络管理员警告我们这很糟糕(取决于应用程式产生的工作阶段数量)。

Right now we're sending the packages with the following statement: 现在,我们将使用以下语句发送软件包:

We've tried several approaches and the result is always the same, we can't seem to be able to set the source port to a fixed value. 我们尝试了几种方法,结果始终是相同的,我们似乎无法将源端口设置为固定值。

Edit- pasting actual code: 编辑粘贴实际代码:

private boolean sendImage(byte[] imageData, InetAddress address,
int port) throws UnknownHostException, SocketException {

    boolean ret = false;


    DatagramSocket socket = null;

    try {
        socket = new DatagramSocket();
        DatagramPacket dp = new DatagramPacket(imageData, imageData.length, address, PUERTO_UDP);
        socket.send(dp);
        ret = true;
    } catch (IOException e) {
        Main.lanzarExcepcion(e);
        ret = false;
    } finally {
        if (socket != null) {
             socket.close();
        }
    }

    return ret;
}

Thanks for your time! 谢谢你的时间!

I think your issue is that you are closing the actual DatagramSocket instead of reusing the same , and simply call socket.send(DatagramPacket) . 我认为您的问题是,您正在关闭实际的DatagramSocket而不是重复使用相同的数据,只需调用socket.send(DatagramPacket) If you cant keep the client socket open , then you could even define the clients port like : DatagramSocket clientSocket = new DatagramSocket(9743); 如果您不能保持客户端套接字打开,那么您甚至可以定义客户端端口,例如: DatagramSocket clientSocket = new DatagramSocket(9743); so each time you are calling a new clientSocket , it will get the same port or it will throw a java.net.BindException: Address already in use: Cannot bind 因此,每次调用新的clientSocket时,它将获得相同的端口,否则将抛出java.net.BindException: Address already in use: Cannot bind

This will not solve the network sessions issue, because you are opening a new UDP Socket. 这将无法解决网络会话问题,因为您正在打开新的UDP套接字。 Also i suspect that your network admin , sees the previous sessions , because you are not closing the UDP Sockets at all , but simply spawning them 我也怀疑您的网络管理员看到了以前的会话,因为您根本没有关闭UDP套接字,而只是生成它们

The source port is an ephemeral port, generated for you by the underlying networking implementation. 源端口是一个临时端口,由基础网络实现为您生成。 There is no reason to set it to a particular port number. 没有理由将其设置为特定的端口号。

As mentioned by @AntJavaDev 如@AntJavaDev所述

The solution was to: 解决方案是:

1.- keep the DatagramSocket open 2.- pass src port in the arguments 3.- reusing the unclosed DatagramSocket for every new data packet to the same destination! 1.-使DatagramSocket保持打开状态2.-在参数中传递src端口3.-对于到达相同目的地的每个新数据包,重新使用未关闭的DatagramSocket!

Thanks all! 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM