I have some programming experience, mostly in web apps, based on pre-existing frameworks. That is, all the networking side of things are handled already, and no knowledge of them is necessary.
I would like to learn more about programming for networking, and networking security. I'm interested in penetration testing, and I've been reading material on that, but most of it seems fairly shallow, and focused on specific exploits and defense of those attacks. I'd like to go deeper, and look at how networks work, and networking theory. Are there any good books that cover those topics, aimed at and audience with experience like mine, preferably with exercises (like implementing sockets etc.)?