IT. Expert System.

Java Standard Edition (SE)

Socket Options in Java

Socket Options in Java

The C way

Programming in C, one sets options by using a system call along the lines of:
        setsockopt(int fd, int level, int optval, void *optdata, 
                   int optdatalen);

        fd = already opened (possibly connected) socket fd;
        level = level in the protocol stack (IP, UDP, TCP) where
                the option applies;
        optval = the option, a CONSTANT;
        optdata = ptr to option dependent struct of parameters relevant
                 only to a particular option;

In java

The C way of setting options lacks the type-safety of object-oriented programming. The option one wishes to set/get is identified by an int, and the value to set/get into is an opaque void*. It is all too easy to pass the wrong option identifier, the wrong type object in the void* parameter, or the wrong for that parameter. Worse still, the code for these errors will typically compile, and the error will only be manifested at runtime.

Java now provides a type-safe way to set options. Each socket class has a get/set method for each option it supports, taking and returning the appropriate type. The options supported, for which socket classes and their meaning in brief:

    • Disable Nagle's algorithm.
    • Valid for (client) Sockets.
    • Specify a linger-on-close timeout.
    • Valid for (client) Sockets.
    • Specify a timeout on blocking socket operations. (Don't block forever!
    • Valid for all sockets: Socket, ServerSocket, DatagramSocket.
    • Fetch the local address binding of a socket.
    • Valid for Socket, ServerSocket, DatagramSocket.
    • Enable reuse address for a socket.
    • Valid for Socket, ServerSocket, DatagramSocket.
    • Enables a socket to send broadcast messages.
    • Valid for DatagramSocket.
    • Set a hint the size of the underlying buffers for outgoing network I/O.
    • Valid for all sockets: Socket, ServerSocket, DatagramSocket.
    • Get the size of the buffer actually used by the platform when receiving in data on this socket.
    • Valid for all sockets: Socket, ServerSocket, DatagramSocket.
    • Turn on socket keepalive.
    • Valid for Socket.
    • Enable inline reception of TCP urgent data.
    • Valid for Socket.
    • Specify the outgoing interface for multicast packets (on multihomed hosts).
    • Valid for MulticastSockets.
    • Enables or disables local loopback of multicast datagrams.
    • Valid for MulticastSocket.
  • IP_TOS
    • Sets the type-of-service or traffic class field in the IP header for a TCP or UDP socket.
    • Valid for Socket, DatagramSocket

Fell by the wayside...

Some possible BSD options that are not supported in java:
    The main argument in favor of this one seemed to be so people could write "ping" in java. Security nightmare. Must be root on UNIX machines.

The implementation details...

...that you don't need to know, unless you subclass SocketImpl/DatagramSocketImpl. Every *Socket object has an underlying SocketImpl/DatagramSocketImpl that interfaces to native code. The Impl classes implement two methods to support options:
        void setOption(int optID, Object val) throws SocketException;
        Object getOption(int optID) throws SocketException;
that look much like C. These methods act as glue to the native methods, and ensure type safety before native methods are invoked.


Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)





Java Script








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

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: 335 / 159178154. Delta: 0.02780 с