This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time

 
zeedan khalid
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got a problem with ObjectInputStream while iam sending Vector but in case of String it works corrects
following is the code
/////////////////////server side//////////////////////////
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class server extends JFrame implements ActionListener
{
JButton but=new JButton("hello");
JTextField jf=new JTextField(15);
ObjectOutputStream os;
Vector v=new Vector();
public server()
{
but.addActionListener(this);
Container c=getContentPane();
c.setLayout(new FlowLayout());
c.add(but);
c.add(jf);
setSize(300,300);
show();
try
{
ServerSocket ss=new ServerSocket(9000);
Socket s=ss.accept();
ObjectInputStream is=new ObjectInputStream(s.getInputStream());
System.out.println(is.readObject());
os=new ObjectOutputStream(s.getOutputStream());
}catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public void actionPerformed(ActionEvent e)
{
v.add(jf.getText());
try
{
os.flush();
// os.writeObject(jf.getText());/////////it works correct
os.writeObject(v);///////// it does not work correct
}catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public static void main(String args[])
{
new server();
}
}
//////////////////////////////////clinent side code/////////////////////////////////////////////////////////////
import java.net.*;
import java.util.*;
import java.io.*;
class client
{
public client()
{
try
{
Socket s=new Socket("localhost",9000);
ObjectOutputStream os=new ObjectOutputStream(s.getOutputStream());
os.writeObject("Client Connected");
while (true)
{
ObjectInputStream is=new ObjectInputStream(s.getInputStream());
System.out.println(is.readObject());// if write String it work properly in case of vector it cuase problem
is.close();
}
}catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
public static void main(String agr[])
{
new client();
}
}
 
Murad Iqbal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you didnt tell what problems are you facing. Kindly elaborate. If your code is compiling properly, then I guess the problem is the way you use the Vector object. You cannot just print it out to see it contents. Following is the code that you have to use if it is a Vector object
//if a vector is received
for(int i=0;i>v.size();i++)
{
System.out.println(v.elementAt(i));
}
//provided all the elements in the vector are Strings or printable data types.

Muhammad Murad Iqbal
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is a dupe of this post. I told zeedan that his problem was that he was closing the input stream then trying to read from it again and that was his problem, but he didn't listen. You see, when you create an ObjectInputStream it places a header into the stream and this header is read by the initialization code of the ObjectOutputStream. Close the OutputStream, open another and BAM! bad header error. But try telling kids today that.
Our friend would do well to read the Serialization FAQ, especially the part about serializing an object (like, say, a Vector), writing to the ObjectOutputStream, then modifying it and re-writing it. He may save himself another post. . .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic