【问题标题】:EOF when serializing an object in java?在java中序列化对象时出现EOF?
【发布时间】:2015-12-20 00:31:30
【问题描述】:

我是 Java 套接字编程的新手。

我的问题:我已将服务器放入 while 循环以继续工作。

服务器有很多功能要做......其中之一就是添加一些数据。

我在客户端序列化一个类的对象,然后将其发送到服务器。它成功完成,但是当我尝试在工作客户端程序期间重复此步骤时,我收到错误java.io.EOFException

我的客户代码:

// some data put in object named (p) of class (Product).

Socket s = new Socket("localhost",4000);
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("Add Product");
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(p);
out.close();

我的服务器代码:

int i = 0;
ServerSocket s1 = new ServerSocket(4000); 
String request = null;
while(i == 0)
{
Socket ss = s1.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(ss.getInputStream()));
request = in.readLine();

switch(request){
case "Add Product":
ObjectInputStream inn = new ObjectInputStream(ss.getInputStream());
Product pr = (Product)inn.readObject();
inn.close();
e.add_product(pr);
break;

} // end of switch case.
} // end of while loop.

【问题讨论】:

    标签: java sockets serialization


    【解决方案1】:

    您正在丢失缓冲阅读器中的数据。它缓冲,包括以下对象的全部或部分。完全摆脱缓冲的读取器/写入器。您需要对所有内容使用对象流。使用writeUTF() 发送请求,使用readUTF() 读取它,然后使用您已经在使用的read/writeObject() 方法。

    【讨论】:

    • 哇……你太棒了,先生……效果很好:) .. +1
    猜你喜欢
    • 2010-10-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多