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

This code example shows how to use the following methods:getLocalAddress, getLocalPort
 
	public UDPEcho(String host, int port) throws IOException,
			UnknownHostException { 
		this.hostIP = InetAddress.getByName(host);
		this.port = port;
		sock = new DatagramSocket();
		System.out.println("UDP: " + sock.getLocalAddress() + ":"
				+ sock.getLocalPort());
		// sock.connect(hostIP,port); 
	} 
 
	public void send(String s) throws IOException {
		System.out.println("Sending:" + s);
		final DatagramPacket packet = new DatagramPacket(s.getBytes(), s
				.length(), hostIP, port);
		sock.send(packet);
	} 
 
	public void run() { 
		final byte[] buf = new byte[BUF_SIZE];
6
CodeRank
This code example shows how to use the following methods:getInetAddress, getLocalPort, joinGroup
 
    public MulticastConnection(SocketInetAddress address, MessageSerializer<T> serializer) {
        this.address = address;
        this.serializer = serializer;
        try { 
            socket = new MulticastSocket(address.getPort());
            socket.joinGroup(address.getAddress());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        } 
        localAddress = new SocketInetAddress(socket.getInetAddress(), socket.getLocalPort());
    } 
 
    @Override 
    public String toString() {
        return String.format("multicast connection %s", address);
    } 
 
    public void dispatch(T message) {
        try { 
6
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort
	public void start()	{ 
 
		StringBuffer name = new StringBuffer("Cyber.SSDPSearchResponseSocket/");
		DatagramSocket s = getDatagramSocket();
		// localAddr is null on Android m3-rc37a (01/30/08) 
		InetAddress localAddr = s.getLocalAddress();
		if (localAddr != null) {
			name.append(s.getLocalAddress()).append(':');
			name.append(s.getLocalPort());
		} 
		deviceSearchResponseThread = new Thread(this,name.toString());
		deviceSearchResponseThread.start();		
	} 
	 
	public void stop() 
	{ 
		deviceSearchResponseThread = null;
	} 
 
	//////////////////////////////////////////////// 
6
CodeRank
This code example shows how to use the following methods:close, connect, getLocalPort, receive, setSoTimeout
    return result;
  } 
 
  private int attemptConnection( int timeout ) {
    try { 
      socket = new DatagramSocket();
      socket.connect(new InetSocketAddress(server, port));
      socket.setSoTimeout(timeout);
      sendSignal(new OpenPortSignal(sessionId));
 
      SignalResponse response = readSignalResponse();
 
      if (response.getStatusCode() != 200) {
        Log.e("NetworkConnector", "Bad response from server.");
        socket.close();
        return -1; 
      } 
 
      int localPort = socket.getLocalPort();
      socket.close();

7
CodeRank
This code example shows how to use the following methods:getLocalAddress, getLocalPort, setSoTimeout
 
	/** 
	 * Starts udp relay server. Spawns two threads of execution and returns. 
	 */ 
	public void start() throws IOException { 
		remote_sock.setSoTimeout(iddleTimeout);
		client_sock.setSoTimeout(iddleTimeout);
 
		log.info("Starting UDP relay server on {}:{}", relayIP, relayPort);
		log.info("Remote socket {}:{}", remote_sock.getLocalAddress(),
				remote_sock.getLocalPort());
 
		pipe_thread1 = new Thread(this, "pipe1");
		pipe_thread2 = new Thread(this, "pipe2");
 
		lastReadTime = System.currentTimeMillis();
 
		pipe_thread1.start();
		pipe_thread2.start();
	} 
6
CodeRank
This code example shows how to use the following methods:close, getLocalPort, receive, send, setSoTimeout
        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
                InetAddress.getLocalHost(), 1000);
        assertEquals("Incorrect port returned", 1000, dp.getPort());
 
        final InetAddress localhost = InetAddress.getLocalHost();
        DatagramSocket socket = new DatagramSocket(0, localhost);
        final int port = socket.getLocalPort();
 
        socket.setSoTimeout(3000);
        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5,
                6 }, 6, localhost, port);
        socket.send(packet);
        socket.receive(packet);
        socket.close();
        assertTrue("datagram received wrong port: " + packet.getPort(), packet
                .getPort() == port);
    } 
 
    /** 
     * @tests java.net.DatagramPacket#setAddress(java.net.InetAddress) 
6
CodeRank
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:getLocalPort
public class SendSize { 
    static final int bufferLength = 512;
    static final int packetLength = 256;
 
    public static void main(String[] args) throws Exception {
        DatagramSocket serverSocket = new DatagramSocket();
        new ServerThread(serverSocket).start();
        new ClientThread(serverSocket.getLocalPort()).start();
    } 
 
    static class ServerThread extends Thread {
        DatagramSocket server;
 
        ServerThread(DatagramSocket server) {
            this.server = server;
        } 
 
        public void run() { 
            try { 
                System.err.println("started server thread: " + server);
5
CodeRank
This code example shows how to use the following methods:getLocalPort, receive, setSoTimeout
	public  Object receive(int timeout) throws IOException,
			SocketTimeoutException { 
		// Maximale Wartezeit fuer Receive am Socket einstellen 
		try { 
			log.info("RECEIVE MIT TIMEOUT: "+timeout);
			socket.setSoTimeout(timeout);
			// System.out.println("RECEIVE: Maximale Wartezeit: " + 
			// timeout +" ms"); 
		} catch (SocketException e) {
			log.error("RECEIVE: "
					+ "Fehler beim Einstellen der maximalen Wartezeit"); 
			throw e;
		} 
 
		byte[] bytes = new byte[65527];
		DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
 
		try { 
			// Blockiert nur, bis Timeout abgelaufen ist 
			socket.receive(packet);

5
CodeRank
This code example shows how to use the following methods:getLocalPort, receive, setSoTimeout
	public  Object receive(int timeout) throws IOException,
			SocketTimeoutException { 
		// Maximale Wartezeit fuer Receive am Socket einstellen 
		try { 
//			log.info("RECEIVE MIT TIMEOUT: "+timeout); 
			socket.setSoTimeout(timeout);
			// System.out.println("RECEIVE: Maximale Wartezeit: " + 
			// timeout +" ms"); 
		} catch (SocketException e) {
			log.error("RECEIVE: "
					+ "Fehler beim Einstellen der maximalen Wartezeit"); 
			throw e;
		} 
 
		byte[] bytes = new byte[65527];
		DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
 
		try { 
			// Blockiert nur, bis Timeout abgelaufen ist 
			socket.receive(packet);

5
CodeRank
getLocalPort Method Overview
Gets the local port which this socket is bound to.
See Code Examples for other Java 8 DatagramSocket Methods: