クライアント接続ごとに新しいスレッドを作成し、処理するデータを探すために、各スレッドのストリームのブロッキング読み取りを継続します。
class Server {
ClientThread threads[];
int size;
ServerSocket serverSocket;
boolean active;
Server() throws Exception {
/* initialize connection */
active = true;
listen();
}
void listen() throws Exception {
while (active) {
Socket clientSocket = serverSocket.accept();
threads[ size++ ] = new ClientThread(clientSocket);
threads[ size - 1 ].start();
}
}
}
class ClientThread extends Thread {
OutputStream out;
InputStream in;
boolean active;
ClientThread(Socket clientSocket) throws Exception {
out = clientSocket.getOutputStream();
in = clientSocket.getInputStream();
}
public void run() {
active = true;
while (active) {
listen();
}
}
private void listen() {
try {
int res = process(in.read());
out.write(res);
} catch (Exception e) {}
}
private int process(int b) {
return -1;
}
}
必要条件はsocket.Anywayでどうやってサーブレットで行うのか教えてください。 – bharathi