Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem in using Vector!!!

Status
Not open for further replies.

arunmnair

Programmer
Sep 11, 2003
1
NZ
I have a problem in my socket program(trying to implement sliding window algorithm). Details are given below:
I have declared a static member variable ServerWindow of type Vector in my socket program. I am adding all the packets receiving from the client to this vector. But whenever I am trying to add a new packet, it simply overwrites all the records in the vector. So my vector when displayed has the last packet for all elements. I have checked the received packet and its contents are OK.

public void receive(DatagramPacket p) throws IOException {
boolean KeepRunning = true;
boolean KeepGoing = true;
ServerWindow = new Vector();

while(KeepRunning){
try{
while (KeepGoing){
if (ServerWindow.size() <= 7){
super.receive(p);
if (isCorrectCRC(p)){//checking CRC
ServerWindow.add(p);
}
}
if (ServerWindow.size() == 7){
for (i=0;i<ServerWindow.size();i++){
System.out.println(&quot;In receive(): &quot; + i + &quot; &quot; + new String( (byte[])ServerWindow.get(i)));
}
}
}

}catch(SocketTimeoutException e){
} //end of catch
} // end of while
} // end of receive method
 
I'm guessing that everytime you recieve a packet, you call 'receive'.

Every time you do, you create a new ServerWindow vector - hence destroying previous contents.

If you create a new instance of ServerWindow where you declare it, at class level, then you should be OK ...
 
ServerWindow vector has to be at Class Level Member variable and no need of Two Loops only one Loop will Work.



regards,
Abhijeet Mahalkar
INDUS PUNE




 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top