Solved

[Java] send Vector through TCP socket

Posted on 2007-04-08
6
1,658 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java 8 lambda expresssions exception handling 3 90
more than one jdk and one jre 1 41
jboss wildfly 10.1 10 89
Adding multiple JVM environments to RedHat 6 7 20
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

770 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