?
Solved

[Java] send Vector through TCP socket

Posted on 2007-04-08
6
Medium Priority
?
1,723 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
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…
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 …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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