I am building a network utility for OS X. I've gone through Apples documentation, but I cannot find the framework that allows my app to monitor incoming bytes. Can anybody point me in the right direction? Thank you for your time!
-
What do you mean by monitoring incoming bytes? Do you want to capture all incoming packets, and be able to display their contents? Or do you just want to get a count of incoming bytes (or bytes per second) to a network interface, so you can display performance metrics? – Brian Campbell May 21 '12 at 15:46
-
I'm just keeping track of data usage. I only need the packets to determine their size. – Tanner Silva May 21 '12 at 16:13
3 Answers
To get statistics on a network, you can use the sysctl
system call. This is fairly thinly documented; there's another answer on StackOverflow that gives a brief example, and for more detail, I'd recommend looking at the netstat source code.

- 1
- 1

- 322,767
- 57
- 360
- 340
I think for something like this could be done with
http://www.wireshark.org/ or http://www.tastycocoabytes.com/cpa/
In Linux you could simply listen to the file that is associated with your network card.
But I don't think this can be done an easy way on OS X. But indeed there must be some way, thinking of LittleSnitch.

- 8,415
- 9
- 52
- 80
-
Which file are you thinking of? One of the files under `/sys/class/net/{interface}/statistics`, where {interface} is the name of the network interface for the network adapter? If so, then Brian Campbell's answer applies - you use sysctl on OS X (and on other BSD-flavored OSes) to get those statistics (not as easy as reading a statistics file, but not too bad - as he says, see the `netstat` command source). – May 21 '12 at 17:25
You can use libpcap, which is a portable library for doing packet captures used by tcpdump, Wireshark, and more. It's not an official Apple library, but it's BSD-licensed so you shouldn't have any problem using it.

- 390,455
- 97
- 512
- 589
-
1Note that, while it's not an official Apple library, in that it wasn't created by Apple, it *is* a library that ships with OS X, complete with manual pages and header files. – May 21 '12 at 17:02