Java Reference
In-Depth Information
public class SocketException extends IOException
However, knowing that a problem occurred is often not sufficient to deal with the
problem. Did the remote host refuse the connection because it was busy? Did the remote
host refuse the connection because no service was listening on the port? Did the con‐
nection attempt timeout because of network congestion or because the host was down?
There are several subclasses of SocketException that provide more information about
what went wrong and why:
public class BindException extends SocketException
public class ConnectException extends SocketException
public class NoRouteToHostException extends SocketException
A BindException is thrown if you try to construct a Socket or ServerSocket object on
a local port that is in use or that you do not have sufficient privileges to use. A Connec
tException is thrown when a connection is refused at the remote host, which usually
happens because the host is busy or no process is listening on that port. Finally, a
NoRouteToHostException indicates that the connection has timed out.
The java.net package also includes ProtocolException , which is a direct subclass of
IOException :
public class ProtocolException extends IOException
This is thrown when data is received from the network that somehow violates the TCP/
IP specification.
None of these exception classes have any special methods you wouldn't find in any other
exception class, but you can take advantage of these subclasses to provide more infor‐
mative error messages or to decide whether retrying the offending operation is likely
to be successful.
Sockets in GUI Applications
The HotJava web browser was the first large-scale Java GUI network client. HotJava has
been discontinued, but there are still numerous network-aware client applications writ‐
ten in Java, including the Eclipse IDE and the Frostwire BitTorrent client. It is completely
possible to write commercial-quality client applications in Java; and it is especially pos‐
sible to write network-aware applications, both clients and servers. This section dem‐
onstrates a network client, whois, to illustrate this point; and to discuss the special
considerations that arise when integrating networking code with Swing applications.
The example stops short of what could be done, but only in the user interface. All the
necessary networking code is present. Indeed, once again you find out that network
code is easy; it's user interfaces that are hard.
Search WWH ::




Custom Search