本文整理汇总了Java中java.net.DatagramPacket.setLength方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.setLength方法的具体用法?Java DatagramPacket.setLength怎么用?Java DatagramPacket.setLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.setLength方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendTrapMessage
import java.net.DatagramPacket; //导入方法依赖的package包/类
/**
* 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++;
}
示例2: receive
import java.net.DatagramPacket; //导入方法依赖的package包/类
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();
}
}
示例3: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void run() {
try {
InetAddress multicastAddress = InetAddress.getByName(Constants.MULTICAST_ADDRESS);
this.msocket = new MulticastSocket(Constants.SSDP_PORT);
this.msocket.setReuseAddress(true);
this.msocket.joinGroup(multicastAddress);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (!this.terminated) {
this.msocket.receive(packet);
String message = new String(packet.getData(), "UTF-8");
if (message.contains("M-SEARCH")) {
log.debug("Received Search Request from " + packet.getSocketAddress().toString() + ":\n" + message);
if (this.checkSearch(message) && packet.getPort() == 50000) {
log.info("DiscoveryResponse needed");
this.sendDiscoveryResponse(packet.getAddress(), packet.getPort());
}
}
// Reset the length of the packet before reusing it.
packet.setLength(buffer.length);
}
} catch (IOException e) {
log.error("Error while listening for UDP packages", e);
} finally {
this.msocket.close();
}
}
示例4: pipe
import java.net.DatagramPacket; //导入方法依赖的package包/类
private void pipe(DatagramSocket from, DatagramSocket to, boolean out)
throws IOException {
final byte[] data = new byte[datagramSize];
final DatagramPacket dp = new DatagramPacket(data, data.length);
while (true) {
try {
from.receive(dp);
lastReadTime = System.currentTimeMillis();
if (auth.checkRequest(dp, out)) {
to.send(dp);
}
} catch (final UnknownHostException uhe) {
log.info("Dropping datagram for unknown host");
} catch (final InterruptedIOException iioe) {
// log("Interrupted: "+iioe);
// If we were interrupted by other thread.
if (iddleTimeout == 0) {
return;
}
// If last datagram was received, long time ago, return.
final long timeSinceRead = System.currentTimeMillis()
- lastReadTime;
if (timeSinceRead >= iddleTimeout - 100) {
return;
}
}
dp.setLength(data.length);
}
}
示例5: receive
import java.net.DatagramPacket; //导入方法依赖的package包/类
/**
* Receives udp packet. If packet have arrived from the proxy relay server,
* it is processed and address and port of the packet are set to the address
* and port of sending host.<BR>
* If the packet arrived from anywhere else it is not changed.<br>
* <B> NOTE: </B> DatagramPacket size should be at least 10 bytes bigger
* than the largest packet you expect (this is for IPV4 addresses). For
* hostnames and IPV6 it is even more.
*
* @param dp
* Datagram in which all relevent information will be copied.
*/
public void receive(DatagramPacket dp) throws IOException {
super.receive(dp);
if (server_mode) {
// Drop all datagrams not from relayIP/relayPort
final int init_length = dp.getLength();
final int initTimeout = getSoTimeout();
final long startTime = System.currentTimeMillis();
while (!relayIP.equals(dp.getAddress())
|| (relayPort != dp.getPort())) {
// Restore datagram size
dp.setLength(init_length);
// If there is a non-infinit timeout on this socket
// Make sure that it happens no matter how often unexpected
// packets arrive.
if (initTimeout != 0) {
final long passed = System.currentTimeMillis() - startTime;
final int newTimeout = initTimeout - (int) passed;
if (newTimeout <= 0) {
throw new InterruptedIOException(
"In Socks5DatagramSocket->receive()");
}
setSoTimeout(newTimeout);
}
super.receive(dp);
}
// Restore timeout settings
if (initTimeout != 0) {
setSoTimeout(initTimeout);
}
} else if (!relayIP.equals(dp.getAddress())
|| (relayPort != dp.getPort())) {
return; // Recieved direct packet
// If the datagram is not from the relay server, return it it as is.
}
byte[] data;
data = dp.getData();
if (encapsulation != null) {
data = encapsulation.udpEncapsulate(data, false);
}
// FIXME: What is this?
final int offset = 0; // Java 1.1
// int offset = dp.getOffset(); //Java 1.2
final ByteArrayInputStream bIn = new ByteArrayInputStream(data, offset,
dp.getLength());
final ProxyMessage msg = new Socks5Message(bIn);
dp.setPort(msg.port);
dp.setAddress(msg.getInetAddress());
// what wasn't read by the Message is the data
final int data_length = bIn.available();
// Shift data to the left
System.arraycopy(data, offset + dp.getLength() - data_length, data,
offset, data_length);
dp.setLength(data_length);
}