Java Code Examples for java.net.DatagramPacket#setAddress()

The following examples show how to use java.net.DatagramPacket#setAddress() . 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: CapEnv.java    From finalspeed-91yun with GNU General Public License v2.0 6 votes vote down vote up
private void detectMac_udp(){
	for(int i=0;i<10;i++){
		try {
			DatagramSocket ds=new DatagramSocket();
			DatagramPacket dp=new DatagramPacket(new byte[1000], 1000);
			dp.setAddress(InetAddress.getByName(testIp_udp));
			dp.setPort(5555);
			ds.send(dp);
			ds.close();
			Thread.sleep(500);
			if(local_mac!=null){
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				Thread.sleep(1);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
		}
	}

}
 
Example 2
Source File: Socks5DatagramSocket.java    From T0rlib4Android with Apache License 2.0 6 votes vote down vote up
/**
 * This method allows to send datagram packets with address type DOMAINNAME.
 * SOCKS5 allows to specify host as names rather than ip addresses.Using
 * this method one can send udp datagrams through the proxy, without having
 * to know the ip address of the destination host.
 * <p>
 * If proxy specified for that socket has an option resolveAddrLocally set
 * to true host will be resolved, and the datagram will be send with address
 * type IPV4, if resolve fails, UnknownHostException is thrown.
 * 
 * @param dp
 *            Datagram to send, it should contain valid port and data
 * @param host
 *            Host name to which datagram should be send.
 * @throws IOException
 *             If error happens with I/O, or the host can't be resolved when
 *             proxy settings say that hosts should be resolved locally.
 * @see Socks5Proxy#resolveAddrLocally(boolean)
 */
public void send(DatagramPacket dp, String host) throws IOException {
	if (proxy.isDirect(host)) {
		dp.setAddress(InetAddress.getByName(host));
		super.send(dp);
		return;
	}

	if ((proxy).resolveAddrLocally) {
		dp.setAddress(InetAddress.getByName(host));
	}

	final byte[] head = formHeader(host, dp.getPort());
	byte[] buf = new byte[head.length + dp.getLength()];
	final byte[] data = dp.getData();
	// Merge head and data
	System.arraycopy(head, 0, buf, 0, head.length);
	// System.arraycopy(data,dp.getOffset(),buf,head.length,dp.getLength());
	System.arraycopy(data, 0, buf, head.length, dp.getLength());

	if (encapsulation != null) {
		buf = encapsulation.udpEncapsulate(buf, true);
	}

	super.send(new DatagramPacket(buf, buf.length, relayIP, relayPort));
}
 
Example 3
Source File: SnmpAdaptorServer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 4
Source File: SnmpAdaptorServer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 5
Source File: Packet.java    From Kore with Apache License 2.0 6 votes vote down vote up
/**
 * Sends this packet to the EventServer
 * @param adr Address of the EventServer
 * @param port Port of the EventServer
 * @throws IOException
 */
public void send(InetAddress adr, int port) throws IOException
{
	int maxseq = getNumPackets();
	DatagramSocket s = new DatagramSocket();
	// For each Packet in Sequence...
	for(int seq=1;seq<=maxseq;seq++)
	{
		// Get Message and send them...
		byte[] pack = getUDPMessage(seq);
		DatagramPacket p = new DatagramPacket(pack, pack.length);
		p.setAddress(adr);
		p.setPort(port);
		s.send(p);
	}
}
 
Example 6
Source File: SyslogClient.java    From freeacs with MIT License 6 votes vote down vote up
public static void send(String host, SyslogPacket receivedPacket, int defaultPort) {
  try {
    int port = defaultPort;
    String hostname = host;
    if (host.contains(":")) {
      hostname = host.substring(0, host.indexOf(':'));
      port = Integer.parseInt(host.substring(host.indexOf(':') + 1));
    }
    InetAddress address = InetAddress.getByName(hostname);
    DatagramSocket socket = new DatagramSocket();
    String msg = receivedPacket.getSyslogStr();
    if (!msg.contains("|||")) {
      msg += "|||" + receivedPacket.getAddress();
    }
    byte[] message = msg.getBytes();
    DatagramPacket packet = new DatagramPacket(message, message.length);
    packet.setPort(port);
    packet.setAddress(address);
    socket.send(packet);
    socket.close();
  } catch (Throwable t) {
    System.err.println("An error occured in SyslogClient.send(): " + t);
  }
}
 
Example 7
Source File: SnmpAdaptorServer.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 8
Source File: SnmpAdaptorServer.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 9
Source File: SnmpAdaptorServer.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 10
Source File: SnmpAdaptorServer.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Send the specified message on trapSocket.
 */
private void sendTrapMessage(SnmpMessage msg)
    throws IOException, SnmpTooBigException {

    byte[] buffer = new byte[bufferSize] ;
    DatagramPacket packet = new DatagramPacket(buffer, buffer.length) ;
    int encodingLength = msg.encodeMessage(buffer) ;
    packet.setLength(encodingLength) ;
    packet.setAddress(msg.address) ;
    packet.setPort(msg.port) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sending trap to " + msg.address + ":" +
              msg.port);
    }
    trapSocket.send(packet) ;
    if (SNMP_ADAPTOR_LOGGER.isLoggable(Level.FINER)) {
        SNMP_ADAPTOR_LOGGER.logp(Level.FINER, dbgTag,
            "sendTrapMessage", "sent to " + msg.address + ":" +
              msg.port);
    }
    snmpOutTraps++;
    snmpOutPkts++;
}
 
Example 11
Source File: VDatagramSocket.java    From finalspeed with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void receive(DatagramPacket p) throws IOException {
    TunData td;
    try {
        td = packetList.take();
        p.setData(td.data);
        p.setLength(td.data.length);
        p.setAddress(td.tun.remoteAddress);
        p.setPort(CapEnv.toUnsigned(td.tun.remotePort));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: McastServiceImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
public void init() throws IOException {
    setupSocket();
    sendPacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
    sendPacket.setAddress(address);
    sendPacket.setPort(port);
    receivePacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
    receivePacket.setAddress(address);
    receivePacket.setPort(port);
    member.setCommand(new byte[0]);
    if ( membership == null ) membership = new Membership(member);
}
 
Example 13
Source File: NetworkBridge.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private static int postRecvfrom(boolean isRead, DatagramPacket packet, boolean isConnected, InetSocketAddress srcAddress, int byteCount) {
    if (isRead && byteCount == 0) {
        return -1;
    }
    if (packet != null) {
        packet.setReceivedLength(byteCount);
        if (!isConnected) {
            packet.setAddress(srcAddress.getAddress());
            packet.setPort(srcAddress.getPort());
        }
    }
    return byteCount;
}
 
Example 14
Source File: StunServer.java    From freeacs with MIT License 5 votes vote down vote up
private void setDatagramAddress(ResponseAddress ra, DatagramPacket receive, DatagramPacket send)
    throws UtilityException, UnknownHostException {
  if (ra != null) {
    send.setPort(ra.getPort());
    send.setAddress(ra.getAddress().getInetAddress());
  } else {
    send.setPort(receive.getPort());
    send.setAddress(receive.getAddress());
  }
}
 
Example 15
Source File: DNSTunnel.java    From java-tcp-tunnel with MIT License 5 votes vote down vote up
public void run() {
  String dateStr = sdf.format(new Date());
  byte[] buffer = new byte[65536];
  DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
  while (true) {
    try {
      packet.setData(buffer);
      packet.setLength(buffer.length);
      sourceSocket.receive(packet);
      active = true;
      InetAddress sourceAddress = packet.getAddress();
      int srcPort = packet.getPort();
      InetAddress address = InetAddress.getByName(params.getRemoteHost());
      String strAddr = toStr(packet);
      if (params.isPrint()) {
        System.out.println(dateStr + ": DNS Forwarding " +packet.getLength()+ " bytes " + strAddr + " --> " + address.getHostAddress() + ":" + params.getRemotePort());
      }
      //send client request to server
      packet.setPort(params.getRemotePort());
      packet.setAddress(address);
      Thread tunnel = new DNSForwarder(packet, sourceSocket, sourceAddress, srcPort, params);
      tunnel.start();

    } catch (Throwable e) {
      if (params.isPrint()) {
        String remoteAddr = params.getRemoteHost() + ":" + params.getRemotePort();
        String humanRemoteAddr = Utils.mapAddrToHumanReadable(remoteAddr);
        remoteAddr = remoteAddr + " (" + humanRemoteAddr + ")";
        System.err.println(dateStr + ": Failed to connect to remote host (" + remoteAddr + ")");
        e.printStackTrace();
      }
      connectionBroken();
    }
  }
}
 
Example 16
Source File: McastServiceImpl.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
public void init() throws IOException {
    setupSocket();
    sendPacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
    sendPacket.setAddress(address);
    sendPacket.setPort(port);
    receivePacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
    receivePacket.setAddress(address);
    receivePacket.setPort(port);
    member.setCommand(new byte[0]);
    member.getData(true, true);
    if ( membership == null ) membership = new Membership(member);
}
 
Example 17
Source File: DatagramSocketAdaptor.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void send(DatagramPacket p) throws IOException {
    synchronized (dc.blockingLock()) {
        if (!dc.isBlocking())
            throw new IllegalBlockingModeException();
        try {
            synchronized (p) {
                ByteBuffer bb = ByteBuffer.wrap(p.getData(),
                                                p.getOffset(),
                                                p.getLength());
                if (dc.isConnected()) {
                    if (p.getAddress() == null) {
                        // Legacy DatagramSocket will send in this case
                        // and set address and port of the packet
                        InetSocketAddress isa = (InetSocketAddress)
                                                dc.remoteAddress();
                        p.setPort(isa.getPort());
                        p.setAddress(isa.getAddress());
                        dc.write(bb);
                    } else {
                        // Target address may not match connected address
                        dc.send(bb, p.getSocketAddress());
                    }
                } else {
                    // Not connected so address must be valid or throw
                    dc.send(bb, p.getSocketAddress());
                }
            }
        } catch (IOException x) {
            Net.translateException(x);
        }
    }
}
 
Example 18
Source File: McastServiceImpl.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
public void init() throws IOException {
    setupSocket();
    sendPacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
    sendPacket.setAddress(address);
    sendPacket.setPort(port);
    receivePacket = new DatagramPacket(new byte[MAX_PACKET_SIZE],MAX_PACKET_SIZE);
    receivePacket.setAddress(address);
    receivePacket.setPort(port);
    member.setCommand(new byte[0]);
    member.getData(true, true);
    if ( membership == null ) membership = new Membership(member);
}
 
Example 19
Source File: DatagramSocketEmulator.java    From jmeter-plugins with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void receive(DatagramPacket p) throws IOException {
    log.debug("Emulate receive: " + p);
    if (toRead == null) {
        throw new SocketException();
    }

    p.setData(toRead.getData());
    p.setAddress(toRead.getAddress());
    //p.setSocketAddress(toRead.getSocketAddress());
    toRead = null;
}
 
Example 20
Source File: UPnPGatewayDiscover.java    From open-ig with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void run() {

    try (DatagramSocket ssdp = new DatagramSocket(new InetSocketAddress(ip, 0))) {
        // Create socket bound to specified local address

        byte[] searchMessageBytes = searchMessage.getBytes();
        DatagramPacket ssdpDiscoverPacket = new DatagramPacket(searchMessageBytes, searchMessageBytes.length);
        ssdpDiscoverPacket.setAddress(InetAddress.getByName(broadcastIP));
        ssdpDiscoverPacket.setPort(ssdpPort);

        ssdp.send(ssdpDiscoverPacket);
        ssdp.setSoTimeout(broadcastTimeout);


        boolean waitingPacket = true;
        while (waitingPacket) {
            DatagramPacket receivePacket = new DatagramPacket(new byte[1536], 1536);
            try {
                ssdp.receive(receivePacket);
                byte[] receivedData = new byte[receivePacket.getLength()];
                System.arraycopy(receivePacket.getData(), 0, receivedData, 0, receivePacket.getLength());

                // Create GatewayDevice from response
                UPnPGatewayDevice gateDev = parseMSearchReplay(receivedData);

                gateDev.setLocalAddress(ip);
                gateDev.loadDescription();

                synchronized (devices) {
                    devices.put(ip, gateDev);
                }
            } catch (SocketTimeoutException ste) {
                waitingPacket = false;
            }
        }

    } catch (Throwable e) {
    	Exceptions.add(e);
    }
}