---
name: "pcap_setnonblock"
section: "3pcap"
platform: "macos"
package: "libpcap"
version: "146"
release: "macOS 26.5"
platforms: ["linux", "macos"]
url: "https://new.carta.tech/pcap_setnonblock/3pcap"
---

# pcap\_setnonblock(3pcap)

set or get the state of non-blocking mode on a capture device

## NAME

pcap\_setnonblock, pcap\_getnonblock - set or get the state of non-blocking mode on a capture device

## SYNOPSIS

```
#include <pcap/pcap.h>
```

```
char errbuf[PCAP_ERRBUF_SIZE];
```

**int pcap\_setnonblock(pcap\_t \*p, int nonblock, char \*errbuf);** **int pcap\_getnonblock(pcap\_t \*p, char \*errbuf);**

## DESCRIPTION

**pcap\_setnonblock**() puts a capture handle into \`\`non-blocking'' mode, or takes it out of \`\`non-blocking'' mode, depending on whether the *nonblock* argument is non-zero or zero.  It has no effect on \`\`savefiles''. If there is an error, **PCAP\_ERROR** is returned and *errbuf* is filled in with an appropriate error message; otherwise, **0** is returned.

In \`\`non-blocking'' mode, an attempt to read from the capture descriptor with **pcap\_dispatch**(3PCAP) and **pcap\_next\_ex**(3PCAP) will, if no packets are currently available to be read, return **0** immediately rather than blocking waiting for packets to arrive.

**pcap\_loop**(3PCAP) will loop forever, consuming CPU time when no packets are currently available; **pacp\_dispatch**() should be used instead. **pcap\_next**(3PCAP) will return **NULL** if there are no packets currently available to read; this is indistinguishable from an error, so **pcap\_next\_ex**() should be used instead.

When first activated with **pcap\_activate**(3PCAP) or opened with **pcap\_open\_live**(3PCAP)**,** a capture handle is not in \`\`non-blocking mode''; a call to **pcap\_setnonblock**() is required in order to put it into \`\`non-blocking'' mode.

## RETURN VALUE

**pcap\_getnonblock**() returns the current \`\`non-blocking'' state of the capture descriptor; it always returns **0** on \`\`savefiles''. If there is an error, **PCAP\_ERROR** is returned and *errbuf* is filled in with an appropriate error message.

*errbuf* is assumed to be able to hold at least **PCAP\_ERRBUF\_SIZE** chars.

## SEE ALSO

**pcap**(3PCAP), **pcap\_next\_ex**(3PCAP), **pcap\_geterr**(3PCAP)
