473,834 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector<cha r> troubles

Hi

I'm writing a p2p client for an existing protocol.

I used a std::vector<cha r> as a buffer for messages read from the server.
The message length is the first 4 bytes. The message code the second 4.
The total message length is therefore 4 + message length.

A number of messages work fine/as expected but there are consistant
errors occuring. After a period
the message lengths and codes are very large numbers. Another important
(i think) point is that in the while loop, bytes
are only removed from the vector if a whole message exists. Yet on
occasion the messageLength calculated changes
between itterations that do not remove any data? how can this be so?

Is it maybe the case that i should be using std::vector<uns igned char>
to store the data?

The code i suspect is to blame is below. I can't work out if memory is
getting overwritten/used uninitialised. As i understand it
the erase() call is fine so each complete message is removed correctly.

Any help very welcome as i'm stuck as a big stuck thing at the minute.
Chris

// inbByteBuffer = std::vector<cha r>
| // if we have got the message length and code bytes
while (inbByteBuffer. size
<http://www.php.net/manual-lookup.php?lang =en&pattern=siz e>() >= 8) {
int messageLength = 0;
int msgCode = 0;

// get the message length (first 4)
for(int i=3; i>=0; i--) {
messageLength*= 256;
messageLength+= inbByteBuffer[i];
}

// get the message code (second 4)
for(int i=7; i>=4; i--) {
msgCode *=256;
msgCode += inbByteBuffer[i];
}

// check message is long enough
if ((messageLength +4) > inbByteBuffer.s ize
<http://www.php.net/manual-lookup.php?lang =en&pattern=siz e>()) {
return false;
}

// create the bytes specific to this message
std::vector<cha r> data;
for(int i=0; i<messageLength +4; i++) {
char c = inbByteBuffer[i];
data.push_back
<http://www.php.net/manual-lookup.php?lang =en&pattern=pus h_back>(c);
}

// the buffer is the correct size, create a message with it
try {
Message * pmsg = MessageFactory: :buildMessage
<http://www.php.net/manual-lookup.php?lang =en&pattern=bui ldMessage>(data );

// add it to the inbound queue
inbMessageQueue .push
<http://www.php.net/manual-lookup.php?lang =en&pattern=pus h>(pmsg);
} catch
<http://www.php.net/manual-lookup.php?lang =en&pattern=cat ch>
(logic_error e) {
cout << "Invalid message exception" << endl;
}

// get the iterator
std::vector<cha r>::iterator it = inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>();

// remove the items from the inbByteBuffer
for(int i=0; i<= messageLength+4 && inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>() !=
inbByteBuffer.e nd
<http://www.php.net/manual-lookup.php?lang =en&pattern=end >();i++) {
it = inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>();
inbByteBuffer.e rase
<http://www.php.net/manual-lookup.php?lang =en&pattern=era se>(it);
}
}

|
Jul 22 '05 #1
2 4786

"Chris Thompson" <ch************ @tascomi.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hi

I'm writing a p2p client for an existing protocol.

I used a std::vector<cha r> as a buffer for messages read from the server.
The message length is the first 4 bytes. The message code the second 4.
The total message length is therefore 4 + message length.

A number of messages work fine/as expected but there are consistant
errors occuring. After a period
the message lengths and codes are very large numbers. Another important
(i think) point is that in the while loop, bytes
are only removed from the vector if a whole message exists. Yet on
occasion the messageLength calculated changes
between itterations that do not remove any data? how can this be so?

Is it maybe the case that i should be using std::vector<uns igned char>
to store the data?
Hard to say, but if not you should certainly cast to unsigned char in your
msgLength and msgCode calculations.

// get the message length (first 4)
for(int i=3; i>=0; i--) {
messageLength*= 256;
messageLength+= static_cast<uns igned char>(inbByteBu ffer[i]);
}

The code i suspect is to blame is below. I can't work out if memory is
getting overwritten/used uninitialised. As i understand it
the erase() call is fine so each complete message is removed correctly.

Any help very welcome as i'm stuck as a big stuck thing at the minute.
Chris

[snip]

// get the iterator
std::vector<cha r>::iterator it = inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>();

// remove the items from the inbByteBuffer
for(int i=0; i<= messageLength+4 && inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>() !=
inbByteBuffer.e nd
<http://www.php.net/manual-lookup.php?lang =en&pattern=end >();i++) {
it = inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>();
inbByteBuffer.e rase
<http://www.php.net/manual-lookup.php?lang =en&pattern=era se>(it);
}


This loop is wrong,

For a start it should say

for(int i=0; i< messageLength+4 && inbByteBuffer.b egin

i.e. < not <=

But even then it is indescribably inefficient because you are copying the
whole vector repeatedly back over itself for each character you delete.

Try this, it's simpler and more efficient.

inbByteBuffer.e rase(inbByteBuf fer.begin(), inbByteBuffer.b egin() +
messageLength + 4);

I don't believe you need to check if the buffer is long enough to delete the
message since you already make that check earlier in the code.

john
Jul 22 '05 #2
Excellent!! thanks a bunch john.

Regards
Chris

John Harrison wrote:
"Chris Thompson" <ch************ @tascomi.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hi

I'm writing a p2p client for an existing protocol.

I used a std::vector<cha r> as a buffer for messages read from the server.
The message length is the first 4 bytes. The message code the second 4.
The total message length is therefore 4 + message length.

A number of messages work fine/as expected but there are consistant
errors occuring. After a period
the message lengths and codes are very large numbers. Another important
(i think) point is that in the while loop, bytes
are only removed from the vector if a whole message exists. Yet on
occasion the messageLength calculated changes
between itterations that do not remove any data? how can this be so?

Is it maybe the case that i should be using std::vector<uns igned char>
to store the data?

Hard to say, but if not you should certainly cast to unsigned char in your
msgLength and msgCode calculations.

// get the message length (first 4)
for(int i=3; i>=0; i--) {
messageLength*= 256;
messageLength+= static_cast<uns igned char>(inbByteBu ffer[i]);
}

The code i suspect is to blame is below. I can't work out if memory is
getting overwritten/used uninitialised. As i understand it
the erase() call is fine so each complete message is removed correctly.

Any help very welcome as i'm stuck as a big stuck thing at the minute.
Chris

[snip]

// get the iterator
std::vector<cha r>::iterator it = inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>();

// remove the items from the inbByteBuffer
for(int i=0; i<= messageLength+4 && inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>() !=
inbByteBuffer .end
<http://www.php.net/manual-lookup.php?lang =en&pattern=end >();i++) {
it = inbByteBuffer.b egin
<http://www.php.net/manual-lookup.php?lang =en&pattern=beg in>();
inbByteBuffer.e rase
<http://www.php.net/manual-lookup.php?lang =en&pattern=era se>(it);
}

This loop is wrong,

For a start it should say

for(int i=0; i< messageLength+4 && inbByteBuffer.b egin

i.e. < not <=

But even then it is indescribably inefficient because you are copying the
whole vector repeatedly back over itself for each character you delete.

Try this, it's simpler and more efficient.

inbByteBuffer.e rase(inbByteBuf fer.begin(), inbByteBuffer.b egin() +
messageLength + 4);

I don't believe you need to check if the buffer is long enough to delete the
message since you already make that check earlier in the code.

john

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1894
by: Steven Reddie | last post by:
I have a large char which I'd like to copy a portion of into a vector<char>. I can't seem to locate a simple method for doing so. I assume involving iterators will not result in memcpy not being used and therefore will be less than optimal. Can anyone help me out? Regards, Steven
3
2810
by: Chris | last post by:
Hi, I'm playing/developing a simple p2p file sharing client for an existing protocol. A 2 second description of the protocol is Message Length = int, 4 bytes Message Code = int, 4 bytes Data
6
4138
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
13
4658
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar, bar); }
8
4326
by: Marco Costa | last post by:
Hello all, I wrote a simple ODBC wrapper class that used code like this ( not real code, added types for clarification ): char** type bufs = new char* for( int i = 0 ; i < numberOfColumns ; i++ ) { size = getSizeOfColumn(); bufs = new char;
4
2392
by: Jim Langston | last post by:
I'm using a function like this: char TextBuffer; jGet_DropDown_Selected_Text( cc.ddSex, TextBuffer); Where the function is filling in the text buffer. I don't have access to the actual function to change it to a std::string (it's a library function) so I was thinking, well, I could use a std::vector<charinstead of a char array, but would that actually gain me anything at all? I mean, what's the difference between calling it like the...
6
5659
by: Bobrick | last post by:
Hi. Thanks to everyone who replied to my last post, it turns out it wasn't the line where I was trying to treat the variable in question as an array which was the problem, but the line above. char temp; std::vector<unsigned charmmessage; while (!done){
6
2770
by: JackC | last post by:
Hi, If i have a vector<charmaybe 15mb in size, whats the most efficient way to write out all the elements to an output file? I have tried this: for(int z = 0; z < Output_Buffer.size(); z++) { write(ostr, (char *) Output_Buffer, 1);
0
9799
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10515
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10554
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10224
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5799
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4428
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3985
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3084
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.