DatagramSocket bind - 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 bind method.

This code example shows how to use the following methods:bind, getLocalPort, setReceiveBufferSize, setReuseAddress, setSoTimeout
			dgSocket=new DatagramSocket(localPort, localAddress); 
		}else{ 
			dgSocket=new DatagramSocket(localPort); 
		} 
		*/ 
	    dgSocket = new DatagramSocket(null);
	    dgSocket.setReuseAddress(true);
	    dgSocket.bind(new InetSocketAddress(localAddress, localPort));
	     
		if(localPort>0)this.port = localPort;
		else port=dgSocket.getLocalPort();
		 
		//set a time out to avoid blocking in doReceive() 
		dgSocket.setSoTimeout(30 * 1000);
		//buffer size 
		dgSocket.setReceiveBufferSize(128*1024);
	} 
 
	/** 
	 * bind to the default network interface on the machine 
	 *  
6
CodeRank
This code example shows how to use the following methods:bind, close, isBound, isClosed, isConnected
        assertEquals("Failed to set TTL", 120, mss.getTTL());
        mss.close();
    } 
 
    public void test_ConstructorLjava_net_SocketAddress() throws Exception { 
        MulticastSocket ms = new MulticastSocket((SocketAddress) null);
        assertTrue("should not be bound", !ms.isBound() && !ms.isClosed() && !ms.isConnected());
        ms.bind(new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed() && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed() && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress("localhost", Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed() && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        try { 
5
CodeRank
This code example shows how to use the following methods:bind, setSoTimeout
 
        try 
        { 
            udpChnl = DatagramChannel.open();
            DatagramSocket socket = udpChnl.socket();
            socket.setSoTimeout(SOCK_TIMEOUT);
            socket.bind(new InetSocketAddress(0));
 
            NtpRequest req = new NtpRequest();
            int bytes = udpChnl.send(req.toByteBuffer(), new InetSocketAddress(server, PORT_NTP)); //time.jmu.edu.cn wwv.nist.gov
 
            Log.d(TAG, bytes + " bytes sent");
 
            socket.setSoTimeout(SOCK_TIMEOUT);
            ByteBuffer buf = ByteBuffer.allocate(60);
            udpChnl.receive(buf);
 
            NtpResponse resp = new NtpResponse();
            resp.fromByteBuffer(buf);
            long newTs = resp.calcSystemTime(req.getOriginateTimestamp());
4
CodeRank
This code example shows how to use the following methods:bind, close, isBound, isClosed, isConnected
 
	/** 
	 * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress) 
	 */ 
	public void test_ConstructorLjava_net_SocketAddress() throws Exception {	 
		MulticastSocket ms = new MulticastSocket((SocketAddress) null);
        assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
                Support_PortManager.getNextPortForUDP())); 
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress(InetAddress
                .getLocalHost(), Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
4
CodeRank
This code example shows how to use the following methods:bind, close, isBound, isClosed, isConnected
 
	/** 
	 * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress) 
	 */ 
	public void test_ConstructorLjava_net_SocketAddress() throws Exception {	 
		MulticastSocket ms = new MulticastSocket((SocketAddress) null);
        assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
                Support_PortManager.getNextPortForUDP())); 
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress(InetAddress
                .getLocalHost(), Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
4
CodeRank
This code example shows how to use the following methods:bind, close, isBound, isClosed, isConnected
 
	/** 
	 * @tests java.net.MulticastSocket#MulticastSocket(java.net.SocketAddress) 
	 */ 
	public void test_ConstructorLjava_net_SocketAddress() throws Exception {	 
		MulticastSocket ms = new MulticastSocket((SocketAddress) null);
        assertTrue("should not be bound", !ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.bind(new InetSocketAddress(InetAddress.getLocalHost(),
                Support_PortManager.getNextPortForUDP())); 
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
        ms = new MulticastSocket(new InetSocketAddress(InetAddress
                .getLocalHost(), Support_PortManager.getNextPortForUDP()));
        assertTrue("should be bound", ms.isBound() && !ms.isClosed()
                && !ms.isConnected());
        ms.close();
        assertTrue("should be closed", ms.isClosed());
4
CodeRank
This code example shows how to use the following methods:sleep, bind, close, connect
        assertEquals(localAddress1, theSocket.getLocalSocketAddress());
 
        // now make sure that datagrams sent from this socket appear to come 
        // from the address we bound to 
        InetSocketAddress localAddress2 = new InetSocketAddress(localHost, ports[2]);
        DatagramSocket ds = new DatagramSocket((SocketAddress) null);
        ds.bind(localAddress2);
 
        DatagramServer server = new DatagramServer(serverPortNumber, localHost);
        server.start();
        Thread.sleep(1000);
 
        ds.connect(new InetSocketAddress(localHost, serverPortNumber));
 
        byte[] sendBytes = { 'T', 'e', 's', 't', 0 };
        DatagramPacket send = new DatagramPacket(sendBytes, sendBytes.length);
        ds.send(send);
        Thread.sleep(1000);
        ds.close();
        // Check that the address in the packet matches the bound address. 

3
CodeRank
This code example shows how to use the following methods:bind
  } 
 
  public void bind(SocketAddress address)
    throws IOException 
  { 
    _socket.bind(address);
  } 
 
  public void connect(SocketAddress address)
    throws IOException 
  { 
    _socket.connect(address);
     
    init(); 
  } 
 
  public void init() 
  { 
    DatagramStream sock = new DatagramStream(_socket);
    sock.setThrowReadInterrupts(true);
4
CodeRank
bind Method Overview
Binds this socket to the local address and port specified by localAddr.
See Code Examples for other Java 8 DatagramSocket Methods: