Reputation: 93
I am new in java socket programming.
My problem: I have server put in while loop to keep working.
The server has many functions to do ... one of them is adding some data.
I serialize object of a class in client then send it to the server. it done successfully but when i tried to repeat this step during the working client program i receive error java.io.EOFException
my client code:
// 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();
my server code:
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.
Upvotes: 3
Views: 92
Reputation: 310859
You're losing data in the buffered reader. It buffers, including all or part of the following object. Get rid of the buffered reader/writer altogether. You need to use the Object streams for everything. Use writeUTF()
to send the request, readUTF()
to read it, and then the read/writeObject()
methods you're already using.
Upvotes: 1