本文整理汇总了Java中java.net.DatagramPacket.getOffset方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.getOffset方法的具体用法?Java DatagramPacket.getOffset怎么用?Java DatagramPacket.getOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.getOffset方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void run()
{
byte[] abyte = new byte[1024];
while (!this.isInterrupted())
{
DatagramPacket datagrampacket = new DatagramPacket(abyte, abyte.length);
try
{
this.socket.receive(datagrampacket);
}
catch (SocketTimeoutException var5)
{
continue;
}
catch (IOException ioexception)
{
LanServerDetector.logger.error((String)"Couldn\'t ping server", (Throwable)ioexception);
break;
}
String s = new String(datagrampacket.getData(), datagrampacket.getOffset(), datagrampacket.getLength());
LanServerDetector.logger.debug(datagrampacket.getAddress() + ": " + s);
this.localServerList.func_77551_a(s, datagrampacket.getAddress());
}
try
{
this.socket.leaveGroup(this.broadcastAddress);
}
catch (IOException var4)
{
;
}
this.socket.close();
}
示例2: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
@Override
public void run() {
byte[] buffer = new byte[65535];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
try {
socket.receive(packet);
byte channel = buffer[packet.getOffset()];
byte[] data = new byte[packet.getLength() - 1];
System.arraycopy(buffer, packet.getOffset() + 1, data, 0, data.length);
if (channel > 0 && ROUTES.containsKey(channel)) {
ROUTES.get(channel).send(data);
}
else {
switch (channel) {
case CHANNEL_KILL_SOCKET: {
byte target = data[0];
ROUTES.get(target).killSocket();
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
示例3: main
import java.net.DatagramPacket; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
byte b1[] = new byte[1024];
DatagramPacket p = new DatagramPacket(b1, 512, 512 );
byte b2[] = new byte[20];
p.setData(b2);
if (p.getOffset() != 0) {
throw new Exception("setData(byte[]) didn't reset offset");
}
}
示例4: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
public void run()
{
byte[] abyte = new byte[1024];
while (!this.isInterrupted())
{
DatagramPacket datagrampacket = new DatagramPacket(abyte, abyte.length);
try
{
this.socket.receive(datagrampacket);
}
catch (SocketTimeoutException var5)
{
continue;
}
catch (IOException ioexception)
{
LanServerDetector.LOGGER.error((String)"Couldn\'t ping server", (Throwable)ioexception);
break;
}
String s = new String(datagrampacket.getData(), datagrampacket.getOffset(), datagrampacket.getLength());
LanServerDetector.LOGGER.debug("{}: {}", new Object[] {datagrampacket.getAddress(), s});
this.localServerList.addServer(s, datagrampacket.getAddress());
}
try
{
this.socket.leaveGroup(this.broadcastAddress);
}
catch (IOException var4)
{
;
}
this.socket.close();
}
示例5: executeDiscovery
import java.net.DatagramPacket; //导入方法依赖的package包/类
private void executeDiscovery() {
try {
final InetAddress inetAddress = InetAddress.getByName(MCAST_GROUP);
final byte[] datagramBuffer = new byte[512];
discoverySocket = new MulticastSocket(DISCOVERY_SOCKET_PORT);
discoverySocket.setSoTimeout(0);
discoverySocket.setReuseAddress(true);
discoverySocket.joinGroup(inetAddress);
while (!Thread.currentThread().isInterrupted()) {
final DatagramPacket recv = new DatagramPacket(datagramBuffer, datagramBuffer.length);
discoverySocket.receive(recv);
final String responseMessage = new String(recv.getData(), recv.getOffset(), recv.getLength());
final ZMoteDevice zmoteDevice = parseDiscoveryResponse(responseMessage);
if (zmoteDevice != null) {
discoveryResults.put(zmoteDevice.getUuid(), new ZMoteDiscoveryResult(zmoteDevice));
notifyDiscovery(zmoteDevice);
if (logger.isDebugEnabled()) {
logger.debug("Discovered ZMote device: {}", zmoteDevice.toString());
}
} else if (logger.isDebugEnabled()) {
logger.debug("Discovered unsupported device: {}", responseMessage);
}
}
} catch (final Exception e) {
if ((e instanceof SocketException) && Thread.currentThread().isInterrupted()) {
if (logger.isDebugEnabled()) {
logger.debug("Discovery service worker has been terminated.");
}
} else if (logger.isWarnEnabled()) {
logger.warn("ZMote device discovery failed!", e);
}
} finally {
safeCloseDiscoverySocket();
}
}