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 08-21-2009, 12:10 PM   #1
Diego10
LQ Newbie
 
Registered: Aug 2009
Location: Canada
Posts: 1

Rep: Reputation: 0
Exclamation Can't get socket peek to work


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!
 
Old 08-22-2009, 09:39 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
http://support.microsoft.com/kb/192599

Peeking for data in Winsock means that an application requests for the amount of incoming data waiting to be received without actually receiving it...

You should avoid doing this because it is highly inefficient, and it subjects an application to an incorrect data count.
Good advice, IMHO
 
  


Reply

Tags
socket



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
Python: work while waiting for socket connection? hbar Programming 4 10-27-2010 04:57 AM
LXer: Peek Presents Challenge: Linux on Peek LXer Syndicated Linux News 0 07-11-2009 10:10 PM
bad file descriptor on PEEK of a connected socket DavidHB Programming 3 08-23-2006 04:57 PM
Second PCMCIA socket doesn't work. composer777 Linux - Laptop and Netbook 2 05-10-2004 01:09 PM
A peek into one of my IT meetings. Whitehat General 3 08-05-2003 11:00 PM

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

All times are GMT -5. The time now is 03:29 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