List<PrintStream> nouts = new ArrayList<>();
boolean isRun = true;
ServerSocket svrSocket = new ServerSocket(10000);
while (isRun) {
Socket socket = svrSocket.accept();
new Thread(new Runnable() {
public void run() {
try {
InputStream nis = socket.getInputStream();
OutputStream nos = socket.getOutputStream();
ReadWriteLock lock = new ReentrantReadWriteLock();
PrintStream nout = new PrintStream(nos, true);
lock.writeLock().lock();
nouts.add(nout);
lock.writeLock().unlock();
Scanner nscan = new Scanner(nis);
String msg;
do {
msg = nscan.nextLine();
System.out.println(msg);
lock.readLock().lock();
for (int i = 0; i < nouts.size(); i++) {
nouts.get(i).println("나됴니서버:" + msg);
}
lock.readLock().unlock();
} while (!msg.equals("bye"));
nscan.close();
nout.close();
nos.close();
nis.close();
socket.close();
} catch (IOException e) {
}
}
}).start();
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.