LINUX programming

Moderators: ITA
Number of threads: 1339
Number of posts: 2924

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Can't get socket peek to work Posted by Diego10 on 21 Aug 2009 at 10:42 AM
Hi,

I am trying to peek the number of received bytes in a socket using the recv() function with the MSG_PEEK flag.

So I wrote a line:

maxToread = recv(source_fd, tmpBuffer, maxToRead, MSG_PEEK | MSG_DONTWAIT);

and then if maxToRead > 0 (there is something to be read) I do:

readn = recv(source_fd, buffer, msgLen, MSG_DONTWAIT);


According to the Man pages, the expected behavior when setting the MSG_PEEK flag is that the data is not removed from the reception buffer so that the next read returns the same data.

In my case I am seeing that, after the first 'recv()' call (with MSG_PEEK), the second reads 0 bytes instead of reading the same data the first call read.

This is quite annoying because I need to be able to check if there is data in the buffer without consuming it. Am I missing something here? Did I misunderstand the MSG_PEEK behavior?

Any help appreciated!!
Thanks!
Report
Re: Can't get socket peek to work Posted by Diego10 on 21 Aug 2009 at 1:58 PM
Sorry, fixed! wrong diagnosis, MSG_PEEK seems to be working



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.