|[ LiB ]|
You've probably heard the term many times before in many contexts, but what is a socket ? Probably the first thing that springs to mind is an electrical socket, like that shown in Figure 2.4.
If you know that your system is in big-endian order, you should still use the htonl and ntohl functions, in the interest of portability. You never know when you'll want to convert the program to a different platform, and you can save yourself lots of headaches by using the functions. Besides, if your system is already in big-endian, calling these functions won't incur overhead, since any decent compiler will just ignore the function call. So omitting the func tions doesn't speed things up.
The electrical socket may look unfamiliar to you if you do not live in North America, but I'm sure you have your own equivalent. You plug an electrical device into the socket to make a connection and can then use all of its services, such as electricity.
There are literally thousands of other kinds of sockets, such as phone sockets (also known as RJ-11 jacks ), Ethernet sockets (RJ-45), and so on.
Network sockets are similar in concept, but they aren't actual physical objects like electrical sockets are. With network programming, each machine in a two-machine connection creates a socket, and then one machine connects to the other. Figure 2.5 shows three computers in a simple socket network.
Before I let you dive down into some actual socket coding, I'll explain the different "kinds" of sockets. I put that word in quotation marks for a reason, which you will understand later on, when I get into the code examples.
The most important kind of socket is the listening socket . This is a socket that the server creates with a specific port number. Once the socket is created, it sits there and listens. Think of a person sitting by a telephone waiting for a call.
This kind of socket is used only with TCP connections, since TCP is a connection-oriented protocol. UDP does not use listening sockets.
Once a listening socket receives a TCP connection, it creates a new socket of a different type so that you can "talk" to the socket.
The other kind of socket is a data socket . These sockets don't do any listening; instead, they "call" a server and, once connected to a server, data sockets can send or receive data (hence the name data sockets).
Figure 2.6 shows a connection diagram for TCP. When the server starts up, it creates a listening socket and assigns it a port number. When the client wants to connect to the listening socket, it creates a data socket and tells it to connect to the listening socket. When the listening socket gets the connection request, it creates a new data socket, and then the server and the client can communicate through their data sockets.
Because UDP is connectionless and doesn't implement flow controls as TCP does, it is a popular protocol for Internet viruses and Distributed Denial Of Service ( DDOS) attacks. UDP is one of the most abused protocols, but that doesn't mean you shouldn't use it.
UDP is even simpler, as shown in Figure 2.7.
Remember, UDP is a connectionless protocol, so there is no connecting needed, ever. You just create the socket on a specific port and send or receive packets.
Whether or not the data is actually received, the timing of the transmission is up to the Application layer protocol, instead of the Transport protocol.
|[ LiB ]|