【问题标题】:error sending client object to server将客户端对象发送到服务器时出错
【发布时间】:2014-07-21 01:17:31
【问题描述】:

我无法通过客户端向服务器发送对象,因为当我运行客户端时,服务器程序崩溃让我出现“连接重置”错误

这是客户:

    public class Client {

    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException{
        Socket s = new Socket("192.168.1.3", 4444);
        ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

        Group g = new Group("EXAMPLE");
        String command = "ADD_GROUP";
        System.out.println("Sending: " + command);
        oos.writeUTF(command);
        oos.flush();
        oos.writeObject(g);
        oos.flush();
        s.close();
     }

}

这是服务器:

    public class Server{

    public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException, ParseException
    {

       ServerSocket server = new ServerSocket(4444);

       System.out.println("Waiting for clients to connect...");

       SimpleDataSource.init("database.properties");
       Network net = new Network();

       while (true)
       {
          Socket s = server.accept();
          InetAddress clientAddress = s.getInetAddress();
          System.out.println("Incoming connection from: " + clientAddress.getHostName() + "[" + clientAddress.getHostAddress() + "]");

          ServiceClass service = new ServiceClass(s,net);
          service.doService();

       }
    }
}

这是 ServiceClass 类:

    public class ServiceClass{

    private Socket s;
    private Network net;

    public ServiceClass(Socket s, Network net){
        this.s = s;
        this.net = net;
    }

    public void doService() throws IOException, SQLException, ClassNotFoundException, ParseException
    {
       ObjectInputStream ois = new ObjectInputStream(s.getInputStream());

       while(true){

            String line = (String) ois.readUTF();
            System.out.println("Received: " + line);

            if(line.equals("ADD_GROUP")){
                Group group = (Group) ois.readObject();
                net.addGroup(group);
            }
        }
    }
}

当我运行服务器时,它等待客户端连接,当我运行我的客户端程序时,服务器崩溃给我这个错误:

    Waiting for clients to connect...
Incoming connection from: UNKNOWN_DEVICE[192.168.1.3]
Received: ADD_GROUP
Error: Group already exists
Exception in thread "main" java.io.EOFException
    at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:340)
    at java.io.ObjectInputStream$BlockDataInputStream.readUnsignedShort(ObjectInputStream.java:2808)
    at java.io.ObjectInputStream$BlockDataInputStream.readUTF(ObjectInputStream.java:2864)
    at java.io.ObjectInputStream.readUTF(ObjectInputStream.java:1072)
    at social_network.ServiceClass.doService(ServiceClass.java:35)
    at social_network.Server.main(Server.java:39)
Java Result: 1

这是怎么回事?

【问题讨论】:

  • 从导致问题的服务器类中删除 s.close(); 后重试。
  • @Braj 不,它不会导致错误。服务器在读取时收到“连接重置”异常。它永远不会到达 a.close() 行。
  • 我已经成功了,我已经用新的错误日志编辑了我的代码
  • @Braj 已经找到了。代码对我来说已经足够完整了。

标签: java object client send


【解决方案1】:

客户端没有关闭套接字,因此服务器在尝试读取不存在的第二个字符串时可以获得连接重置而不是 EOFException。

当你解决这个问题时,EOFException 意味着没有什么可读的了。这不是问题。你只要抓住它,然后打破。

【讨论】:

    猜你喜欢
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    相关资源
    最近更新 更多