我觉得用ServerSocket应该放在一个独立的线程中运行,而不是在main方法中。

解决方案 »

  1.   

    首先对象要序列化
      class Mine implements Serializable{
      }发送时用writeObject(),接受时用(Mine)readObject()
      

  2.   

    我把我编的一个相似的例子程序给你,希望能对你有借鉴作用.
    //服务器端程序
    //DaytimeServerimport java.net.*;
    import java.io.*;
    import java.util.*;public class DaytimeServer {
      private ServerSocket ss;  public static void main(String [] args) throws IOException {
        DaytimeServer ds = new DaytimeServer();
        ds.go();
      }  public void go() throws IOException {
        Socket s = null;
        ss = new ServerSocket(DaytimeClient.DAYTIME_PORT, 1);
        System.out.println("Daytime Server running...");
        while(true) {
          s = ss.accept();
          BufferedWriter out = new BufferedWriter(
                           new OutputStreamWriter(s.getOutputStream()));
         out.write("Java Daytime Server: "+ 
                     (new Date()).toString() + "\n"); 
         out.close();
         s.close();
         System.out.println("one client request processed.");
        }
      }
    }
    //客户端程序
    //DaytimeClientimport java.net.*;
    import java.io.*;public class DaytimeClient {
      public static final int DAYTIME_PORT = 13;
      String host;
      Socket s;
      
      public static void main(String [] args) throws IOException {
         if (args.length < 1) {
           usage();
           System.exit(1);
         }
         DaytimeClient dc = new DaytimeClient(args[0]);
         dc.go();
      }
      
      public DaytimeClient(String host) throws IOException{
        this.host = host;
      }
      
      public void go() throws IOException {
        s = new Socket(host, DAYTIME_PORT);
        BufferedReader in = new BufferedReader(
                         new InputStreamReader(s.getInputStream()));
        System.out.println(in.readLine());
        in.close();
        s.close();
      }
      
      public static void usage() {
        System.out.println("USAGE :");
        System.out.println("java DaytimeClient hostname");
      }
    }
      

  3.   

    add a line to send.java
              out.writeInt(11);
    //socket automatically buffer the content
    //you can also change this with the socket option
                out.flush();
      

  4.   

    cloudwindbase:
    你的这个程序应该只适合字符串的传输吧
    不能传输克序列化的对象sharetop(天生不笨):我的这个只是个测试,实际应用是我是把它放到一个循环中
    因为accept()会自动堵塞,线程很难调试wavecheng(CryingOwl)
    基本的数据类型都是可以序列化的 zdt(大海)
    不需要刷新吧欢迎大家讨论