简体   繁体   English

udp服务器上的Java中已经使用的地址错误

[英]address already in use error in java on udp server

I have an application that does many things. 我有一个可以执行许多操作的应用程序。 I'd like to add a method that enables this application to behave like a udp server on a socket. 我想添加一个方法,使该应用程序的行为类似于套接字上的udp服务器。 In particular it listens on the localhost address on port 8888. I tried implementing this behaviour with the following code but i get an Address already in use error . 特别是它在端口8888上的本地主机地址上进行侦听。我尝试使用以下代码实现此行为,但我得到一个Address already in use errorAddress already in use error Plus the whole application is stuck on this udp server method. 另外,整个应用程序都停留在此udp服务器方法上。 I guess its probably due to the fact that its all running on one thread. 我猜可能是由于它全部在一个线程上运行。

1) Can you please show me how to correct my method. 1)您能告诉我如何更正我的方法吗? In particular how to make this udp server listener start on a new thread. 特别是如何使此udp服务器侦听器在新线程上启动。

2) This server will listen forever for pks from the client. 2)该服务器将永远侦听来自客户端的pk。 Depending on wether the server receveid a specific packet or not it needs to do certain things. 根据服务器是否接收特定的数据包,它是否需要执行某些操作。 Is the logic correct: if the packetReceived is != null and the packet has not been processed then process it. 逻辑是否正确:如果packetReceived为!= null,并且尚未处理数据包,则对其进行处理。 repeat forever (as show in the code)? 永远重复一次(如代码中所示)?

public void startSocketListening(){
        byte[] receiveData = new byte[1024];
        DatagramPacket receivePacket;
        System.out.println("Waiting to receive...");
        while (true) {
            receivePacket = new DatagramPacket(receiveData, receiveData.length);
            try {
                DatagramSocket serverSocket = new DatagramSocket(8888);
                serverSocket.receive(receivePacket);
                //if i receive a packet and it doesn't already have a flow rule process it
                if ((receivePacket != null) && (newOFRuleAdded == false)){
                    this.rlocAddress = new String(receivePacket.getData());
                    System.out.println("RECEIVED: " + rlocAddress);
                    System.out.println("RLOC: " + rlocAddress);
                    //process the message
                    newOFRuleAdded = true;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

For 1.) First move the socket creation (serverSocket = new DatagramSocket(8888);) out of the loop. 对于1.),首先将套接字创建(serverSocket = new DatagramSocket(8888);)移出循环。 That is where your bind failed errors come from. 这就是绑定失败错误的来源。

Best put your whole startSocketListening() code in the run() method of a class extending Thread or implementing Runnable (see code below for threadexample) 最好将整个startSocketListening()代码放在扩展Thread或实现Runnable的类的run()方法中(有关线程示例,请参见下面的代码)

For 2.) there might be a misunderstanding. 对于2.)可能会有误解。 "serverSocket.receive(receivePacket);" “ serverSocket.receive(receivePacket);” will wait till a packet is received. 将等到收到数据包。 So whatever comes later on is for the newly received packet that has never been processed. 因此,以后发生的任何事情都是针对从未处理过的新接收到的数据包。 The while loop just gets you back to waiting for a new packet. while循环只会使您回到等待新数据包的状态。

Something like this might do. 这样的事情可能会做。 If you are unsure debug it step by step how it behaves when a packet is received. 如果不确定调试,请逐步调试它在收到数据包时的行为。

public class UdpListener extends Thread
{
    public void run()
    {
        byte[] receiveData = new byte[1024];
        DatagramPacket receivePacket;
        System.out.println("Waiting to receive...");
        DatagramSocket serverSocket;
        try {
            serverSocket = new DatagramSocket(8888);    
            while (true) {
                receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);
                //if i receive a packet and it doesn't already have a flow rule process it
                if ((receivePacket != null) ){
                    System.out.println("First byte of received package is: " + receivePacket.getData()[0]);
                }
            }
        } catch (SocketException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Called like this: 这样称呼:

Thread t = new UdpListener();
t.start();

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

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