MessageServerクラスをセットアップして、別のリクエストで各クライアントにサービスを提供しようとしています(これはかなり直感的です)。Java - 各クライアントを別々のスレッドで処理するサーバー?
どうすればよいですか?
import java.net.*;
import java.io.*;
public class MessageServer {
public static final int PORT = 6100;
public static void main(String[] args) {
Socket client = null;
ServerSocket sock = null;
BufferedReader reader = null;
try {
sock = new ServerSocket(PORT);
// now listen for connections
while (true) {
client = sock.accept();
reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
Message message = new MessageImpl(reader.readLine());
// set the appropriate character counts
message.setCounts();
// now serialize the object and write it to the socket
ObjectOutputStream soos = new ObjectOutputStream(client.getOutputStream());
soos.writeObject(message);
System.out.println("wrote message to the socket");
client.close();
}
}
catch (IOException ioe) {
System.err.println(ioe);
}
}
}
私は自分のオペレーティングシステムのテキストブックで検索していますが、読み込みに問題はありませんが、それほど多くはありません。/ –
時間がかかる部分はありますか?あなたはテストしましたか?どんな負荷の下でどれくらいのスピードを得る必要がありますか?早期の最適化はすべての悪の根源です。これを実行する前に何をしようとしているのかを必ず確認してください。 (例えば、多くの場合、スレッディングが遅くなる可能性があります) –