What Is a Socket?

[ 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.

Figure 2.4. A typical electrical socket used in North America.

graphic/02fig04.gif


NOTE

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.

Figure 2.5. This simple network connects three computers (1 to 2, and 3 to 2).

graphic/02fig05.gif


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.

Listening Sockets

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.

Data Sockets

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).

Connection Diagrams

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.

Figure 2.6. This is a connection diagram for TCP.

graphic/02fig06.gif


NOTE

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.

Figure 2.7. This is a connection diagram for UDP.

graphic/02fig07.gif


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 ]


MUD Game Programming
MUD Game Programming (Premier Press Game Development)
ISBN: 1592000908
EAN: 2147483647
Year: 2003
Pages: 147
Authors: Ron Penton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net