Practical 07 - Java Socket Programming
Practical 07 - Java Socket Programming
Practical Session 07
Java Socket Programming
Before we get started, the Oracle Java Tutorial is an excellent resource for catching up on your Java
skills. You can use it to learn about the various Java APIs, or simply to refresh your memory about an API
you have not used in a while. The tutorial is located on Oracle's website:
https://docs.oracle.com/javase/tutorial/
https://docs.oracle.com/javase/tutorial/networking/index.html
Application Layer
This layer that delivers data to the user. The three layers below work together to define how data
is transferred from one computer to another. The application layer decides what to do with that data after
it's transferred. For example, an application protocol such as HTTP makes sure that your web browser knows
to display a graphic image as a picture (and not a long stream of numbers). The application layer is where
most of the network parts of your programs spend their time. Your applications can use existing protocols or
define their own layer protocols as necessary.
Ports
IP Addresses would be all you needed if each computer performed one networking function at a time
-modern computers do many different things at once. For example, Email needs to be separated from
FTP requests, which need to be separated from web traffic. This is accomplished through ports. Each
computer with an IP address has several thousand logical ports (65,535 per transport layer protocol, to
be precise). These are logical components in the computer's memory and do not represent anything physical
like a serial or parallel port. Each port is identified by a number from 1 to 65,535. Each port is allocated to
a particular
service – each port is a file.
For example, the HTTP service, which is used by the Web, generally runs on port 80: we say that a web
server listens on port 80 for incoming connections. When data is sent to a web server on a particular
machine at a particular IP address, it is also sent to a particular port (usually port 80) on that machine.
The receiver checks each packet it sees for the port and sends the data to any programs that are listening to
the specified port.
Port numbers from 1 to 1023 are reserved for well-known services such as finger, FTP, HTTP, and email. On
Unix systems, only programs running as root can receive data from these ports, but all programs may send
data to them. On Windows and the Mac, including Windows NT, any program may use these ports without
special privileges. On Unix systems, a fairly complete listing of assigned ports is stored in
the file /etc/services. These assignments are not absolutely guaranteed; in particular, web servers often
run on ports other than 80, either because multiple servers need to run on the same machine, or
because the person who installed the server doesn't have the root privileges needed to run it on port 80.
Sockets
Sockets are the endpoints of a connection between two hosts. They are used for seven basic operations:
• Connect to a Host
• Send data
• Receive data
• Close a connection
• Bind to a port (incoming connection requests must use this port)
• Listen for incoming data
• Accept connections from remote machines on the bound port
Java's java.net.Socket class, which is used by both clients and servers, has methods that correspond to the
first four of these operations. The last three operations are needed only by servers, which wait for clients to
connect to them. They are implemented by the java.net.ServerSocket class, which is discussed in the next
section. Java programs normally use client sockets in the following fashion:
3. Once the connection is established, the local and remote hosts get input and output
streams from the socket, and use those streams to send data to each other. Much like the
streams we obtain from a file. This connection is full-duplex - both hosts can send
and receive data simultaneously.
4. When the transmission of data is complete, one, or both, side/s closes the connection. Some
protocols, such as HTTP 1.0, require the connection to be closed after each request is
serviced. Others, such as FTP, allow multiple requests to be processed in a single
connection.
Exercise 01
● Create a for loop that iterates through the command-line arguments (String[] args)
Server Sockets
Clients are programs that open a socket to a server that's listening for connections. However, client sockets
themselves aren't enough; clients aren't much use unless they can talk to a server, and if you think about it,
sockets aren't sufficient for writing servers. To create a Socket, you need to know the Internet host to which
you want to connect (URL or IP Address). When you're writing a server, you don't know in advance who will
contact you, and even if you did, you wouldn't know when that host wanted to contact you. In other words,
servers are like receptionists who sit by the phone and wait for incoming calls. They don't know who will call
or when, only that when the phone rings, they have to pick it up and talk to whoever is there. We can't
program that behavior with the Socket class alone. Granted, there's no reason that clients written in Java
have to talk to Java servers in fact, a client doesn't care what language the server was written in or what
platform it runs on. However, if Java didn't let us write servers, there would be a glaring hole in its
capabilities.
Java provides a java.net.ServerSocket class to allow programmers to write servers. Basically, a server
socket's job is to sit by the phone and wait for incoming calls. More technically, a ServerSocket runs on the
host and listens for incoming TCP connections on a specific port.
Each ServerSocket listens on a particular port on the server machine. When a client Socket on a remote host
attempts to connect to that port, the server wakes up, negotiates the connection between the client and the
server, and opens a regular Socket between the two hosts. In other words, server sockets wait for
connections while client sockets initiate connections. Once the server socket has set up the connection, the
server uses a regular Socket object to send data to the client. Data always travels over the regular
socket.
The ServerSocket class contains everything you need to write servers in Java. It has constructors that create
new ServerSocket objects, methods that listen for connections on a specified port, and methods that return a
Socket object when a connection is made so that you can send and receive data. In addition, it has methods
to set various options and the usual miscellaneous methods such as toString( ).
2. The ServerSocket listens for incoming connection attempts on that port using its accept( ) method. accept(
) blocks until a client attempts to make a connection, at which point accept( ) returns a Socket object
connecting the client and the server.
3. Depending on the type of server, either the Socket's getInputStream( ) method, getOutputStream( )
method, or both are called to get input and output streams that communicate with the client.
4. The server and the client interact according to an agreed-upon protocol until it is time to close the
connection.
6. The server returns to step 2 and waits for the next connection.
HTTP Protocol
HTTP, the Hypertext Transfer Protocol, is the standard protocol for communication between web browsers
and web servers. HTTP specifies how a client and server establish a connection, how the client requests
data from the server, how the server responds to that request, and finally how the connection is closed.
HTTP connections use the TCP/IP protocol for data transfer. HTTP 1.0 is the currently accepted version of
the protocol. The basic protocol defines a sequence of four steps for each request from a client to the server:
1. Making the connection. The client establishes a TCP connection to the server, on port 80 by default; other
ports may be specified in the URL.
2. Making a request. The client sends a message to the server requesting the page at a specified URL. The
format of this request is typically something like:
GET is a keyword. /index.html is a relative URL to a file on the server. The file is assumed to be on the
machine that receives the request, so there is no need to prefix it with http://www.thismachine.com/. HTTP
1.0 is the version of the protocol that the client understands. The request is terminated with two carriage
return/linefeed pairs (\r\n\r\n in Java parlance) regardless of how lines are terminated on the client or server
platform.
3. The response. The server sends a response to the client. The response begins with a response code,
followed by MIME header information, then a blank line, then the requested document or an error message.
Assuming the requested file is found, a typical response looks like this:
<html>
<Head>
<Title>
A Sample HTML file
</Title>
</Head>
<body>
The rest of the document goes here
</body>
</html>
The first line indicates the protocol the server is using (HTTP 1.0), followed by a response code. 200 OK is
the most common response code, indicating that the request was successful. The other header lines identify
the server software (the NCSA server, Version 1.4.2), the version of MIME in use, the MIME content type,
and the length of the document delivered (not counting this header) in this case, 107 bytes.
19. Closing the connection. Either the client or the server or both close the connection. Thus, a separate
network connection is used for each request. If the client reconnects, the server retains no memory of the
previous connection or its results. A protocol that retains no memory of past requests is called stateless ; in
contrast, a stateful protocol such as FTP can process many requests before the connection is closed. The
lack of state is both a strength and a weakness of HTTP.
HTTP Servers
HTTP is a large protocol. A full-featured HTTP server must respond to requests for files, convert URLs into
filenames on the local system, respond to POST and GET requests, handle requests for files that don't exist,
interpret MIME types, launch CGI programs, and much, much more. However, many HTTP servers don't
need all of these features. For example, many sites simply display an "under construction" message. Clearly,
Apache (popular UNIX web server) is overkill for a site like this. Such a site is a candidate for a custom
server that does only one thing. Java's network class library makes writing simple servers like this almost
trivial.
Finally, Java isn't a bad language for feature-full web servers meant to compete with the likes of Apache or
AOLServer. In particular, sites that make heavy use of dynamic content through CGI scripts, PHP pages, or
other mechanisms can often run much faster when reimplemented on top of a pure or mostly pure Java web
server. Indeed, there are several production web servers written in Java such as the W3C's testbed server
Jigsaw (http://www.w3.org/Jigsaw/ ).
Many other web servers written in C now include substantial Java components to support the Java Servlet
API and Java Server Pages. On many sites, these are replacing the traditional CGIs, ASPs, and server-side
includes, mostly because the Java equivalents are faster and less resource-intensive. We're not going to
explore these technologies here since they easily deserve a course of their own.
Our investigation of HTTP servers entails a server that always sends out the same file, no matter who or
what the request. The filename, and local port, are read from the command line. If the port is omitted, port 80
is assumed.
Exercise 02
● Download the Java source code file called HTTPServer.java via the course website. This file contains the
main method of the HTTP server. Note that it refers to a class that does not exist yet –RequestHandler –
we will now construct this class.
● Construct the following text using the remaining parameters and fields, and assign a byte array of this text
to the header field (hint: use getBytes()):
If it works, invite another student to visit your page – don't forget to notify them of your IP Address! (there is
a shell command will provide this information - windows: ipconfig linux/mac: ifconfig)