• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. . .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic