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

The following examples show how to use java.net.DatagramPacket#setPort() . 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: TesterMulticast.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public void run() {
    try (MulticastSocket s = new MulticastSocket(PORT)) {
        s.setLoopbackMode(false);
        s.joinGroup(INET_ADDRESS);
        DatagramPacket p = new DatagramPacket(new byte[4], 4);
        p.setAddress(INET_ADDRESS);
        p.setPort(PORT);
        while (run) {
            s.receive(p);
            String d = new String (p.getData());
            System.out.println("Rx: " + d);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: SnmpAdaptorServer.java    From jdk1.8-source-analysis with Apache License 2.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 3
Source File: DataMessage.java    From finalspeed-91yun with GNU General Public License v2.0 6 votes vote down vote up
public void create(int timeId){
	this.timeId=timeId;
	dpData=new byte[this.length+16+8];
	ByteShortConvert.toByteArray(ver, dpData, 0);  //add: ver
	ByteShortConvert.toByteArray(sType, dpData, 2);  //add: service type
	
	ByteIntConvert.toByteArray(connectId, dpData, 4); //add: sequence
	ByteIntConvert.toByteArray(clientId, dpData, 8); //add: sequence
	
	ByteIntConvert.toByteArray(this.sequence, dpData, 12); //add: sequence
	ByteShortConvert.toByteArray((short) this.length, dpData, 16); //add:length
	ByteIntConvert.toByteArray(this.timeId, dpData, 18); //add: sequence
	System.arraycopy(this.data, 0, dpData, 22, this.length);
	dp=new DatagramPacket(dpData,dpData.length);
	dp.setAddress(dstAddress);
	dp.setPort(dstPort);
	
}
 
Example 4
Source File: SnmpAdaptorServer.java    From dragonwell8_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: DataMessage.java    From finalspeed with GNU General Public License v2.0 6 votes vote down vote up
public void create(int timeId) {
    this.timeId = timeId;
    dpData = new byte[this.length + 16 + 8];
    ByteShortConvert.toByteArray(ver, dpData, 0);  //add: ver
    ByteShortConvert.toByteArray(sType, dpData, 2);  //add: service type

    ByteIntConvert.toByteArray(connectId, dpData, 4); //add: sequence
    ByteIntConvert.toByteArray(clientId, dpData, 8); //add: sequence

    ByteIntConvert.toByteArray(this.sequence, dpData, 12); //add: sequence
    ByteShortConvert.toByteArray((short) this.length, dpData, 16); //add:length
    ByteIntConvert.toByteArray(this.timeId, dpData, 18); //add: sequence
    System.arraycopy(this.data, 0, dpData, 22, this.length);
    dp = new DatagramPacket(dpData, dpData.length);
    dp.setAddress(dstAddress);
    dp.setPort(dstPort);

}
 
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 JDKSourceCode1.8 with MIT License 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 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 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: 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 12
Source File: SnmpAdaptorServer.java    From jdk8u-dev-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 13
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 14
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 15
Source File: VDatagramSocket.java    From finalspeed-91yun with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void receive(DatagramPacket p) throws IOException {
	TunData td=null;
	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 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: 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 18
Source File: SimpleNTPServer.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
private void handleRequest(DatagramPacket requestPacket) throws IOException {
    final long receivedTime = System.currentTimeMillis();
    NtpV3Packet responsePacket = new NtpV3Impl();
    responsePacket.setMode(NtpV3Packet.MODE_SERVER);
    responsePacket.setTransmitTime(TimeStamp.getNtpTime(receivedTime));
    DatagramPacket dataPacket = responsePacket.getDatagramPacket();
    dataPacket.setPort(requestPacket.getPort());
    dataPacket.setAddress(requestPacket.getAddress());
    socket.send(dataPacket);
}
 
Example 19
Source File: SyslogClient.java    From freeacs with MIT License 5 votes vote down vote up
public static void send(String msg) throws IOException {
  DatagramSocket socket = new DatagramSocket();
  byte[] message = msg.getBytes(StandardCharsets.UTF_8);
  DatagramPacket packet = new DatagramPacket(message, message.length);
  InetAddress address = InetAddress.getByName(SYSLOG_SERVER_HOST);
  packet.setPort(9116);
  packet.setAddress(address);
  socket.send(packet);
  socket.close();
}
 
Example 20
Source File: VDatagramSocket.java    From NSS with Apache License 2.0 5 votes vote down vote up
public synchronized void receive(DatagramPacket p) throws IOException {
	TunData td=null;
	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();
	}
}