In article <11**********************@x35g2000prf.googlegroups .com>,
Aditya <ad************@gmail.comwrote:
>Hi
I am using recv() from socket.h in one of my TCP-client projects.
recv(), socket.h, and TCP clients are not defined by the C language and
are therefore beyond the scope of comp.lang.c. It does appear, though,
that your problem is actually C-related (probably by accident), so:
>The problem is that the buffer variable in recv(socketDescriptor,
buffer, flags)
I think you're either getting the interface for recv wrong or using a
socket API that's completely and irredeemably broken.
Shouldn't there be some way to tell it how many bytes you're expecting
it to read?
points to some stray location and when the incoming
string is filled into it, there are trailing junk characters also.
recv is, if it behaves like any sane socket read would (but see above),
similar to fread in its behavior: It reads a series of bytes, not a
string. If you want a string, you need to null-terminate it yourself.
One way to do this is to zero-fill the buffer before calling it; another
is to look at how much you actually read and write a null terminator
for the string at the end of that. In either case you need to make sure
there's enough room in your buffer for it.
>The code portion refering to it is somewhat like:
If you have a problem with your code, post the actual code you're having
problems with. Not "something like" it.
>int rcvStatus;
char *buffer;
rcvStatus = recv(socketDescriptor, buffer, flags);
I certainly hope that the code you're actually trying to use is somewhat
less broken than this. Where is buffer pointing when you give it to recv?
>std::cout<< buffer << std::endl;
This is not C.
dave
(posting, for the first time somewhere more widely read than uw.test,
using a new trn I built to replace the one that vanished from the
somebody-else's-hobby-project server I read news on; please let me know
if something looks odd.)
--
Dave Vandervies
dj******@csclub.uwaterloo.ca
I've taken a vow of poverty. To annoy me, send me money.