LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2004, 07:24 AM   #1
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Rep: Reputation: 0
empty buffer


Hi,

can anyone tell me how to clean buffer in a TCP client server architecture?

I have a situation where client send() much more info than server recv(), thus on specific occasions I want client to empty() buffer from all msgs it sent before and only than send the new msg.

how can I do it?

thanks
 
Old 09-19-2004, 05:12 PM   #2
chort
Senior Member
 
Registered: Jul 2003
Location: Silicon Valley, USA
Distribution: OpenBSD 4.6, OS X 10.6.2, CentOS 4 & 5
Posts: 3,660

Rep: Reputation: 76
Hi there. While your question is related to networking, what you're really asking is for some programming advice and for that, it's more appropriate in the Programming forum. You should get an answer much faster, here.
 
Old 09-19-2004, 06:49 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Are you aware that one send() on one side of the connection doesn't necessarily correspond to one recv() on the other? i.e., does the rest of the data come in if you do some more recv()s?

Any TCP application needs to have some sort of protocol so that it knows where each "message" begins and ends, for whatever "message" means to that application.

IOW, TCP just gives you a stream of bytes, not a sequence of discrete messages (in the view of the application). Moreover, recv() may give you fewer bytes than you asked for even though there are more "on the way", either because they are still on the wire (or the other machine) or because of the whims of your kernel. Your app should deal with any such case, including, if it helps to think about it, the extreme case where you only get 1 byte per recv().

I hope I've answered the question you are actually asking. Apologies if you aren't new to programming with TCP; this issue comes up a lot with people who are.
 
Old 09-20-2004, 01:25 AM   #4
cynthia
LQ Newbie
 
Registered: Sep 2004
Posts: 20

Original Poster
Rep: Reputation: 0
Question

well, even though I'm new I do know that recv()/ send() does not necessarily = 1.

but, let's assume that each msg sent by client equals MSG_SIZE, and the same regarding every msg recv() by the server (even if few recv() are needed) - my question still stays - is it possible for client to clean buffer from all previous bytes it sent before?

if no, I tried to have the server empty the buffer in a loop (so, it will receive the last msg sent) but it didn't work...here is the code - I would appreciate any ideas what is the problem:

Code:
while (recv(..., buf,...) != -1)
         {
                   strcpy(lastMsg, buf);
         }
I hoped that at the end of this loop I'll receive the last msg sent...but my program crushed

Last edited by cynthia; 09-20-2004 at 01:28 AM.
 
Old 09-20-2004, 10:29 AM   #5
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
The reason I answered the way I did is that afaik it really shouldn't be necessary to flush any buffer unless perhaps you're talking about a *very* small delay (well under a second).

I'm not sure if either of these things is the problem, but your while loop should deal with the case where recv returns 0, which is EOF from the peer, and ideally you would handle NUL characters in the data gracefully. (strcpy will think the string ends whereever the first NUL is.)

Not catching the return value of 0 means the code will spin forever in a tight loop when the peer closes the conenction.

Obviously what you gave is pseudocode, but are you sure you want strcpy there and not strcat? (Alternatively you could be adjusting where lastMsg points on each loop.)
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl and its buffer (though) enemorales Programming 2 02-09-2005 03:17 AM
Gentoo VNC, empty dialog box !! "Question" window is empty ! TheHushedCaskeT Linux - Software 0 02-01-2005 10:14 PM
IO Buffer Haafiz Linux - General 1 07-14-2004 03:24 PM
Buffer Memory mikeshn Linux - General 1 09-09-2002 09:38 PM
framed buffer (needs to go) enzo250gto Slackware 12 09-05-2002 01:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration