members.cox.net/defiant_penguin/documents/basic-socket.html
I will try to go in depth to the reasoning behind all the strange formats to certain code, which will hopefully make it easier for you to understand and remember. It probably wont make a lot of sense for a while, because I am not going to give examples about why you do some stuff. You probably dont want to skip anything, because it is for the most part all necessary. All the code in this tutorial has been tested and compiled successfully with gcc on gnu/Linux. It should work fine on any of the Linuxs or BSDs, I dont know about anything else. You should be able to get most of this to work on microshit, but I will have no part in it.
Note that the code in this tutorial does not have error checking, for clearity. I advise you to add your own error checking when you begin programming, because otherwise your program will just keep going and you wont even know there is an error. Conventions: include dependencies function and other definition code And include dependencies int main //Normal Code which does stuff, not just a definition return 0; Note how I use the s instead of the Greater-than / Less-than signs. If I dont explain something fully or you need some more data on it, please look in the man pages and on the Internet before consulting me. However, If you cant find it I would be happy to answer your question.
Also, if you want some further advanced reading on this subject, I HIGHLY recommend the book UNIX Network Programming Volume I by Richard Stevens. Basic Stuff Chapter 1: Really Basic Stuff Okay first off, you need to know what a socket file descriptor sfd is. You open a socket, and you get a sfd back, and you use this sfd to connect to hosts, send data, and receive data. There should be already three socket file descriptors open on your machine.
That is probably all you need to know about that for now, so onwards. This next one will take a brief history, although you probably already know this, it is necessary. When the socket interface was created, there were a bunch of different protocols competing to be the leading one. As we know today, the ones that were the major survivors of this contest were TCP and IP. However, when they were creating the socket interface, they had no idea who would win. So they cleverly invented a way so they could easily add more protocols, and people wouldnt have to learn a new interface. One of the most important structures in the socket interface is struct sockaddr. What struct sockaddr contains is the family more in a minute, a network address, and a port. Sockaddr is the generic struct they put in every function, however, to use the tcp and ip protocols, you must use the struct sockaddr_in. This is somewhat confusing to socket-newbies, but after a while it makes perfect sense. Another example of a structure you might use in here, is sockaddr_un. You wont be learning about this, but it is important to show a common example.
So as you can see, it really is not that complicated to create a socket. Next up, you get to learn what to do with your spankin-new socket. Chapter 3: Connecting Okay, to use a socket in cool ways, you will have to connect it to a remote host, or bind it to a local port. The first section will focus on connecting, then in the end I will cover binding. Before you can do any of that, however, struct sockaddr_in needs further explanation.
If that looks bafflingly hard, try going over line by line explaining to yourself what each line does. Dont get too attached to the ease of specifying an ip address as the s_addr. Chapter 4: Reading and Writing Ok, just one more chapter before you learn about format conversion and can actually build something. But you wouldnt really want to build something where you couldnt read and write to a socket, would you? First off, I will cover the read and write functions which are the easiest. Then I will cover using fdopen so you can use fprintf and fgets on sockets. I wont be explaining the use of sendto and recvfrom, mainly because those are functions which are primarily used for UDP and RAW sockets.
Thats coming up next, and when youre done youll be able to read and write from sockets with ease. Format Conversion Chapter 1: HBO and NBO Okay, I should first explain what these acronyms stand for insert lame movie channel joke here. Different hosts use different byte orders, but networks always use the same byte ordering. So there are functions to take numbers in host byte order the one that looks right to you and put it in network byte order, and vice versa. Its easier to remember the names if you think of them as host to network short, host to network long, network to host short, and network to host long. Most of the time with these youll be using the short ones, to convert port numbers to network byte order.
Final Project Chapter 1: tcp_connect Ok this chapter will give you an example of all of this put together. Next chapter will give you some ideas of projects you can do to gain understanding. This is a function I made and use regularly, it creates a socket and connects it to a certain host and port, to save space in the main functions. You can give it either an IP address or a domain name, and it will work.
|