Randall Twede
Sockets
Table of Contents
Introduction 1
The client server model 1
Socket types 1
Creating socket applications 2
Creating socket applications using C++ on the Unix platform 2
Creating socket applications using C++ on the Windows platform 3
Creating socket applications using Java 3
Conclusion 4
Introduction
Sockets were originally developed for the BSD Unix operating system and are still often referred to as BSD sockets. However, the Berkeley socket interface has become something of a standard so sockets are supported on other platforms as well.
URLs and URL connections provide a relatively high-level mechanism for accessing resources on the Internet. Sockets are used when programs require lower-level network communication.
There seems to be some uncertainty about which layer of the OSI model sockets are implemented at. Some claim it is at layer 4, while others maintain it is at layer 5.
The client server model
Most interprocess communication uses the client server model. These terms refer to the two processes which will be communicating with each other. One of the two processes, the client, connects to the other process, the server, typically to make a request for information. A good analogy is a person who makes a phone call to another person.
Notice that the client needs to know of the existence of and the address of the server, but the server does not need to know the address of (or even the existence of) the client prior to the connection being established. Notice also that once a connection is established, both sides can send and receive information.
The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. A socket is one end of an interprocess communication channel. The two processes each establish their own socket.
Socket Types
When a socket is created, the program has to specify the address domain and the socket type. Two processes can communicate with each other only if their sockets are of the same type and in the same domain. There are two widely used address domains, the unix domain, in which two processes which share a common file system communicate, and the Internet domain, in which two processes running on any two hosts on the Internet communicate. Each of these has its own address format.
The address of a socket in the Unix domain is a character string which is basically an entry in the file system.
The address of a socket in the Internet domain consists of the Internet address of the host machine (every computer on the Internet has a unique 32 bit address, often referred to as its IP address). In addition, each socket needs a port number on that host. Port numbers are 16 bit unsigned integers. The lower numbers are reserved in Unix for standard services. For example, the port number for the FTP server is 21. It is important that standard services be at the same port on all computers so that clients will know their addresses. However, port numbers above 2000 are generally available.
There are two widely used socket types, stream sockets, and datagram sockets. Stream sockets treat communications as a continuous stream of characters, while datagram sockets have to read entire messages at once. Each uses its own communciations protocol. Stream sockets use TCP (Transmission Control Protocol), which is a reliable, stream oriented protocol, and datagram sockets use UDP (Unix Datagram Protocol), which is unreliable and message oriented.
Creating socket applications
In this, and the following sections, we will concentrate on stream sockets over the Internet using TCP. A socket application is actually a pair of programs: a client program, and a server program.
" Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request.
On the client-side: The client knows the hostname of the machine on which the server is running and the port number to which the server is connected. To make a connection request, the client tries to rendezvous with the server on the server's machine and port.
If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to a different port. It needs a new socket (and consequently a different port number) so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client.
On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server. Note that the socket on the client side is not bound to the port number used to rendezvous with the server. Rather, the client is assigned a port number local to the machine on which the client is running. The client and server can now communicate by writing to or reading from their sockets.
Definition: A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent."
Creating socket applications using C++ on the Unix platform
The steps involved in establishing a socket on the client side are as follows:
1. Create a socket with the socket() system call
2. Connect the socket to the address of the server using the connect() system call
3. Send and receive data. There are a number of ways to do this, but the simplest is to use the read() and write() system calls.
The steps involved in establishing a socket on the server side are as follows:
1. Create a socket with the socket() system call
2. Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.
3. Listen for connections with the listen() system call
4. Accept a connection with the accept() system call. This call typically blocks until a client connects with the server
5. Send and receive data
Creating socket applications using C++ on the Windows platform
Creating socket applications in C++ for Windows machines involves using the Winsock API, but the function calls are very similar to the function calls in Unix.
" First and foremost in order to use the Winsock API you have to link to the libraries mpr.lib and wsock32.lib. The first step in using the WinSock API is to initialize WSA. This is done by calling WSAStartup().After you have initialized WinSock the next step is to create a socket. All sockets are of type SOCKET, and you create them with the socket() function.
Now we have a usable socket, what we need to do is make use of it. Since the server has to be listening before a client can connect I'll show how to setup the server first. First we bind the socket to a TCP/IP port. This is done with the bind() function. Now that we have a valid socket bound to a TCP/IP port we need to listen on that socket for incoming connections. We use the listen() function to accomplish that. To accept the incoming connection you use accept().
So now we are listening on a TCP/IP port and ready to accept a connection. So lets look into requesting a connection. To do this we use the connect() function. Now that we have a server with a connected client they need to exchange information. This is done exactly the same for the client as it is for the server. The functions to use are send() and recv()."
Creating socket applications using Java
"The java.net package in the Java platform provides a class, Socket, that implements one side of a two-way connection between your Java program and another program on the network. The Socket class sits on top of a platform-dependent implementation, hiding the details of any particular system from your Java program. By using the java.net.Socket class instead of relying on native code, your Java programs can communicate over the network in a platform-independent fashion. Additionally, java.net includes the ServerSocket class, which implements a socket that servers can use to listen for and accept connections to clients.
The steps the client program must perform are:
1. Open a socket.
2. Open an input stream and output stream to the socket.
3. Read from and write to the stream according to the server's protocol.
4. Close the streams.
5. Close the socket.
The server program begins by creating a new ServerSocket object to listen on a specific port. If the server successfully connects to its port, then the ServerSocket object is successfully created and the server continues to the next step--accepting a connection from a client. The accept method waits until a client starts up and requests a connection on the host and port of this server.
When a connection is requested and successfully established, the accept method returns a new Socket object which is bound to a new port. The server can communicate with the client over this new Socket and continue to listen for client connection requests on the ServerSocket bound to the original, predetermined port. After the server successfully establishes a connection with a client, it communicates with the client."
Eventually, the client is finished so the server cleans up by closing all of the input and output streams (Obviously, the server had to create input and output streams just as the client did), the client socket, and the server socket.
The server also enforces the agreed upon protocol. "All client/server pairs must have some protocol by which they speak to each other; otherwise, the data that passes back and forth would be meaningless. The protocol that your own clients and servers use depends entirely on the communication required by them to accomplish the task."
Conclusion
Sockets are used when programs require lower-level network communication. Examples include web browser/web server, ftp client/ftp server, e-mail client/e-mail server, file sharing programs (where each installation contains both a client and a server), and multi-player game programs (over the Internet or using LANs).
Applications that use URLs depend on another application that uses sockets. For example, suppose you create an e-commerce site. The user's browser sends an HTTP request (to the server's URL). Your server-side program sends an HTTP response (to the client's URL). The web server you are using to host your site, and the client's browser, create the sockets and takes care of the lower-level details for you.
Creating applications that use sockets is more complicated than creating applications that use URLs because sockets operate at a lower level in the OSI stack. You have to establish protocols, and also decide which ports to use. It is much easier to send a response to an HTTP request than it is to write both a client and a server program, establish a protocol, create the sockets, and coordinate communication between those sockets.