And if you want to do floating point, check out the section on Serialization , far below. Assume the numbers in this document are in Host Byte Order unless I say otherwise. First the easy one: a socket descriptor. A socket descriptor is the following type: int Just a regular int.
|Published (Last):||9 February 2007|
|PDF File Size:||17.73 Mb|
|ePub File Size:||17.33 Mb|
|Price:||Free* [*Free Regsitration Required]|
And if you want to do floating point, check out the section on Serialization , far below. Assume the numbers in this document are in Host Byte Order unless I say otherwise. First the easy one: a socket descriptor. A socket descriptor is the following type: int Just a regular int. Things get weird from here, so just read through and bear with me. This structure is a more recent invention, and is used to prep the socket address structures for subsequent use.
This is cool because your code can be IP version-agnostic. You know, in old versions of this guide and so on. Some structs are IPv4, some are IPv6, and some are both. Anyway, the struct sockaddr holds socket address information for many types of sockets. What is that thing? Well, it used to be a union, but now those days seem to be gone. Good riddance. What about IPv6? So check to make sure the result is greater than 0 before using!
All right, now you can convert string IP addresses to their binary representations. What about the other way around? Private Or Disconnected Networks Lots of places have a firewall that hides the network from the rest of the world for their own protection.
Are you getting nervous yet? But I wanted to talk about the network behind the firewall in case you started getting confused by the network numbers you were seeing. For instance, I have a firewall at home. How is this possible? They are on a private network with 24 million IP addresses allocated to it. They are all just for me. Well, all for me as far as anyone else is concerned. But if I ask my local computer what its IP address is, it says Who is translating the IP address from one to the other?
Less common is Fun fact! The IPv6 has private networks, too, in a sense. Tell me now! Of course, there is more than this, but this is what applies to the guide. First of all, try to use getaddrinfo to get all the struct sockaddr info, instead of packing the structures by hand. This will keep you IP version-agnostic, and will eliminate many of the subsequent steps.
Instead of gethostbyname , use the superior getaddrinfo. Instead of gethostbyaddr , use the superior getnameinfo although gethostbyaddr can still work with IPv6. Use IPv6 multicast instead. Et voila! System Calls or Bust This is the section where we get into the system calls and other library calls that allow you to access the network functionality of a Unix box, or any box that supports the sockets API for that matter BSD, Windows, Linux, Mac, what-have-you.
When you call one of these functions, the kernel takes over and does all the work for you automagically. The place most people get stuck around here is what order to call these things in. Please note that for brevity, many code snippets below do not include necessary error checking. And they very commonly assume that the result from calls to getaddrinfo succeed and return a valid entry in the linked list. Both of these situations are properly addressed in the stand-alone programs, though, so use those as a model.
This is a real workhorse of a function with a lot of options, but usage is actually pretty simple. It helps set up the structs you need later on. A tiny bit of history: it used to be that you would use a function called gethostbyname to do DNS lookups. This is no longer necessary, thankfully. Nor is it desirable, if you want to write code that works for both IPv4 and IPv6! In these modern times, you now have the function getaddrinfo that does all kinds of good stuff for you, including DNS and service name lookups, and fills out the structs you need, besides!
The node parameter is the host name to connect to, or an IP address. Or you can put a specific address in as the first parameter to getaddrinfo where I currently have NULL, up there. Then we make the call. If everything works properly, though, servinfo will point to a linked list of struct addrinfos, each of which contains a struct sockaddr of some kind that we can use later! I keep saying that servinfo is a linked list with all kinds of address information.
Sorry about that! Sample run! Keep reading! I guess I can put it off no longer—I have to talk about the socket system call. It used to be people would hardcode these values, and you can absolutely still do that. And they all lived happily ever after, The End.
Anyway, enough of that. Fine, fine, fine, but what good is this socket? Once you have a socket, you might have to associate that socket with a port on your local machine. Read it anyway, just for kicks.
Beej's Guide to Network Programming
A socket descriptor is the following type:. But I wanted to talk about the network behind the firewall in case you started getting confused by the network numbers you were guive. The function returns 0 on successful completion, neworking -1 on error, setting errno as usual. Your system has a name. For instance, each of these pairs of addresses are equivalent:. Additionally, these other fields might be present:. What happened to the remaining bytes?
BEEJ NETWORKING GUIDE PDF