Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > client-server data transfers appended with funny characters

Reply
Thread Tools

client-server data transfers appended with funny characters

 
 
Matt
Guest
Posts: n/a
 
      07-11-2004
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!
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-11-2004
On 10 Jul 2004 19:32:57 -0700, Matt <(E-Mail Removed)> wrote:

> 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!


I don't know anything about recv and send but I can easily see that you
have undefined behaviour in your code.


char recvBuf[100];
while (1)
{
rVal = recv(s, recvBuf, strlen(recvBuf), 0);

recvBuf is uninitialised data, yet the first thing you do is call
strlen(recvBuf). You are lucky that doesn't crash your program. Certainly
you will get a completely arbitary value from strlen(recvBuf).

Now as I said I don't know anything about recv, but I would *guess* that
the third parameter is meant to specify the size of the buffer passed in
the second parameter so that recv knows how much memory its got to play
with. Therefore I would try sizeof not strlen

char recvBuf[100];
while (1)
{
rVal = recv(s, recvBuf, sizeof recvBuf, 0);

If that doesn't work then RTFM.

john
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-11-2004
On 10 Jul 2004 19:32:57 -0700, Matt <(E-Mail Removed)> wrote:

> 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);
> }
>


More guesses about your code but I would imagine that you really want this

rVal = send(theSocket, sendBuf, strlen(sendBuf) + 1, 0);

by saying 'strlen(sendBuf) + 1' you are ensuring that the null byte at the
end of your string gets sent. Just a guess, again.

john
 
Reply With Quote
 
jota
Guest
Posts: n/a
 
      07-11-2004
> rVal = send(theSocket, sendBuf, strlen(sendBuf), 0);
> rVal = recv(s, recvBuf, strlen(recvBuf), 0);


There is two solutions
1. Client sends nullterminator. (strlen(sendBuf)+1)
2. Server appends nullterminator. (recvBuf[noreceived]=0)
//jota


 
Reply With Quote
 
Matt
Guest
Posts: n/a
 
      07-12-2004
thanks a lot!!

I just tried different ways, if I do the following

> > rVal = recv(s, recvBuf, 100, 0);


rather than

> > rVal = recv(s, recvBuf, strlen(recvBuf), 0);

or
> > rVal = recv(s, recvBuf, strlen(recvBuf)+1, 0);


Then, it is working fine. But I still don't quite understand the
rationale, because I thought strlen(recvBuf)+1 already represents the
packet length. 100 is the maximum length of the packet that I defined
for the recvBuf variable.

please advise. thanks!!
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-12-2004
On 11 Jul 2004 22:50:14 -0700, Matt <(E-Mail Removed)> wrote:

> thanks a lot!!
>
> I just tried different ways, if I do the following
>
>> > rVal = recv(s, recvBuf, 100, 0);

> rather than
>
>> > rVal = recv(s, recvBuf, strlen(recvBuf), 0);

> or
>> > rVal = recv(s, recvBuf, strlen(recvBuf)+1, 0);

>
> Then, it is working fine. But I still don't quite understand the
> rationale, because I thought strlen(recvBuf)+1 already represents the
> packet length. 100 is the maximum length of the packet that I defined
> for the recvBuf variable.
>


No, how can strlen(recvBuf)+1 represent the packet length when
strlen(recvBuf)+1 is called *before* you received the packet? At best
strlen(recvBuf)+1 will be the length of the *last* packet you received not
the packet you are *about* to receive.

In any case recv does not need to know the packet length, you've got that
wrong. It does need to know the size of the buffer you have given it
(which is nothing to do with the packet length). In your case the size of
the buffer is 100 bytes, so that is what you should put for the third
argument.

*After* you have called recv, then you have a packet, and then (depending
on what you sent) is might be sensible to call strlen(recvBuf)+1 to find
out how big the packet is. Although I think rVal will tell you the same
information.

But think about it, you cannot find out how long a packet is until *after*
you have received the packet, that seems obvious.

john
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XMLHttpRequest gets data to display in element but data gets appended not replaces previous! libsfan01 Javascript 5 12-20-2006 06:25 AM
Help- "cxc" mysteriously appended to aspx page name =?Utf-8?B?QWltZWU=?= ASP .Net 1 12-14-2004 02:37 PM
Appended extensions in Mozilla 1.7 Gary Nored Firefox 0 10-20-2004 02:25 PM
client-server data transfers appended with funny characters Matt C Programming 2 07-19-2004 05:23 AM
Re: Server couldnt notice object that i appended (ClientSide) ... Karl Seguin ASP .Net 1 07-28-2003 12:04 PM



Advertisments