Solved

[Java] send Vector through TCP socket

Posted on 2007-04-08
6
1,607 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now