试试这个
// The server:
import java.net.*;
import java.io.*;
import java.util.Date;/**
* A class send out date infomation through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeServer {
    public static final int PORT = 1300;    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(PORT);
            Socket connection = null;            while (true) { // using a while (true) loop
                try
                {
                    connection = server.accept();
                    OutputStreamWriter out =
                      new OutputStreamWriter(connection.getOutputStream());
                    Date now = new Date();
                    out.write(now.toString() + "\r\n");
                    out.flush();
                    connection.close();
                } catch (IOException ioe) {
                    System.err.println(ioe);
                } finally {
                    try {
                        if (connection != null)
                            connection.close();
                    } catch (IOException e) {}
                }
            }
        } catch (IOException ee) {
            System.err.println(ee);
        }
    } // end main
} // end DaytimeServer
// The client
import java.net.*;
import java.io.*;/**
* A class to receive time infomation from server through socket.
*
* @author: java8964
* @version: 1
*/
public class DaytimeClient {
    /**
    * The port which client and server make connection.
    */
    public static final int PORT = 1300;    public static void main(String[] args) {
        String hostName;
        if (args.length > 0) {
            hostName = args[0];
        } else {
            hostName = "localhost";
        }        try {
            Socket clientSocket = new Socket(hostName, PORT);
            InputStream timeStream = clientSocket.getInputStream();
            StringBuffer time = new StringBuffer();
            int c;
            while ((c = timeStream.read()) != -1) {
                time.append((char)c);
            }
            String timeString = time.toString().trim();
            System.out.println("It is " + timeString + " at " + hostName);
        } catch (UnknownHostException ukhe) {
            System.err.println(ukhe);
        } catch (IOException ioe) {
            System.err.println(ioe);
        }
    } // end main
} // end DaytimeClient