473,396 Members | 2,026 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,396 software developers and data experts.

client-server data transfers appended with funny characters

I wrote the tcp socket client-server program that the server will
echo the message received from the client.

In client program:
char sendBuf[100];
while(1)
{
cout << "Enter message:";
cin.getline(sendBuf,100);
rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
}

In server program:
char recvBuf[100];
while (1)
{
rVal = recv(s, recvBuf, strlen(recvBuf), 0);
cout << "Echo: " << recvBuf << endl; //appends garable charcters
cout << recvBuf << endl;
//try to clear the buffer first for every echo, but it turned out
//couldn't get any data from client
//strcpy(recvBuf,""); <---------------------
}

The problem is recvBuf variable is appended with funny characters for
every
echo. And I think I need to clean up the buffer for every echo;
otherwise
it will append with previous data. The output of recvBuf is like this:

Echo: hello world ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦`?
Echo: erere world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
Echo: weee world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
^C

If I initialize recvBuf as follows, then server program is not able to
get data from client

strcpy(recvBuf,"");
any workarounds to the problem? please advise!! thanks!
Nov 14 '05 #1
2 2150
jr********@hotmail.com (Matt) writes:
{
cout << "Enter message:";
cin.getline(sendBuf,100);
rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
}


This is not C.
Nov 14 '05 #2
On 10 Jul 2004 19:32:31 -0700, jr********@hotmail.com (Matt) wrote:
I wrote the tcp socket client-server program that the server will
echo the message received from the client.
"Echo" normally means to send back to the client. What you would be
doing except for your bugs is displaying or outputting the message.
Sockets as such are not part of standard C (or for that matter C++)
and thus offtopic, but your actual errors are in the C part of what
you did.
In client program:
char sendBuf[100];
while(1)
{
cout << "Enter message:";
cin.getline(sendBuf,100);
This is C++ and off topic, except that it does (normally) leave a
valid, that is null-byte-terminated, string in sendBuf, which the
following and also offtopic send() therefore sends correctly.
rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
}

In server program:
char recvBuf[100];
This is a local or "automatic" variable with no initializer, and so is
not initialized; it may contain arbitrary garbage.
while (1)
{
rVal = recv(s, recvBuf, strlen(recvBuf), 0);
The first time through, strlen() uses the unitialized contents of
recvBuf to try to determine its length; technically this is Undefined
Behavior and anything is permitted happen up to and including turning
your fingers green, but in practice it will probably just produce some
wrong value, which is passed to recv(). On each subsequent loop it
tries to use the previous contents to determine the length, but since
recv does not add a null terminator and neither does your code it will
still be wrong, though not (additional) Undefined Behavior.

Probably this value will be too small, in which case you may fail to
recv() all the data as the sender sent -- and note that for TCP this
may happen anyway, TCP is a byte-stream protocol and does NOT
guarantee to preserve "message" boundaries, although for short
isolated messages usually it will "by accident" and you won't discover
your code is badly wrong until it's embedded in a large, complicated,
and deployed program and much more difficult and costly to repair.
Possibly the strlen() value is too big, in which case *if* the sender
sends too much data, which your *sample* client won't but an attacker
will, it can crash or "r00t" your system. Either way it's a bad idea.

What you want to use is sizeof(recvBuf) or perhaps that -1, see below
-- but only if the array declaration is visible as here, not if it's
within a function to which you pass it as a parameter because then the
size is NOT known or automatically passed, you must either pass it or
have some other means of determining it.

recv() is offtopic except for the fact that it, barring error, returns
the number of characters stored but does NOT add a null terminator.
cout << "Echo: " << recvBuf << endl; //appends garable charcters
cout << recvBuf << endl;
Again the C++ part is offtopic except for the fact that it expects a
null-terminated string which you haven't given it. You could add the
null-terminator, which is more convenient for C, in which case you
should have received only sizeof(recvBuf)-1 to ensure room, or
alternatively allocate the buffer for say [MAXREAD+1] and recv for
MAXREAD. Or use basic_ostream::write or construct a std::string for
the valid length and output that; see comp.lang.c++ for those.
//try to clear the buffer first for every echo, but it turned out
//couldn't get any data from client
//strcpy(recvBuf,""); <---------------------
}

The problem is recvBuf variable is appended with funny characters for
every
echo. And I think I need to clean up the buffer for every echo;
otherwise
it will append with previous data. The output of recvBuf is like this:

Echo: hello world ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦`?
Echo: erere world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
Echo: weee world¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦ ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦
¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦?
^C

If I initialize recvBuf as follows, then server program is not able to
get data from client

strcpy(recvBuf,"");
Right, because now strlen() reliably returns 0 and you try to recv at
most 0 bytes; see above.

any workarounds to the problem? please advise!! thanks!


- David.Thompson1 at worldnet.att.net
Nov 14 '05 #3

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

Similar topics

7
by: Chris | last post by:
<apologies for cross-posting> Hi All, I am based in the UK and have been doing some private work for a client which involved setting up a database and scripts to search it and display results...
1
by: Justin Stockton | last post by:
I recently upgraded from ActivePython 2.2.2 to ActivePython 2.3.2 and I'm running into an issue importing the win32com.client module. Before installing the new version, I made sure to properly...
2
by: Rhino | last post by:
I am trying to verify that I correctly understand something I saw in the DB2 Information Center. I am running DB2 Personal Edition V8.2.1 on Windows. I came across the following in the Info...
9
by: Harry Smith | last post by:
While reading the documentation on IsStartupScriptRegistered, there is a reference to "client startup script" as "Determines if the client startup script is registered with the Page object." What...
11
by: pshindle | last post by:
We have several machines currently running the DB2 V7 Run-time Client that we would like to actually be running the App Dev Client. To 'upgrade' (within the same version) this client software can...
2
by: J Huntley Palmer | last post by:
I am having a horrific time integrating uw-imap's c-client for imap support in php. The problem is a whole bunch of "Text relocation remains referenced against symbol" errors during linking....
11
by: Wayne | last post by:
I am a one man enterprise and have been asked by a prospective client what happens to their database regarding ongoing changes etc if I get hit by a bus. Obviously my databases are distributed...
1
by: WebServiceSecurity | last post by:
The issue involves the following technologies: - 1. .NET 2.0 Framework 2. WSE2.0 (WS-Security) 3. X.509 certificates 4. BEA Weblogic 8.1.5
3
by: rjha94 | last post by:
Hi I just installed the runtime client on my windows machine. when i go inside the SQLLIB\bin folder i can see db2.exe. is it possible to use this db2 bundled with runtime client for command line...
4
MMcCarthy
by: MMcCarthy | last post by:
http://bytes.com/images/howtos/projectscope_blocks.jpgAs a freelance IT consultant for over 10 years, I’ve come to appreciate well defined project scopes. A project scope is a common understanding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.