Created
May 22, 2018 12:15
-
-
Save mike-neck/2125baf54c3f0719fc170bc026f04ee6 to your computer and use it in GitHub Desktop.
java.net パッケージによる echo サーバー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EchoServer { | |
public static void main(String[] args) { | |
var serverSocket = new ServerSocket(8000); //(1) | |
while(true) { | |
Socket socket = serverSocket.accept(); // (2) | |
var reader = toBufferedReader(socket.getInputStream()); | |
var writer = toPrintWriter(socket.getOutputStream()); | |
String line; | |
while ((line = reader.readLine()) != null && !line.isEmpty()) { //(3) & (4) | |
writer.println(line); // (5) | |
} | |
socket.close(); // (6) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment