/** * Constructs a new {@code DatagramPacket} object to receive data up to * {@code length} bytes with a specified buffer offset. * * @param data * a byte array to store the read characters. * @param offset * the offset of the byte array where the bytes is written. * @param length * the length of the data. */ public DatagramPacket(byte[] data, int offset, int length) { setData(data, offset, length); }
public void write(String msg) throws IOException { if (null == socket) { createSocket(); } LOG.debug("Sending metric: {}", msg); packet.setData(msg.getBytes(StandardCharsets.UTF_8)); socket.send(packet); }
packet.setData(b); udpSocket.send(packet);
LOGGER.severe("Bytes out > 1000. Equals " + bytesOut.length); p.setData(bytesOut); p.setAddress(remoteHost); p.setPort(remotePort);
tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity()); socket.receive(tmpPacket);
packet.setData(b); udpSocket.send(packet);
private static Map<String,String> getMessages(List<String> fragments, int timeout) throws IOException { long startTime = System.currentTimeMillis(); Map<String,String> results = new HashMap<>(); do { receiveData = new byte[65535]; receivePacket.setData(receiveData); try { serverSocket.receive(receivePacket); String message = new String(receivePacket.getData()).trim(); fragments.stream().forEach(fragment -> { if (message.startsWith(fragment)) { results.put(fragment, message); } }); } catch (SocketTimeoutException e) { //expected so that we keep looping } } while (results.size()<fragments.size() && (System.currentTimeMillis() < (startTime + timeout))); return results; }
public void run() { final byte[] receive_buf=new byte[65535]; DatagramPacket packet=new DatagramPacket(receive_buf, receive_buf.length); DataInput inp; while(sock != null && receiver != null && Thread.currentThread().equals(receiver)) { packet.setData(receive_buf, 0, receive_buf.length); try { sock.receive(packet); inp=new ByteArrayDataInputStream(packet.getData(), packet.getOffset(), packet.getLength()); Message msg=new Message(); msg.readFrom(inp); up(msg); } catch(SocketException socketEx) { break; } catch(Throwable ex) { log.error(Util.getMessage("FailedReceivingPacketFrom"), packet.getSocketAddress(), ex); } } if(log.isTraceEnabled()) log.trace("receiver thread terminated"); }
public void run() { final byte[] receive_buf=new byte[65535]; DatagramPacket packet=new DatagramPacket(receive_buf, receive_buf.length); while(mcast_sock != null && receiver != null && Thread.currentThread().equals(receiver)) { packet.setData(receive_buf, 0, receive_buf.length); try { mcast_sock.receive(packet); DataInput inp=new ByteArrayDataInputStream(packet.getData(), packet.getOffset(), packet.getLength()); Message msg=new Message(); msg.readFrom(inp); if(!Objects.equals(local_addr,msg.getSrc())) // discard discovery request from self up(msg); } catch(SocketException socketEx) { break; } catch(Throwable ex) { log.error(Util.getMessage("FailedReceivingPacketFrom"), packet.getSocketAddress(), ex); } } log.debug("receiver thread terminated"); }
tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity()); socket.receive(tmpPacket);
tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity()); socket.receive(tmpPacket);
sock.setTimeToLive(ttl); reqPckt.setData(MSG_ADDR_REQ_DATA);
/*** * Receives echoed data and returns its length. The data may be divided * up among multiple datagrams, requiring multiple calls to receive. * Also, the UDP packets will not necessarily arrive in the same order * they were sent. * @param data the buffer to receive the input * @param length of the buffer * * @return Length of actual data received. * @throws IOException If an error occurs while receiving the data. ***/ public int receive(byte[] data, int length) throws IOException { __receivePacket.setData(data); __receivePacket.setLength(length); _socket_.receive(__receivePacket); return __receivePacket.getLength(); }
private synchronized void init(RouterContext ctx) { _context = ctx; //_dataBuf = _dataCache.acquire(); Arrays.fill(_data, (byte)0); //_packet = new DatagramPacket(_data, MAX_PACKET_SIZE); // // WARNING - // Doesn't seem like we should have to do this every time, // from reading the DatagramPacket javadocs, // but we get massive corruption without it. _packet.setData(_data); // _isInbound = inbound; _initializeTime = _context.clock().now(); _markedType = -1; _validateCount = 0; _remoteHost = null; _released = false; // clear out some values to make debugging easier via toString() _messageType = -1; _enqueueTime = 0; _receivedTime = 0; _fragmentCount = 0; }
/*** * Sends the specified data to the specified server at the specified port. * * @param data The discard data to send. * @param length The length of the data to send. Should be less than * or equal to the length of the data byte array. * @param host The address of the server. * @param port The service port. * @throws IOException If an error occurs during the datagram send * operation. ***/ public void send(byte[] data, int length, InetAddress host, int port) throws IOException { _sendPacket.setData(data); _sendPacket.setLength(length); _sendPacket.setAddress(host); _sendPacket.setPort(port); _socket_.send(_sendPacket); }