Java Code Examples for java.net.DatagramSocket#getLocalPort()

The following examples show how to use java.net.DatagramSocket#getLocalPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageTransmitter.java    From Smack with Apache License 2.0 6 votes vote down vote up
public ImageTransmitter(DatagramSocket socket, InetAddress remoteHost, int remotePort, Rectangle area) {

        try {
            robot = new Robot();

            maxI = (int) Math.ceil(area.getWidth() / tileWidth);
            maxJ = (int) Math.ceil(area.getHeight() / tileWidth);

            tiles = new int[maxI][maxJ][tileWidth * tileWidth];

            this.area = area;
            this.socket = socket;
            localHost = socket.getLocalAddress();
            localPort = socket.getLocalPort();
            this.remoteHost = remoteHost;
            this.remotePort = remotePort;
            this.encoder = new DefaultEncoder();

            transmit = true;

        }
        catch (AWTException e) {
            LOGGER.log(Level.WARNING, "exception", e);
        }

    }
 
Example 2
Source File: SnmpSocket.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
/**
 * Creates a new <CODE>SnmpSocket</CODE> object.
 * @param rspHdlr A Datagram handler.
 * @param bufferSize The SNMP adaptor buffer size.
 * @exception SocketException A socket could not be created.
 */
public SnmpSocket(SnmpResponseHandler rspHdlr, InetAddress addr, int bufferSize) throws SocketException {
    super();

    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, SnmpSocket.class.getName(),
            "constructor", "Creating new SNMP datagram socket");
    }

    // TIME BOMB HERE
    _socket = new DatagramSocket(0, addr);
    _socketPort = _socket.getLocalPort();
    responseBufSize = bufferSize;
    _buffer = new byte[responseBufSize];
    _dgramHdlr = rspHdlr;
    _sockThread = new Thread(this, "SnmpSocket");
    _sockThread.start();
}
 
Example 3
Source File: StunServer.java    From freeacs with MIT License 6 votes vote down vote up
StunServerReceiverThread(DatagramSocket datagramSocket, boolean primaryPortIP) {
  this.primaryPortIP = primaryPortIP;
  this.receiverSocket = datagramSocket;
  for (DatagramSocket socket : sockets) {
    if (socket.getLocalPort() != receiverSocket.getLocalPort()
        && socket.getLocalAddress().equals(receiverSocket.getLocalAddress())) {
      changedPort = socket;
    }
    if (socket.getLocalPort() == receiverSocket.getLocalPort()
        && !socket.getLocalAddress().equals(receiverSocket.getLocalAddress())) {
      changedIP = socket;
    }
    if (socket.getLocalPort() != receiverSocket.getLocalPort()
        && !socket.getLocalAddress().equals(receiverSocket.getLocalAddress())) {
      changedPortIP = socket;
    }
  }
}
 
Example 4
Source File: SnmpSocket.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new <CODE>SnmpSocket</CODE> object.
 * @param rspHdlr A Datagram handler.
 * @param bufferSize The SNMP adaptor buffer size.
 * @exception SocketException A socket could not be created.
 */
public SnmpSocket(SnmpResponseHandler rspHdlr, InetAddress addr, int bufferSize) throws SocketException {
    super();

    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, SnmpSocket.class.getName(),
            "constructor", "Creating new SNMP datagram socket");
    }

    // TIME BOMB HERE
    _socket = new DatagramSocket(0, addr);
    _socketPort = _socket.getLocalPort();
    responseBufSize = bufferSize;
    _buffer = new byte[responseBufSize];
    _dgramHdlr = rspHdlr;
    _sockThread = new Thread(this, "SnmpSocket");
    _sockThread.start();
}
 
Example 5
Source File: BindFailTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    DatagramSocket s = new DatagramSocket();
    int port = s.getLocalPort();

    for (int i=0; i<32000; i++) {
        try {
            DatagramSocket s2 = new DatagramSocket(port);
        } catch (BindException e) {
        }
    }
}
 
Example 6
Source File: BindFailTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    DatagramSocket s = new DatagramSocket();
    int port = s.getLocalPort();

    for (int i=0; i<32000; i++) {
        try {
            DatagramSocket s2 = new DatagramSocket(port);
        } catch (BindException e) {
        }
    }
}
 
Example 7
Source File: BindFailTest.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    DatagramSocket s = new DatagramSocket();
    int port = s.getLocalPort();

    for (int i=0; i<32000; i++) {
        try {
            DatagramSocket s2 = new DatagramSocket(port);
        } catch (BindException e) {
        }
    }
}
 
Example 8
Source File: BindFailTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    DatagramSocket s = new DatagramSocket();
    int port = s.getLocalPort();

    for (int i=0; i<32000; i++) {
        try {
            DatagramSocket s2 = new DatagramSocket(port);
        } catch (BindException e) {
        }
    }
}
 
Example 9
Source File: BindFailTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String args[]) throws Exception {
    DatagramSocket s = new DatagramSocket();
    int port = s.getLocalPort();

    for (int i=0; i<32000; i++) {
        try {
            DatagramSocket s2 = new DatagramSocket(port);
        } catch (BindException e) {
        }
    }
}
 
Example 10
Source File: SyslogSourceTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void configureSource() throws Throwable {
	 // TODO: we can remove this when SI 4.3 is used; UDP can then specify port=0, TCP can already do it.
	DatagramSocket socket = new DatagramSocket(0);
	int port = socket.getLocalPort();
	socket.close();
	Properties properties = new Properties();
	properties.put("syslog.port", Integer.toString(port));
	PropertiesInitializer.PROPERTIES = properties;
}
 
Example 11
Source File: DiscoveryTest.java    From freeacs with MIT License 4 votes vote down vote up
private boolean test2()
    throws UtilityException, SocketException, UnknownHostException, IOException,
        MessageAttributeParsingException, MessageAttributeException,
        MessageHeaderParsingException {
  int timeSinceFirstTransmission = 0;
  int timeout = timeoutInitValue;
  do {
    try {
      // Test 2 including response
      DatagramSocket sendSocket = new DatagramSocket(new InetSocketAddress(iaddress, 0));
      sendSocket.connect(InetAddress.getByName(stunServer), port);
      sendSocket.setSoTimeout(timeout);

      MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
      sendMH.generateTransactionID();

      ChangeRequest changeRequest = new ChangeRequest();
      changeRequest.setChangeIP();
      changeRequest.setChangePort();
      sendMH.addMessageAttribute(changeRequest);

      byte[] data = sendMH.getBytes();
      DatagramPacket send = new DatagramPacket(data, data.length);
      sendSocket.send(send);
      LOGGER.debug("Test 2: Binding Request sent.");

      int localPort = sendSocket.getLocalPort();
      InetAddress localAddress = sendSocket.getLocalAddress();

      sendSocket.close();

      DatagramSocket receiveSocket = new DatagramSocket(localPort, localAddress);
      receiveSocket.connect(ca.getAddress().getInetAddress(), ca.getPort());
      receiveSocket.setSoTimeout(timeout);

      MessageHeader receiveMH = new MessageHeader();
      while (!receiveMH.equalTransactionID(sendMH)) {
        DatagramPacket receive = new DatagramPacket(new byte[200], 200);
        receiveSocket.receive(receive);
        receiveMH = MessageHeader.parseHeader(receive.getData());
        receiveMH.parseAttributes(receive.getData());
      }
      ErrorCode ec =
          (ErrorCode)
              receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
      if (ec != null) {
        di.setError(ec.getResponseCode(), ec.getReason());
        LOGGER.debug("Message header contains an Errorcode message attribute.");
        return false;
      }
      if (!nodeNatted) {
        di.setOpenAccess();
        LOGGER.debug(
            "Node has open access to the Internet (or, at least the node is behind a full-cone NAT without translation).");
      } else {
        di.setFullCone();
        LOGGER.debug("Node is behind a full-cone NAT.");
      }
      return false;
    } catch (SocketTimeoutException ste) {
      if (timeSinceFirstTransmission < 7900) {
        LOGGER.debug("Test 2: Socket timeout while receiving the response.");
        timeSinceFirstTransmission += timeout;
        int timeoutAddValue = timeSinceFirstTransmission * 2;
        if (timeoutAddValue > 1600) {
          timeoutAddValue = 1600;
        }
        timeout = timeoutAddValue;
      } else {
        LOGGER.debug(
            "Test 2: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
        if (!nodeNatted) {
          di.setSymmetricUDPFirewall();
          LOGGER.debug("Node is behind a symmetric UDP firewall.");
          return false;
        } else {
          // not is natted
          // redo test 1 with address and port as offered in the changed-address message attribute
          return true;
        }
      }
    }
  } while (true);
}
 
Example 12
Source File: UdpClientTest.java    From ribbon with Apache License 2.0 4 votes vote down vote up
public int choosePort() throws SocketException {
    DatagramSocket serverSocket = new DatagramSocket();
    int port = serverSocket.getLocalPort();
    serverSocket.close();
    return port;
}
 
Example 13
Source File: DiscoveryTest.java    From freeacs with MIT License 4 votes vote down vote up
private boolean test1()
    throws UtilityException, SocketException, UnknownHostException, IOException,
        MessageAttributeParsingException, MessageHeaderParsingException {
  int timeSinceFirstTransmission = 0;
  int timeout = timeoutInitValue;
  do {
    try {
      // Test 1 including response
      socketTest1 = new DatagramSocket(new InetSocketAddress(iaddress, 0));
      socketTest1.setReuseAddress(true);
      socketTest1.connect(InetAddress.getByName(stunServer), port);
      socketTest1.setSoTimeout(timeout);

      MessageHeader sendMH = new MessageHeader(MessageHeader.MessageHeaderType.BindingRequest);
      sendMH.generateTransactionID();

      ChangeRequest changeRequest = new ChangeRequest();
      sendMH.addMessageAttribute(changeRequest);

      byte[] data = sendMH.getBytes();
      DatagramPacket send = new DatagramPacket(data, data.length);
      socketTest1.send(send);
      LOGGER.debug("Test 1: Binding Request sent.");

      MessageHeader receiveMH = new MessageHeader();
      while (!receiveMH.equalTransactionID(sendMH)) {
        DatagramPacket receive = new DatagramPacket(new byte[200], 200);
        socketTest1.receive(receive);
        receiveMH = MessageHeader.parseHeader(receive.getData());
        receiveMH.parseAttributes(receive.getData());
      }

      ma =
          (MappedAddress)
              receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.MappedAddress);
      ca =
          (ChangedAddress)
              receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ChangedAddress);
      ErrorCode ec =
          (ErrorCode)
              receiveMH.getMessageAttribute(MessageAttribute.MessageAttributeType.ErrorCode);
      if (ec != null) {
        di.setError(ec.getResponseCode(), ec.getReason());
        LOGGER.debug("Message header contains an Errorcode message attribute.");
        return false;
      }
      if (ma == null || ca == null) {
        di.setError(
            700,
            "The server is sending an incomplete response (Mapped Address and Changed Address message attributes are missing). The client should not retry.");
        LOGGER.debug(
            "Response does not contain a Mapped Address or Changed Address message attribute.");
        return false;
      } else {
        di.setPublicIP(ma.getAddress().getInetAddress());
        if (ma.getPort() == socketTest1.getLocalPort()
            && ma.getAddress().getInetAddress().equals(socketTest1.getLocalAddress())) {
          LOGGER.debug("Node is not natted.");
          nodeNatted = false;
        } else {
          LOGGER.debug("Node is natted.");
        }
        return true;
      }
    } catch (SocketTimeoutException ste) {
      if (timeSinceFirstTransmission < 7900) {
        LOGGER.debug("Test 1: Socket timeout while receiving the response.");
        timeSinceFirstTransmission += timeout;
        int timeoutAddValue = timeSinceFirstTransmission * 2;
        if (timeoutAddValue > 1600) {
          timeoutAddValue = 1600;
        }
        timeout = timeoutAddValue;
      } else {
        // node is not capable of udp communication
        LOGGER.debug(
            "Test 1: Socket timeout while receiving the response. Maximum retry limit exceed. Give up.");
        di.setBlockedUDP();
        LOGGER.debug("Node is not capable of UDP communication.");
        return false;
      }
    }
  } while (true);
}
 
Example 14
Source File: PortUnreachable.java    From openjdk-jdk8u with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }
 
Example 15
Source File: PortUnreachable.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }
 
Example 16
Source File: PortUnreachable.java    From jdk8u60 with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }
 
Example 17
Source File: PortUnreachable.java    From openjdk-8 with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }
 
Example 18
Source File: PortUnreachable.java    From openjdk-8-source with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }
 
Example 19
Source File: PortUnreachable.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }
 
Example 20
Source File: PortUnreachable.java    From dragonwell8_jdk with GNU General Public License v2.0 2 votes vote down vote up
PortUnreachable() throws Exception {

        clientSock = new DatagramSocket();
        clientPort = clientSock.getLocalPort();

    }