Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
EchoServer.java EchoServer.java Below is the syntax highlighted version of EchoServer.java from §8.4 Operating Systems. /****************************************************************************** * Compilation: javac EchoServer.java * Execution: java EchoServer port * Dependencies: In.java Out.java * * Runs an echo server which listents for connections on port 4444, * and echoes back whatever is sent to it. * * * % java EchoServer 4444 * * * Limitations * ----------- * The server is not multi-threaded, so at most one client can connect * at a time. * ******************************************************************************/ import java.net.Socket; import java.net.ServerSocket; public class EchoServer { public static void main(String[] args) throws Exception { // create socket int port = 4444; ServerSocket serverSocket = new ServerSocket(port); System.err.println("Started server on port " + port); // repeatedly wait for connections, and process while (true) { // a "blocking" call which waits until a connection is requested Socket clientSocket = serverSocket.accept(); System.err.println("Accepted connection from client"); // open up IO streams In in = new In (clientSocket); Out out = new Out(clientSocket); // waits for data and reads it in until connection dies // readLine() blocks until the server receives a new line from client String s; while ((s = in.readLine()) != null) { out.println(s); } // close IO streams, then socket System.err.println("Closing connection with client"); out.close(); in.close(); clientSocket.close(); } } } Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne. Last updated: Fri Oct 20 14:12:12 EDT 2017.