Code example for DatagramSocket

Methods: closeconnectreceivesendsetSoTimeout

1
        } catch (IllegalArgumentException e) {
            // jndi.40=Unable to connect: bad IP address 
            throw new DomainProtocolException(Messages.getString("jndi.40")); //$NON-NLS-1$ 
        } 
        try { 
            dSocket = new DatagramSocket();
            srvAddr = InetAddress.getByAddress(srvAddrArr);
            dSocket.connect(srvAddr, serverPort);
            outPacket = new DatagramPacket(outBuf, outBufLen, srvAddr,
                    serverPort);
            dSocket.setSoTimeout(timeout);
            dSocket.send(outPacket);
            inPacket = new DatagramPacket(inBuf, inBufLen, srvAddr, serverPort);
            dSocket.receive(inPacket);
        } catch (IllegalStateException e) {
            // jndi.41=Error while querying DNS server 
            throw new DomainProtocolException(Messages.getString("jndi.41"), e); //$NON-NLS-1$
        } catch (SocketTimeoutException e) {
            throw (e);
        } catch (IOException e) {
            // jndi.41=Error while querying DNS server 
            throw new DomainProtocolException(Messages.getString("jndi.41"), e); //$NON-NLS-1$
        } finally { 
            if (dSocket != null) {
                dSocket.close();
            } 
        } 
        if (inPacket != null) {
            return inPacket.getLength();
        }