PlainDatagramSocketImpl questions

John Zavgren john.zavgren at oracle.com
Thu Jun 13 12:34:53 PDT 2013


Greetings:

Why does the procedure:
Java_java_net_PlainDatagramSocketImpl_datagramSocketCreate(...), defined
in the file: jdk/src/solaris/native/java/net/PlainDatagramSocketImpl.c,
close the given socket before returning after an error in some cases,
but not others? Shouldn't the behaviour be the same throughout the
procedure? I think this behaviour should be: throw exception, close socket, return.

For examples consider the following:
     if ((fd = JVM_Socket(domain, SOCK_DGRAM, 0)) == JVM_IO_ERR) {
         NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
                        "Error creating socket");
         return;
     }
>>>> In this case, the socket was never opened, so there no need to
close it.
#ifdef AF_INET6
     /* Disable IPV6_V6ONLY to ensure dual-socket support */
     if (domain == AF_INET6) {
         arg = 0;
         if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, (char*)&arg,
                        sizeof(int)) < 0) {
             NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
             close(fd); <<<< This seems OK
             return;
         }
     }
#endif /* AF_INET6 */

#ifdef __APPLE__
     arg = 65507;
     if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_SNDBUF,
                        (char *)&arg, sizeof(arg)) < 0) {
         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
                         strerror(errno));
         return; <<<<<<<<<< Why not close it? If we can't allocate the
desired buffer size, is it then OK, to just use what we have? It's
interesting that this would fail...
     }
     if (JVM_SetSockOpt(fd, SOL_SOCKET, SO_RCVBUF,
                        (char *)&arg, sizeof(arg)) < 0) {
         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
                         strerror(errno));
         return; <<<< likewise
     }
#endif /* __APPLE__ */

      if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (char*) &t,
sizeof(int))<0) {
         JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
                         "Unable to set SO_BROADCAST");
         return; <<< setsockopt() failed, we threw an exception. We
didn't close the socket.
      }


-- 
John Zavgren
john.zavgren at oracle.com
603-821-0904
US-Burlington-MA






More information about the net-dev mailing list