473,513 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to empty the variable buffer in recv() before accepting a 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 7236
the following will work:
int len = 1024;
char buffer[1024 + 1];
len = recv(socketDescriptor, buffer, len,flags);
if (len 0){
//there is actual data received.
buffer[len] = '\0';
//process the data in buffer.
}else if (len == 0){
//the connection has closed.
}else{
//unknown error occur.
}
On 5ÔÂ29ÈÕ, ÏÂÎç12ʱ55·Ö, Aditya <adityagupta...@gmail.comwrote:
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 #2
On May 28, 9:55 pm, Aditya <adityagupta...@gmail.comwrote:
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);
What is the size of the buffer? Typically you should allocate a
buffer of some size either on the stack or heap and then pass this
size to recv as the 3rd arg. recv on success returns the number of
bytes received. You should then read only those many bytes from the
buffer passed to recv. Hope his helps.
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 #3
Aditya wrote:
Hi
I am using recv() from socket.h in one of my TCP-client projects.
Please don't multi-post on Usenet, as you where told elsewhere, this
belongs on comp.unix.programmer.

--
Ian Collins.
May 29 '07 #4
On May 29, 10:18 am, Archie <nraut...@gmail.comwrote:
On May 28, 9:55 pm, Aditya <adityagupta...@gmail.comwrote:
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);

What is the size of the buffer? Typically you should allocate a
buffer of some size either on the stack or heap and then pass this
size to recv as the 3rd arg. recv on success returns the number of
bytes received. You should then read only those many bytes from the
buffer passed to recv. Hope his helps.
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- Hide quoted text -

- Show quoted text -
Hi
Thanks
That Helps....

May 29 '07 #5

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

Similar topics

1
5012
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
8862
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
1834
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
30333
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
39315
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
3333
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
3562
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
34452
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
13201
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
7259
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
7158
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...
1
7098
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...
0
5683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5085
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
4745
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
3232
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
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
455
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...

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.