473,471 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

recv not completely overwriting last string

Hi,

Is it me or is the recv() function not erasing but only overwriting the last
data it supplied?

example:

when i first send:

"login robert test"
and then try
"getstatus robert"

i see that it recieved:
"getstatus robertt"

I've noticed this before with some other strings using recv

This is a piece of code i am using:

char buf[256]; /* buffer for client data */

if ((aant_bytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
.....
Do i have to erase the content before the next loop or something?

Thanks in Advance,

Robert
Nov 13 '05 #1
9 3248
Robert <R.****@hetnet.nl> spoke thus:
Is it me or is the recv() function not erasing but only overwriting the last
data it supplied? Do i have to erase the content before the next loop or something?


Possibly - what you *do* have to do is ask somewhere where people know about
this. Try comp.unix.programmer.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #2
>Hi,

Is it me or is the recv() function not erasing but only overwriting the last
data it supplied?

example:

when i first send:

"login robert test"
and then try
"getstatus robert"

i see that it recieved:
"getstatus robertt"

I've noticed this before with some other strings using recv

This is a piece of code i am using:

char buf[256]; /* buffer for client data */

if ((aant_bytes = recv(i, buf, sizeof(buf) - 1, 0)) <= 0) {
.....
}
else
{
buf[aant_bytes] = 0;
}
Nov 13 '05 #3

"Robert" <R.****@hetnet.nl> wrote in message
news:bn**********@reader08.wxs.nl...
Hi,

Is it me or is the recv() function not erasing but only overwriting the last data it supplied?


Um yeah. Just like any other read function it only writes as many bytes as
it reads.

Try memseting your buffer to zero before the read. This also has the
benefit of assuring a NULL byte is present [provided you cap your read to
size -1 bytes].

Tom
Nov 13 '05 #4
"Tom St Denis" <to********@iahu.ca> wrote in message
news:Kp******************@twister01.bloor.is.net.c able.rogers.com...

"Robert" <R.****@hetnet.nl> wrote in message
news:bn**********@reader08.wxs.nl...
Hi,

Is it me or is the recv() function not erasing but only overwriting the last
data it supplied?


Um yeah. Just like any other read function it only writes as many bytes

as it reads.

Try memseting your buffer to zero before the read. This also has the
benefit of assuring a NULL byte is present [provided you cap your read to
size -1 bytes].

Didn't someone say that '\0' (NUL) is not same as NULL? :-) But
seriously, memset()ing (or appending '\0') won't help, generally. Stream
of data may contain embedded '\0's, and receiving end should work
with it as such. As John indicated, in case of success number of "bytes"
received is returned by recv(), which is sufficient information to perform
further processing. All str*() calls on receiving buffer are unusable in
traditional sense, and memsetting (buffer) or terminating (data in the
buffer) will only prevent one case of UB, but won't guarantee correct
results. Using size of data in buf and mem*() functions IMHO will
facilitate better results without overhead of memsetting.
Just hope I wasn't too OT.
Nov 13 '05 #5
"nobody" <no****@nowhere.non> wrote in message
news:ei*******************@news04.bloor.is.net.cab le.rogers.com...
"Tom St Denis" <to********@iahu.ca> wrote in message
news:Kp******************@twister01.bloor.is.net.c able.rogers.com...

"Robert" <R.****@hetnet.nl> wrote in message
news:bn**********@reader08.wxs.nl...
Hi,

Is it me or is the recv() function not erasing but only overwriting the

last
data it supplied?


Um yeah. Just like any other read function it only writes as many bytes

as
it reads.

Try memseting your buffer to zero before the read. This also has the
benefit of assuring a NULL byte is present [provided you cap your read to
size -1 bytes].

Didn't someone say that '\0' (NUL) is not same as NULL? :-) But
seriously, memset()ing (or appending '\0') won't help, generally. Stream
of data may contain embedded '\0's, and receiving end should work
with it as such. As John indicated, in case of success number of "bytes"
received is returned by recv(), which is sufficient information to perform
further processing. All str*() calls on receiving buffer are unusable in
traditional sense, and memsetting (buffer) or terminating (data in the
buffer) will only prevent one case of UB, but won't guarantee correct
results. Using size of data in buf and mem*() functions IMHO will
facilitate better results without overhead of memsetting.
Just hope I wasn't too OT.


Your points are valid. Looking for '\0' is a major
design flaw in such apps that cause the dreaded
"buffer overflaw" exploitation.

Design a packet structure that uses an explicit
packet type number and packet length. Verify the
type and length upon receipt. Character data in
the packet should have a field containing the
actual length of the character data. Try to plan
for the various ways that someone could spoof you
with bad type codes, bad lengths, imbedded null
bytes, etc. Write defensive logic.
Nov 13 '05 #6

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:bn**********@chessie.cirr.com...
Robert <R.****@hetnet.nl> spoke thus:
Is it me or is the recv() function not erasing but only overwriting the last data it supplied?
Do i have to erase the content before the next loop or something?


Possibly - what you *do* have to do is ask somewhere where people know

about this. Try comp.unix.programmer.


Well, it is pretty much true in C that when a function returns the number of
characters written to a buffer you should use that number. In any case
where '\0' is a legal character the normal string length methods don't work.
This is true on unix, windows, or any other C environment.

-- glen
Nov 13 '05 #7
Robert wrote:
Hi,

Is it me or is the recv() function not erasing but only overwriting
the last data it supplied?

example:

when i first send:

"login robert test" and then try "getstatus robert"

i see that it recieved: "getstatus robertt"

I've noticed this before with some other strings using recv

This is a piece of code i am using:

char buf[256]; /* buffer for client data */

if ((aant_bytes = recv(i, buf, sizeof(buf), 0)) <= 0) { ..... Do i
have to erase the content before the next loop or something?

Thanks in Advance,

Robert

You have to clarify the protocol: either the 2 sides exchange
zero-terminated strings, in which case it seems pretty clear that your
server-side is wrong: it sends strlen(msg) characters, thus the recv
does NOT get the zero byte;

or, they exchange arrays of bytes, in which case you have to transmit
the length too (I assume you are not using UDP), and append the zero
byte afterwards on the receiver side.

HtH,
--
Michel Bardiaux
Peaktime Belgium S.A. Bd. du Souverain, 191 B-1160 Bruxelles
Tel : +32 2 790.29.41

Nov 13 '05 #8

"Michel Bardiaux" <mb*******@peaktime.be> wrote in message
news:3F**************@peaktime.be...
Robert wrote:
> Hi,
>
> Is it me or is the recv() function not erasing but only overwriting
> the last data it supplied?

(snip)
You have to clarify the protocol: either the 2 sides exchange
zero-terminated strings, in which case it seems pretty clear that your
server-side is wrong: it sends strlen(msg) characters, thus the recv
does NOT get the zero byte;

or, they exchange arrays of bytes, in which case you have to transmit
the length too (I assume you are not using UDP), and append the zero
byte afterwards on the receiver side.


To get it a little more on topic, the fread() function will have the same
problem.

For TCP one can't depend on the record boundaries being maintained, so null
terminated strings would not be recommended.

-- glen
Nov 13 '05 #9
"Tom St Denis" <to********@iahu.ca> writes:
"Robert" <R.****@hetnet.nl> wrote in message
news:bn**********@reader08.wxs.nl...
Hi,

Is it me or is the recv() function not erasing but only overwriting the

last
data it supplied?


Um yeah. Just like any other read function it only writes as many bytes as
it reads.

Try memseting your buffer to zero before the read. This also has the
benefit of assuring a NULL byte is present [provided you cap your read to
size -1 bytes].


Zeroing the buffer won't do any good if the incoming data can include
null characters ('\0').

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #10

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

Similar topics

8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
6
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue,...
8
by: Yuk Tang | last post by:
I am tearing my hair out over this, since I can't see what I'm doing wrong (duh, if I knew, I wouldn't be asking the question). I am adding Field items to a Field Collection, but for some reason...
5
by: sonu | last post by:
Hi, I m trying to receive a message on windows sockets using recv() function. The message is such that the first two bytes of the message represent the length of the message. Hence when trying...
4
by: Aditya | last post by:
Hi I am using recv() from socket.h in one of my TCP-client projects. The problem is that the buffer variable in recv(socketDescriptor, buffer, flags) points to some stray location and when the...
4
by: Aditya | last post by:
Hi I am using recv() from socket.h in one of my TCP-client projects. The problem is that the buffer variable in recv(socketDescriptor, buffer, flags) points to some stray location and when the...
10
by: Hendrik van Rooyen | last post by:
While doing a netstring implementation I noticed that if you build a record up using socket's recv(1), then when you close the remote end down, the recv(1) hangs, despite having a short time out...
1
by: Rumburak | last post by:
Hello everybody, I have a problem receiveing data on a TCP socket. I am sending a binary struct over and when receiveing it I found a lot o bytes containg 0 inside at the begining. Practically I...
3
by: stonefunker | last post by:
I've been playing around with the concept of wiping files from a thumbdrive and it seems to be working pretty well. I have the code below and the file seems to be overwritten as they should. I...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.