📄 tcpechoserver.java
字号:
// Sample Java Program// TCP Echo Server//// Based partially on the code in TCP/IP Sockets in Java, by Calvert and Donahoo//// Requires a single command line arg - the port numberimport java.net.*; // need this for InetAddress, Socket, ServerSocketimport java.io.*; // need this for I/O stuff public class TCPEchoServer { // define a constant used as size of buffer static final int BUFSIZE=1024; // main starts things rolling static public void main(String args[]) { if (args.length != 1) { throw new IllegalArgumentException("Must specify a port!"); } int port = Integer.parseInt(args[0]); try { // Create Server Socket (passive socket) ServerSocket ss = new ServerSocket(port); while (true) { Socket s = ss.accept(); handleClient(s); } } catch (IOException e) { System.out.println("Fatal I/O Error !"); System.exit(0); } } // this method handles one client // declared as throwing IOException - this means it throws // up to the calling method (who must handle it!) // try taking out the "throws IOException" and compiling, // the compiler will tell us we need to deal with this! static void handleClient(Socket s) throws IOException { byte[] buff = new byte[BUFSIZE]; int bytesread = 0; // print out client's address System.out.println("Connection from " + s.getInetAddress().getHostAddress()); // Set up streams InputStream in = s.getInputStream(); OutputStream out = s.getOutputStream(); // read/write loop while ((bytesread = in.read(buff)) != -1) { out.write(buff,0,bytesread); } s.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -