⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daytimeserver.java

📁 Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用安全
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -