Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization 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();
}
}
 
Joe Ess
Bartender
Pie
Posts: 9259
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea, closing a stream then trying to read from it again will tend to cause problems.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic