Solved

[Java] send Vector through TCP socket

Posted on 2007-04-08
6
1,684 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
Technology Partners: 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 250 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

733 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