daytimeserver.java
来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 54 行
JAVA
54 行
import java.net.*;
import java.io.*;
// Chapter 6, Listing 2
public class DaytimeServer
{
public static final int SERVICE_PORT = 13;
public static void main(String args[])
{
try
{
// Bind to the service port, to grant clients access to
// the TCP daytime service
ServerSocket server = new ServerSocket (SERVICE_PORT);
System.out.println ("Daytime service started");
// Loop indefinitely, accepting clients
for (;;)
{
// Get the next TCP client
Socket nextClient = server.accept();
// Display connection details
System.out.println ("Received request from " +
nextClient.getInetAddress() + ":" + nextClient.getPort() );
// Don't read, just write the message
OutputStream out = nextClient.getOutputStream();
PrintStream pout = new PrintStream (out);
// Write the current date out to the user
pout.print( new java.util.Date() );
// Flush unsent bytes
out.flush();
// Close the connection
nextClient.close();
}
}
catch (BindException be)
{
System.err.println ("Service already running on port " + SERVICE_PORT );
}
catch (IOException ioe)
{
System.err.println ("I/O error - " + ioe);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?