zero read timeout for pcap
Is -1 a valid read timeout for pcap?
I was told by someone that setting to_ms to -1 in pcap_open_live(char *device, int snaplen, int promisc, int开发者_JAVA百科 to_ms, char *errbuf);
causes it to return immediately when a packet is detected - effectively a zero timeout. It makes a lot of sense that there be some way to set a zero timeout, but then why not make -1 be infinite and 0 be 0? That would be more logical. Additionally, I haven't been able in any way to confirm what effect a to_ms value of -1 has on pcap.
So, is -1 a valid timeout value? If so, what effect does it have? If not, is there any way to set a detection timeout of zero?
I don't think your explanation is making sense.
A timeout for a read call should only come in to play if no data is ever present to be read. In your case, if a packet is never received, then the timeout should control how long the call sits around waiting, before returning. If data is received, the timeout doesn't matter, since then the call must return as soon as it detects (and reads) the data, that's what's your asking it do do.
From reading the code, it seems a negative timeout in pcap means "non-blocking mode", which will make it not wait at all, but instead return directly if no data is available.
精彩评论