Distributed Systems Lab 3 - TCP/IP connections in Java
Objectives
- In today's lab you will use TCP and UDP sockets for communication.
In Task 1 you will use TCP to retrieve a web pages and UDP to
send strings between two different machines. At the end of the
lab, you should know how to use Java to implement TCP and UDP
sockets and in the lecture you will learn what the pros/cons of
the two protocols are.
- And finally, you will also learn how to use javadoc to document
you code (TASK 3) (This will be useful for documenting
your coursework code, so do attempt it).
Overview
TCP and UDP are example protocols at the "Transport" layer in
the OSI
model of network architecture
- TCP
-
- Connection Oriented (like a telephone service)
- Reliable (i.e in the way of handling data): Guarantees
delivery of data.
- Examples: Web (HTTP), Email (SMTP, IMAP)
- UDP
-
- Connection-less (postal service like)
- Unreliable, no guaranteed delivery
- Example: DNS, Various types of streaming media.
...About Sockets...
Sockets are essentially end points for communication, analagous to
telephones on a telephone line. You need to have one on each end in
order for there to be communication. So before you use a program to
talk using UDP or TCP you need to open a socket. When you open a
socket you bind (binding is like plugging in) it to a specific IP
address and port. After a socket is bound it is ready to send and
receive data. At least in UNIX type systems, sockets are like file
descriptors. So the important thing to remember is that they need to
be properly opened and closed like files in order to keep things from
getting messy. Trying to bind multiple sockets to the same port on a
computer can cause conflicts. Also, when you create a socket it comes
back to you as a socket descriptor number, this descriptor is the
handle for the socket and is how you will refer to it in the
future.
A final bit to remember about sockets is that they have both sending
and receiving buffers associated with them. In relation to UDP the
sending buffer does not have much relevance as long as it is bigger
than any single packet you will send. On the receiving side, a socket
will store up UDP packets in the buffer until the buffer is full. Of
course, when you read a packet from the socket it will remove it from
the buffer. When the buffer is full any more packets going to the
buffer will be lost. So if you are sending data at high speeds and
your packet reading application can't keep up with the sender it is
important to keep in mind that the buffer may have some kind of effect
on your program's behavior.
Task 1: Retrieving web pages using TCP sockets
Task 2: Client/server interaction using UDP sockets
Task 3:
Documenting Java code
Question:
- Which type of application is better off with TCP, which with
UDP? Discuss the trade-off between reliability and performance.