How to empty the variable buffer in recv() before accepting string

A

Aditya

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 incoming
string is filled into it, there are trailing junk characters also.

The code portion refering to it is somewhat like:

int rcvStatus;
char *buffer;
rcvStatus = recv(socketDescriptor, buffer, flags);
std::cout<< buffer << std::endl;

If I initialize the buffer variable to NULL or "0", the recv() returns
-1, that is it doesn't accept the incoming string.
So, Please help me to clean the buffer variable, before it is being
filled with the incoming string.

Thanks
Aditya
 
D

Dave Vandervies

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.)
 
K

Keith Thompson

Aditya said:
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 incoming
string is filled into it, there are trailing junk characters also.

recv() is not a standard C function. Try comp.unix.programmer.
The code portion refering to it is somewhat like:

int rcvStatus;
char *buffer;
rcvStatus = recv(socketDescriptor, buffer, flags);
std::cout<< buffer << std::endl;

And that's C++, not C.

[snip]
 
T

Tor Rustad

Aditya said:
Hi
I am using recv() from socket.h in one of my TCP-client projects.

Socket programming is *OFF-TOPIC*!

The problem is that the buffer variable in recv(socketDescriptor,
buffer, flags) points to some stray location and when the incoming
string is filled into it, there are trailing junk characters also.

The code portion refering to it is somewhat like:

int rcvStatus;
char *buffer;
rcvStatus = recv(socketDescriptor, buffer, flags);

You must be kidding???

Do you really think the recv() API is broken like this? IF recv() really
did the memory allocation for you, you must have passed in the address
of 'buffer'!

Consult your man page for recv(), before posting more crap and while at
it, order Stevens "Network Programming":

http://www.amazon.com/UNIX-Network-Programming-Interprocess-Communications/dp/0130810819


std::cout<< buffer << std::endl;

Grrrrrrrr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top