本文整理汇总了Java中java.net.DatagramPacket.setData方法的典型用法代码示例。如果您正苦于以下问题:Java DatagramPacket.setData方法的具体用法?Java DatagramPacket.setData怎么用?Java DatagramPacket.setData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.net.DatagramPacket
的用法示例。
在下文中一共展示了DatagramPacket.setData方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mockServer
import java.net.DatagramPacket; //导入方法依赖的package包/类
/**
* MockServer plays the role of peer C. Respond to two requests for votes
* with vote for self and then Assert.fail.
*/
void mockServer() throws InterruptedException, IOException {
byte b[] = new byte[36];
ByteBuffer responseBuffer = ByteBuffer.wrap(b);
DatagramPacket packet = new DatagramPacket(b, b.length);
QuorumServer server = peers.get(Long.valueOf(2));
DatagramSocket udpSocket = new DatagramSocket(server.addr.getPort());
LOG.info("In MockServer");
mockLatch.countDown();
Vote current = new Vote(2, 1);
for (int i=0;i<2;++i) {
udpSocket.receive(packet);
responseBuffer.rewind();
LOG.info("Received " + responseBuffer.getInt() + " " + responseBuffer.getLong() + " " + responseBuffer.getLong());
LOG.info("From " + packet.getSocketAddress());
responseBuffer.clear();
responseBuffer.getInt(); // Skip the xid
responseBuffer.putLong(2);
responseBuffer.putLong(current.getId());
responseBuffer.putLong(current.getZxid());
packet.setData(b);
udpSocket.send(packet);
}
}
示例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: sendData
import java.net.DatagramPacket; //导入方法依赖的package包/类
private boolean sendData(byte[] data) {
DatagramPacket packet = new DatagramPacket(sendBuffer, DAEMON_BUF_RECEIVE_SIZE, address);
packet.setData(data);
try {
logger.debug("Sending UDP packet.");
daemonSocket.send(packet);
} catch (IOException e) {
logger.error("Exception while sending segment over UDP.", e);
return false;
}
return true;
}
示例4: 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");
}
}
示例5: run
import java.net.DatagramPacket; //导入方法依赖的package包/类
@Override
public void run()
{
byte[] buf = new byte[512];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
while (true)
{
try
{
checkSocket();
packet.setData(buf);
try {
socket.receive(packet);
processNetworkData(packet.getAddress(), buf, packet.getLength());
} catch (SocketTimeoutException ste) {
}
checkForTimeouts();
}
catch (Exception e)
{
log.log(Level.WARNING, "error in receiverthread: " + e, e);
try { Thread.sleep(1000); } catch (InterruptedException ie) {}
}
}
}
示例6: prepareDatagramPacket
import java.net.DatagramPacket; //导入方法依赖的package包/类
private void prepareDatagramPacket(InetAddress address, int port){
byte[] buffer = processOutputData();
dataPacket = new DatagramPacket(buffer,buffer.length, address, port);
dataPacket.setData(buffer);
}