代码之家  ›  专栏  ›  技术社区  ›  Mike

插座未正确闭合

  •  2
  • Mike  · 技术社区  · 15 年前

    I am trying to create a UDP listener that will listen on a separate thread. It works fine the first time but when I stop the connection and then start it again it gives me errors.

    listenerRunnable = new Runnable() {
            public void run() {
                //This thread will listen keep listening to the UDP traffic and put it to the log source string
                try {
                    sock = new DatagramSocket(portNumber);
                } catch (SocketException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                while(keepListening) {
                    try {
                        pack = new DatagramPacket(recievedData, BUFFERSIZE);
                        sock.receive(pack);
    
                        String data = new String(pack.getData(), 0, pack.getLength());
                        addToLog(data);
                        System.out.println(data);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    sock.close();
                }
            }
        };
    
    /**
     * Function to start the listening thread.
     */
    public void startListening(int portNum) {
        keepListening = true;
        portNumber = portNum;
    
        listenerThread = new Thread(listenerRunnable);
    
        logSource_buffer = "";
        logSourcehtml_buffer = "";
        logSourcehtml_temp = "";
        ipListIndex_beg = 0;
        ipListIndex_end = -1;
    
        if(!listenerThread.isAlive()) {
            listenerThread.start();
        }
    }
    
    /**
     * stops the listening thead.  When the listening thread sees that keepListening is set to false
     * it will reach the end of its loop, close the socket, and the thread will die.
     */
    public void stopListening() {
        keepListening = false;
    }
    

    它给出了以下错误:

    logupdingthread已进入同步块!!!! java.net.socketException:无法识别的Windows套接字错误:0:无法绑定 在Java.NET.PLANDATAGAMSOCKETDIMP.BDI0(原生方法) 在Java.NET.PLANDATAGAMSOCKETDIMPL.BIN(未知源)

    指向带有sock.recieve(pack)的行; It seems like for some reason the socket isn't closing because, I think, its waiting at sock.recieve(pack) and never gets out of the while loop to close the socket. How would I get around this though?

    谢谢

    2 回复  |  直到 15 年前
        1
  •  1
  •   Peter Tillemans    15 年前

    You'll have to add a setSoTimeout(timeout) before calling receive. This will regularly throw SocketTimeoutExceptions, but keeping the Datagram socket open. This will allow you to regularly check the loop variable.

    Additionally you should move the loop inside the first try-catch block and add a finally block to close the socket.

    像:

            try {
                sock = new DatagramSocket(portNumber);
                sock.setSoTimeout(250);
                while(keepListening) {
                    try {
                        pack = new DatagramPacket(recievedData, BUFFERSIZE);
                        sock.receive(pack);
    
                        String data = new String(pack.getData(), 0, pack.getLength());
                        addToLog(data);
                        System.out.println(data);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            } catch (SocketException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } finally {
                sock.close();
            }
    
        2
  •  2
  •   Nick Smith    15 年前

    As Peter Tillemans said, you should set a receive timeout so that you're not sitting there trying to receive() for ever.

    另外,保留由返回的线程对象 new Thread(listenerRunnable) 以便stopListening()方法可以等待线程终止:

    public void stopListening() {
        keepListening = false;
        listenerThread.join();
    }