aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time " Watch "I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time " New topic
Author

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

zeedan khalid
Greenhorn

Joined: Mar 18, 2004
Posts: 9
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

Joined: Dec 09, 2003
Posts: 90
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

Joined: Oct 29, 2001
Posts: 8705
    
    6

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. . .


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time
 
Similar Threads
change it into frame, kindly see the source code.
I Got a Problem in ObjectInputStream while iam sending Vector 2nd Time
inputStream freezes
ObjectInputStream problems "connection reset"
little chat program