473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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

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

May 29 '07 #1
4 3308
Aditya <ad************@gmail.comwrites:
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]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
May 29 '07 #2
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.
May 29 '07 #3
Aditya wrote:
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-P.../dp/0130810819
std::cout<< buffer << std::endl;
Grrrrrrrr

--
Tor <torust [at] online [dot] no>
May 29 '07 #4
On May 29, 1:55 pm, Tor Rustad <tor_rus...@hotmail.comwrote:
Aditya wrote:
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-P...cess-Communica...
std::cout<< buffer << std::endl;

Grrrrrrrr

--
Tor <torust [at] online [dot] no>
Thanks!!!!

May 29 '07 #5

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

Similar topics

1
by: Wayno | last post by:
My php logs are coming up empty. I have done all I can think of, and all that made sense to me. Can someone take a look at my php.ini please and tell me what you think may be the problem. I...
3
by: tornado | last post by:
Hi all, I am pretty new to PHP. I was reading PHP manual and trying out the example from 2nd chapter (A simple Tutorial). When i try to print the variable as given in the example it returns...
2
by: Robert A. van Ginkel | last post by:
Hello Fellow Developer, This looks like a long mail, but at the end of this post is my socket wrapper attached. I want to make a timeout procedure that starts counting down after the...
5
by: daniel.shaya | last post by:
I'll try and keep this brief so in a nutshell: I have large distributed java system running on a Windows 2003 server (4cpu 8Gb memory). Periodically the following exceptions occurs in the...
6
by: ppuniversal | last post by:
Can anyone tell me how to flush a socket stream in C++. I am making an Client Server application in C++ and use the send() and recv() functions. i want to flush the socket buffer as i am getting...
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...
3
by: deepak nayak | last post by:
hi, We have Primary / Secondary Client / Server architecture. The server is supposed to receive data from Primary client but ignore the Secondary Client data. I have to somehow ignore the...
10
by: John Salerno | last post by:
I wrote some pretty basic socket programming again, but I'm still confused about what's happening with the buffer_size variable. Here are the server and client programs: -------------- from...
3
by: Giampaolo Rodola' | last post by:
Hi, I'd like to know if there's a way to determine which is the best buffer size to use when you have to send() and recv() some data over the network. I have an FTP server application which, on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.