別のスレッドでリッスンするUDPリスナーを作成しようとしています。最初は正常に動作しますが、接続を停止してから再開すると、エラーが発生します。
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;
}
次のエラーが発生します。
logUpdatingThreadが同期ブロックに入りました!!! java.net.SocketException:認識されないWindowsソケットエラー:0:java.net.PlainDatagramSocketImpl.bind(不明なソース)のjava.net.PlainDatagramSocketImpl.bind0(ネイティブメソッド)でバインドできません
これはsock.recieve(pack);の行を指します。何らかの理由でソケットが閉じていないようです。これは、sock.recieve(pack)で待機していて、whileループから抜け出してソケットを閉じることがないためだと思います。どうすればこれを回避できますか?
ありがとう