Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

[Java] send Vector through TCP socket

Posted on 2007-04-08
6
Medium Priority
?
1,752 Views
Last Modified: 2012-06-27
Dear all,

I'm writing a Java program to send a vector through TCP socket. On client side, user can click a button "Send" to send a vector to server side without limitation (means allow to click many times). On the server side, receive the vector and store into database. In my case, it's work when the first clicking on client side, however, the content of vector on server side is unchange no matter click the button how many times on client side...it seems only received the first vector sent by client...
any expert know why and how to solve it? Thanks a lot !

Code as follows:
<Client side--method call in the button>

//dout = new DataOutputStream( socket.getOutputStream() );
//oos = new ObjectOutputStream(dout);
protected void sendRecord (Vector vr){
      try {
                oos.writeObject(vr);
                oos.flush();                
     } catch( IOException ie ) {    System.out.println( ie );}
}


<Sever side>

public void run() {
                 try{
             din =new DataInputStream(socket.getInputStream());
             ois = new ObjectInputStream(din);

             while (true) {
                  try{
                                v1 = (Vector) ois.readObject();
                                System.out.println("Vector to string :" + v1.toString());
                  }catch (IOException ioe){      break;      }
                   catch (IOException ee) {      break;      }
                   catch (ClassNotFoundException cnfe){      break;      }
              }
                 } catch( EOFException ie ) { }
                    catch (IOException ee) { }
}

<Server Output when click the button on client side>
Vector to string : [abc, cde] // Input abc & cde and add into vector on client side
Vector to string : [abc, cde] // Input xyz & opq and add into vector on client side
Vector to string : [abc, cde] // Input nothing
.............
 

0
Comment
Question by:adidaz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:Jaax
ID: 18871894
I think it is cos the you are not reading from the socket afresh. You are re-reading what has already been fetched from the dataInputStream.

Try this:
In the try inside the while, include the data fetching from the socket
while(true){
try{
 din =new DataInputStream(socket.getInputStream());
  ois = new ObjectInputStream(din);
  v1 = (Vector) ois.readObject();
  System.out.println("Vector to string :" + v1.toString());
....



0
 

Author Comment

by:adidaz
ID: 18871925
Thanks Jaax, i tried before and exception occur (I think it's because using different input stream).
In fact, i have tried to send a string object instead of vector and result is fine. Code as follows:

<Client side>
protected void sendRecord (String recs){
      try {
                oos.writeObject(recs);
                oos.flush();                
     } catch( IOException ie ) {    System.out.println( ie );}
}

<Server side>
....
while (true) {
                  try{
                                recInString = (String) ois.readObject();
                                System.out.println("String :" + recInString);
                  }catch (IOException ioe){      break;      }
.....

<Server Output when click the button on client side>
String : abc_cde  // Input abc_cde on client side
String : xyz_opq  // Input xyz_opq on client side

Don't know why it's not work when using vector....
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 18871998
create a copy of the vector and send to the server!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:adidaz
ID: 18872024
ksivananth, do you mean i need revise the code on client side as follows:

<Client side>
//Vector v2 = new Vector();
protected void sendRecord (Vector vr){
     this.v2.clear();    
     this.v2 = vr;
      try {
                oos.writeObject(v2);
                oos.flush();                
     } catch( IOException ie ) {    System.out.println( ie );}
}

Tried and same result...
I print the result on client side and output is fine always.
Anyway, thanks.
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 1000 total points
ID: 18873039
try this,

protected void sendRecord (Vector vr){
     Vector v2 = new Vector() ;
    Enumeration e = vr.elements();
    while( e.hasMoreElements() ) v2.addElement( e.nextElement() );
      try {
                oos.writeObject(v2);
                oos.flush();                
     } catch( IOException ie ) {    System.out.println( ie );}
}
0
 

Author Comment

by:adidaz
ID: 18873127
oic...I only copy the pointers...I understand your point now.
This is exactly what i need. Thanks a lot.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question