udpSocket.send(packet); packet.setLength(b.length);
requestBuffer.put(zeroes); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr); requestBuffer.put(zeroes); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr); requestBuffer.put(zeroes); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr); requestBuffer.putLong(m.epoch); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr);
packet.setLength(cont.length); packet.setAddress(InetAddress.getByName(ip)); packet.setPort(Integer.parseInt(port));
udpSocket.send(packet); packet.setLength(b.length);
requestBuffer.put(zeroes); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr); requestBuffer.put(zeroes); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr); requestBuffer.put(zeroes); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr); requestBuffer.putLong(m.epoch); requestPacket.setLength(48); try { requestPacket.setSocketAddress(m.addr);
public void run() { final byte receive_buf[]=new byte[66000]; // to be on the safe side (IPv6 == 65575 bytes, IPv4 = 65535) final DatagramPacket packet=new DatagramPacket(receive_buf, receive_buf.length); while(thread != null && Thread.currentThread().equals(thread)) { try { // solves Android ISSUE #24748 - DatagramPacket truncated UDP in ICS if(is_android) packet.setLength(receive_buf.length); receiver_socket.receive(packet); int len=packet.getLength(); if(len > receive_buf.length && log.isErrorEnabled()) log.error(Util.getMessage("SizeOfTheReceivedPacket"), len, receive_buf.length, receive_buf.length); receive(new IpAddress(packet.getAddress(), packet.getPort()), receive_buf, packet.getOffset(), len); } catch(SocketException sock_ex) { if(receiver_socket.isClosed()) { log.debug("receiver socket is closed, exception=" + sock_ex); break; } log.error(Util.getMessage("FailedReceivingPacket"), sock_ex); } catch(Throwable ex) { log.error(Util.getMessage("FailedReceivingPacket"), ex); } } if(log.isDebugEnabled()) log.debug(name + " thread terminated"); }
byte[] b = new byte[BUFFER_LENGTH]; DatagramPacket dgram = new DatagramPacket(b, b.length); MulticastSocket socket = new MulticastSocket(DEST_PORT); // must bind receive side socket.joinGroup(InetAddress.getByName(MCAST_ADDR)); while(true) { socket.receive(dgram); // blocks until a datagram is received System.err.println("Received " + dgram.getLength() + " bytes from " + dgram.getAddress()); dgram.setLength(b.length); // must reset length field! }
/*** * 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 DiscoveryIncomingMessage receiveMessage() { byte buf[] = new byte[AbstractDiscoveryMessage.MAX_MSG_SIZE]; DatagramPacket packet = new DatagramPacket(buf, buf.length); try { packet.setLength(buf.length); socket.receive(packet); return new DiscoveryIncomingMessage(packet); } catch (IOException e) { if (!socket.isClosed()) { logHandler.info("Error while handling discovery request" + (packet.getAddress() != null ? " from " + packet.getAddress() : "") + ". Ignoring this request. --> " + e); } return null; } }
/*** * This is a method only available within the package for * implementing efficient datagram transport by elminating buffering. * It takes a datagram as an argument, and a byte buffer in which * to store the raw datagram data. Inside the method, the data * is set as the datagram's data and the datagram returned. * * @param datagram The datagram to create. * @param data The buffer to store the packet and to use in the datagram. * @return The datagram argument. ***/ @Override final DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) { int fileLength, modeLength; fileLength = _filename.length(); modeLength = _modeBytes[_mode].length; data[0] = 0; data[1] = (byte)_type; System.arraycopy(_filename.getBytes(), 0, data, 2, fileLength); data[fileLength + 2] = 0; System.arraycopy(_modeBytes[_mode], 0, data, fileLength + 3, modeLength); datagram.setAddress(_address); datagram.setPort(_port); datagram.setData(data); datagram.setLength(fileLength + modeLength + 3); return datagram; }
/** * TESTING ONLY. * Creates an arbitrary packet for unit testing. * Null transport in constructor OK. * * @since IPv6 */ public UDPPacket buildPacket(byte[] data, InetAddress to, int port) { UDPPacket packet = UDPPacket.acquire(_context, false); byte d[] = packet.getPacket().getData(); System.arraycopy(data, 0, d, 0, data.length); packet.getPacket().setLength(data.length); setTo(packet, to, port); return packet; }
/*** * This is a method only available within the package for * implementing efficient datagram transport by elminating buffering. * It takes a datagram as an argument, and a byte buffer in which * to store the raw datagram data. Inside the method, the data * is set as the datagram's data and the datagram returned. * * @param datagram The datagram to create. * @param data The buffer to store the packet and to use in the datagram. * @return The datagram argument. ***/ @Override DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) { int length; length = _message.length(); data[0] = 0; data[1] = (byte)_type; data[2] = (byte)((_error & 0xffff) >> 8); data[3] = (byte)(_error & 0xff); System.arraycopy(_message.getBytes(), 0, data, 4, length); data[length + 4] = 0; datagram.setAddress(_address); datagram.setPort(_port); datagram.setData(data); datagram.setLength(length + 4); return datagram; }
/*** * 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); }
/*** * This is a method only available within the package for * implementing efficient datagram transport by elminating buffering. * It takes a datagram as an argument, and a byte buffer in which * to store the raw datagram data. Inside the method, the data * is set as the datagram's data and the datagram returned. * * @param datagram The datagram to create. * @param data The buffer to store the packet and to use in the datagram. * @return The datagram argument. ***/ @Override DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) { data[0] = 0; data[1] = (byte)_type; data[2] = (byte)((_blockNumber & 0xffff) >> 8); data[3] = (byte)(_blockNumber & 0xff); // Doublecheck we're not the same if (data != _data) { System.arraycopy(_data, _offset, data, 4, _length); } datagram.setAddress(_address); datagram.setPort(_port); datagram.setData(data); datagram.setLength(_length + 4); return datagram; }
/*** * This is a method only available within the package for * implementing efficient datagram transport by elminating buffering. * It takes a datagram as an argument, and a byte buffer in which * to store the raw datagram data. Inside the method, the data * is set as the datagram's data and the datagram returned. * * @param datagram The datagram to create. * @param data The buffer to store the packet and to use in the datagram. * @return The datagram argument. ***/ @Override DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) { data[0] = 0; data[1] = (byte)_type; data[2] = (byte)((_blockNumber & 0xffff) >> 8); data[3] = (byte)(_blockNumber & 0xff); datagram.setAddress(_address); datagram.setPort(_port); datagram.setData(data); datagram.setLength(4); return datagram; }
/** * Build a destroy packet, which contains a header but no body. * @param cipherKey non-null * @param macKey non-null * @since 0.9.2 */ private UDPPacket buildSessionDestroyPacket(SessionKey cipherKey, SessionKey macKey, InetAddress addr, int port) { UDPPacket packet = buildPacketHeader(SESSION_DESTROY_FLAG_BYTE); int off = HEADER_SIZE; // no body in this message // pad up so we're on the encryption boundary DatagramPacket pkt = packet.getPacket(); byte data[] = pkt.getData(); off = pad1(data, off); off = pad2(data, off); pkt.setLength(off); authenticate(packet, cipherKey, macKey); setTo(packet, addr, port); return packet; }
/** * Creates an empty unauthenticated packet for hole punching. * Parameters must be validated previously. */ public UDPPacket buildHolePunch(InetAddress to, int port) { UDPPacket packet = UDPPacket.acquire(_context, false); if (_log.shouldLog(Log.INFO)) _log.info("Sending relay hole punch to " + to + ":" + port); // the packet is empty and does not need to be authenticated, since // its just for hole punching packet.getPacket().setLength(0); setTo(packet, to, port); packet.setMessageType(TYPE_PUNCH); return packet; }
pkt.setLength(off); authenticate(packet, charlieCipherKey, charlieMACKey); setTo(packet, charlieIP, charliePort);
pkt.setLength(off); authenticate(packet, charlie.getCurrentCipherKey(), charlie.getCurrentMACKey()); setTo(packet, charlie.getRemoteIPAddress(), charlie.getRemotePort());