集册 Java实例教程 创建时间服务器以提供时间值

创建时间服务器以提供时间值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

505
创建时间服务器以提供时间值

import java.io.BufferedOutputStream;

import java.io.IOException;

import java.io.PrintWriter;//时代Java 提 供

import java.net.ServerSocket;

import java.net.Socket;

import java.util.Date;


public class Main extends Thread {

  private ServerSocket sock;


  public Main() {

    super();

    try {
    /*来自 
     时 代 J     a    v  a - nowjava.com*/

      sock = new ServerSocket(4415);

      System.out.println("TimeServer running ...");

    } catch (IOException e) {

      System.out.println("Error: couldn't create socket.");

      System.exit(1);

    }

  }


  public void run() {

    Socket client = null;


    while (true) {

      if (sock == null)

        return;

      try {

        client = sock.accept();

        BufferedOutputStream bos = new BufferedOutputStream(

            client.getOutputStream());

        PrintWriter os = new PrintWriter(bos, false);

        Date now = new Date();

        os.println(now);

        os.flush();

        os.close();

        client.close();

      } catch (IOException e) {

        System.out.prin
展开阅读全文