私のメインスレッドでは、私のServerSocketオブジェクトでaccept()を呼び出し、新しいクライアントスレッドを開始し、新しいクライアントが受け入れられます。 しかし、while(listen)ループブロックでaccept()を呼び出すと、接続を閉じるのが苦労します。私の下のサンプルコードを見つけてください、サーバー側プログラムの下のソケット接続を閉じるためのラッパーサービスを作成する方法はありますか?マルチスレッドソケット - 正常にソケットを閉じることができません
public static final void serverSocket(IData pipeline) throws ServiceException {
int clientNumber = 0;
ServerSocket listener = null;
int port =0;
Properties socketProperties = new Properties();
try
{
listener = new ServerSocket(port);
System.out.println(listener.getLocalSocketAddress());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(true){
new Connections(listener.accept(), clientNumber++).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
}
finally{
try {
listener.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static class Connections extends Thread {
private Socket socket;
private int clientNumber;
public Connections(Socket socket, int clientNumber) {
this.socket = socket;
this.clientNumber = clientNumber;
log("New connection with client# " + clientNumber + " at " + socket);
}
public void run()
{
try {
// Send a welcome message to the client.
while (true)
{
// my program
}
catch (IOException e) {
//exception
}
finally {
try {
socket.close();
}
catch (IOException e) {
log.error("Couldn't close a socket, what's going on?");
}
log("Connection with client# " + clientNumber + " closed");
}
}
いずれの提案も大歓迎です。
'Connections'オブジェクトが何をやろうとしているのか、EOSに遭遇したとき、または使用しているソケットで致命的な' IOException'が起きたときに接続を閉じるべきです。例はたくさんあります。問題が何であるか不明です。 – EJP