Chapter 3 Networking in Java
Chapter 3 Networking in Java
1. Sockets
Computers running on the Internet communicate with each other using either
the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP).
When you write Java programs that communicate over the network, you are
programming at the application layer.
Typically, you don't need to concern yourself with the TCP and UDP layers.
Instead, you can use the classes in the java.net package.
These classes provide system-independent network communication.
However, to decide which Java classes your programs should use, you do
need to understand how TCP and UDP differ.
1. Sockets…
TCP is a connection-based protocol that provides a reliable flow of data
between two computers.
When two applications want to communicate to each other reliably, they
establish a connection and send data back and forth over that connection.
TCP guarantees that data sent from one end of the connection actually gets
to the other end and in the same order it was sent.
Otherwise, an error is reported.
HTTP, FTP, and Telnet are all examples of applications that require a
reliable communication channel.
UDP is a protocol that sends independent packets of data, called
datagrams, with no guarantees about arrival.
UDP is not connection-based unlike TCP.
For many applications, the guarantee of reliability is critical to the success of
the transfer of information from one end of the connection to the other.
1. Sockets…
However, other forms of communication don't require such strict standards.
In fact, they may be slowed down by the extra overhead or the reliable
connection may invalidate the service altogether.
Ports
Generally speaking, a computer has a single physical connection to the
network.
All data destined for a particular computer arrives through that connection.
However, the data may be intended for different applications/servers
running on the computer.
So how does the computer know to which application to forward the data?
Through the use of ports.
Data transmitted over the Internet is accompanied by addressing
information that identifies the computer and the port for which it is destined.
1. Sockets…
The computer is identified by its 32-bit IP address, which IP uses to deliver
data to the right computer on the network.
Ports are identified by a 16-bit number, which TCP and UDP use to deliver
the data to the right application.
To see the local port on the client, you can write the following code on the
client side
System.out.println("local port: " + socket.getLocalPort());
2.2 Client Connection…
The close() method shuts down both input and output from the socket by
closing the socket.
On occasion, you may want to shut down only half of the connection, either
input or output.
The shutdownInput() and shutdownOutput() methods close only half the
connection:
void shutdownInput()
void shutdownOutput()
Neither actually closes the socket.
Instead, they adjust the stream connected to the socket so that it thinks it’s at
the end of the stream.
Further reads from the input stream after shutting down input return –1.
Further writes to the socket after shutting down output throw an IOException.
2.2 Client Connection…
Reading from and Writing to Sockets
After the server accepts the connection and Socket is created, communication
between server and client is conducted using I/O streams.
The InputStream and OutputStream streams are used to read or write bytes
to sockets.
To get an input stream, use the getInputStream() method on a socket object.
Typically, a server runs continuously on a server computer, and clients from all
over the Internet/LAN can connect to it.
You can use threads to handle the server’s multiple clients simultaneously.
outputToClient.writeDouble(area);
jta.append("Radius received from client: " + radius + '\n');
jta.append("Area computed: " + area + '\n');
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
2.2 UDP Datagrams
Some applications that you write to communicate over the network will not
require the reliable, point-to-point channel provided by TCP.
Rather, your applications might benefit from a mode of communication that
delivers independent packages of information whose arrival and order of
arrival are not guaranteed.
The UDP protocol provides a mode of network communication whereby
applications send packets of data, called datagrams, to one another.
Datagrams are bundles of information passed between machines.
A datagram is an independent, self-contained message sent over the
network whose arrival, arrival time, and content are not guaranteed.
Once the datagram has been released to its intended target, there is no
assurance that it will arrive at the destination.
Likewise, when the datagram is received, there is no assurance that it hasn’t
been damaged in transit or that whoever sent it is still there to receive a
response.
2.2 UDP Datagrams…
UDP is appropriate for data transfers where it doesn't matter if a packet is
lost in transition.
For instance, imagine a transfer of a live TV-signal over the internet.
You want the signal to arrive at the clients as close to live as possible.
Therefore, if a frame or two are lost, you don't really care.
You don't want the live broadcast to be delayed just to make sure all frames
are shown at the client.
You'd rather skip the missed frames, and move directly to the newest frames
at all times.
Java implements datagrams on top of the UDP protocol by using two
classes:
the DatagramPacket which is the data container
The only difference is the port they bind to: the server socket should bind to
By specifying port 0 you ask Java to pick a random available port for you,
method.
This tells the socket to time out after 10 seconds of non-responsiveness:
socket.setSoTimeout(10000);
Timeouts are even more important for UDP than TCP because many