ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
Distribution: OpenBSD 4.6, OS X 10.6.2, CentOS 4 & 5
Posts: 3,660
Rep:
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.
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.
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
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.)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.