DatagramSocket close - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for DatagramSocket close method.

This code example shows how to use the following methods:close
        } 
 
        @Override 
        void close() { 
            closed.set(true);
            socket.close();
        } 
 
        @Override 
        public void run(){ 
 
            while (!closed.get()){
                try { 
                    DatagramPacket packet = new DatagramPacket(new byte[2048], 2048);
                    socket.receive(packet);
                    byte[] bytes = new byte[packet.getLength()];
                    System.arraycopy(packet.getData(), 0, bytes, 0, packet.getLength());
                    receivedData.add(bytes);
                } catch (IOException e) {
                    if (!closed.get()){
7
CodeRank
This code example shows how to use the following methods:close, send
		try { 
			byte[] magic = { 'a', 'r', 'd', 'u', 'i', 'n', 'o' };
			DatagramPacket packet;
			packet = new DatagramPacket(magic, magic.length, _addr, _port);
 
			DatagramSocket socket = new DatagramSocket();
			socket.send(packet);
			socket.close();
			System.out.println("Packet sent.");
		} catch(SocketException e) {
			e.printStackTrace();
		} catch(IOException e) {
			e.printStackTrace();
		} 
	} 
 
} 
7
CodeRank
This code example shows how to use the following methods:close, isClosed
	// =========================================================== 
	// Methods 
	// =========================================================== 
 
	public static final void closeSocket(final DatagramSocket pDatagramSocket) {
		if(pDatagramSocket != null && !pDatagramSocket.isClosed()) {
			pDatagramSocket.close();
		} 
	} 
 
	public static final void closeSocket(final Socket pSocket) {
		if(pSocket != null && !pSocket.isClosed()) {
			try { 
				pSocket.close();
			} catch (final IOException e) {
				Debug.e(e);
			} 
		} 
	} 
 
8
CodeRank
This code example shows how to use the following methods:close
    } 
    return null; 
  } 
 
  public void close() { 
    socket.close();
  } 
} 
7
CodeRank
This code example shows how to use the following methods:close, setReuseAddress
			ServerSocket ss = null;
			DatagramSocket ds = null;
			try { 
				ss = new ServerSocket(port);
				ss.setReuseAddress(true);
				ds = new DatagramSocket(port);
				ds.setReuseAddress(true);
				return true; 
			} catch (IOException e) {
			} finally { 
				if (ds != null) {
					ds.close();
				} 
 
				if (ss != null) {
					try { 
						ss.close();
					} catch (IOException e) {
						/* should not be thrown */ 
					} 
6
CodeRank
This code example shows how to use the following methods:close, connect, receive, send, setSoTimeout
        } 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) {

6
CodeRank
This code example shows how to use the following methods:close, send
	 
	private void SendUdp(){ 
		try { 
			Log.v("TEST","sending");
			 
			DatagramSocket visibleSocket = new DatagramSocket();
			InetAddress IPAddress = InetAddress.getByName(destination);
			byte[] sendData = new byte[512];
			sendData = data.getBytes();
			DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
		    visibleSocket.send(sendPacket);
		    visibleSocket.close();
		     
		    Log.v("TEST","send");
		} catch (SocketException e) {
			// TODO Auto-generated catch block 
			Log.v("TEST","prob 1");
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block 
6
CodeRank
This code example shows how to use the following methods:close
    public static void main(String[] args) throws Exception {
        MulticastSocket s1, s2;
 
        try { 
            s1 = new MulticastSocket(4160);
            s2 = new MulticastSocket(4160);
            s1.close();
            s2.close();
        } catch (BindException e) {
            throw new RuntimeException("MulticastSocket do no set SO_REUSEADDR");
        } 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, receive, setSoTimeout
import java.net.SocketTimeoutException; 
 
public class DatagramTimeout { 
    public static void main(String[] args) throws Exception {
        boolean success = false;
        DatagramSocket sock = new DatagramSocket();
 
        try { 
            DatagramPacket p;
            byte[] buffer = new byte[50];
            p = new DatagramPacket(buffer, buffer.length);
            sock.setSoTimeout(2);
            sock.receive(p);
        } catch (SocketTimeoutException e) {
            success = true;
        } finally { 
            sock.close();
        } 
        if (!success)
            throw new RuntimeException("Socket timeout failure.");

7
CodeRank
This code example shows how to use the following methods:close, send, joinGroup, setTimeToLive
                if (config.getHost() != null) {
                    InetAddress addr =  InetAddress.getByName(config.getHost());
                    InetSocketAddress addrs = new InetSocketAddress(addr, config.getMultiport());
                    s = new MulticastSocket(addrs);
                } else 
                    s = new MulticastSocket(config.getMultiport());
 
                s.setTimeToLive(config.getTtl());
                s.joinGroup(group);
            } catch (Exception ex) {
                log.error("Unable to use multicast: " + ex);
                s = null;
                return -1; 
            } 
        } 
 
        byte[] buf;
        buf = mess.getBytes(StandardCharsets.US_ASCII);
        DatagramPacket data = new DatagramPacket(buf, buf.length, group, config.getMultiport());
        try { 

6
CodeRank

Related DatagramSocket close Questions & Answers:

close Method Overview
Closes this UDP datagram socket and all possibly associated channels.
See Code Examples for other Java 8 DatagramSocket Methods: