IT. Expert System.

Java Standard Edition (SE)

New SocketExceptions in JDK 1.1


New SocketExceptions in JDK 1.1

Previously, all network errors in java raised a SocketException, which didn't provide enough information to decipher what went wrong. Was the connection refused by the remote machine (no one listening on that port)? Or was the host unreachable (connect attempt timed out)? JDK 1.1 adds three new classes that provide a finer granularity of reported errors:

  • java.net.BindException

    The local port is in use, or the requested bind address couldn't be assigned locally.

            public class BindException extends SocketException {
            ...
            }
    
  • java.net.ConnectException

    This exception is raised when a connection is refused at the remote host (i.e., no process is listening on that port).

            public class ConnectException extends SocketException {
            ...
            }
    
  • java.net.NoRouteToHostException

    The connect attempt timed out, or the remote host is otherwise unreachable.

            public class NoRouteToHostException extends SocketException {
            ...
            }
    

Simple Usage Examples:

  • On the Client Side:
    import java.net.*;
            ...
            Socket s = null;
            try {
               s = new Socket("example.org", 80);
            } catch (UnknownHostException e) {
               // check spelling of hostname
            } catch (ConnectException e) {
               // connection refused - is server down? Try another port.
            } catch (NoRouteToHostException e) {
               // The connect attempt timed out.  Try connecting through a proxy
            } catch (IOException e) {
               // another error occurred
            }
    
  • On the Server Side:
    import java.net.*;
            ...
            ServerSocket ss = null;
            try {
                /* try to bind to local address 192.0.2.254 */
                InetAddress in = InetAddress.getByName("192.0.2.254");
                int port = 8000;
                int backlog = 5;
                ss = new ServerSocket(port, backlog, in);
            } catch (BindException e) {             
                // port 8000 in use, or can't bind to 192.0.2.254 as a local address
            } catch (SocketException e) {
                // another error occurred
            }
    


Content

Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)

SQL

HTML

PHP

CSS

Java Script

MYSQL

JQUERY

VBS

REGEX

C

C++

C#

Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

License.
All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 621 / . Delta: 0.04260 с