/** * Set the received address and port in the packet. We do this when the * Datagram socket is connected at the native level and the * recvConnnectedDatagramImpl does not update the packet with address from * which the packet was received * * @param packet * the packet to be updated */ private void updatePacketRecvAddress(DatagramPacket packet) { packet.setAddress(connectedAddress); packet.setPort(connectedPort); } }
@Override public void run() { try { InputStream in = localTcpSocket.getInputStream(); InetAddress remoteHost = InetAddress.getByName(remoteUdpHost); process.start(); while (true) { byte[] b = new byte[500]; int s = in.read(b); // if (s == -1) continue; LOGGER.fine("TCP Client:" + new String(b, 0, s, "UTF-8")); DatagramPacket udpPacket = new DatagramPacket(b, s); udpPacket.setAddress(remoteHost); udpPacket.setPort(remoteUdpPort); localUdpSocket.send(udpPacket); } } catch (IOException e) { LOGGER.log(Level.WARNING, "exception", e); } }
@Override public void run() { try { localTcpSocket = serverTcpSocket.accept(); process.start(); InputStream in = localTcpSocket.getInputStream(); InetAddress remoteHost = InetAddress.getByName(remoteUdpHost); while (true) { byte[] b = new byte[500]; int s = in.read(b); // if (s == -1) continue; LOGGER.fine("TCP Server:" + new String(b, 0, s, "UTF-8")); DatagramPacket udpPacket = new DatagramPacket(b, s); udpPacket.setAddress(remoteHost); udpPacket.setPort(remoteUdpPort); localUdpSocket.send(udpPacket); } } catch (IOException e) { LOGGER.log(Level.WARNING, "exception", e); } }
@Override public void run() { byte[] buf = new byte[1024]; DatagramPacket p = new DatagramPacket(buf, 1024); try { while (on) { p.setAddress(remoteHost); p.setPort(remotePort); socket.send(p); try { Thread.sleep(1000); } catch (InterruptedException e) { LOGGER.log(Level.WARNING, "exception", e); } } } catch (IOException e) { LOGGER.log(Level.WARNING, "exception", e); } } }).start();
private static int postRecvfrom(boolean isRead, DatagramPacket packet, boolean isConnected, InetSocketAddress srcAddress, int byteCount) { if (isRead && byteCount == 0) { return -1; } if (packet != null) { packet.setReceivedLength(byteCount); if (!isConnected) { packet.setAddress(srcAddress.getAddress()); packet.setPort(srcAddress.getPort()); } } return byteCount; }
@Override public void run() { try { //System.out.println("Listening for ECHO: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort()); while (true) { DatagramPacket packet = new DatagramPacket(new byte[8], 8); socket.receive(packet); System.out.println("ECHO Packet Received in: " + socket.getLocalAddress().getHostAddress() + ":" + socket.getLocalPort() + " From: " + packet.getAddress().getHostAddress() + ":" + packet.getPort()); for (DatagramListener listener : listeners) { listener.datagramReceived(packet); } packet.setAddress(packet.getAddress()); packet.setPort(packet.getPort()); if (!Arrays.equals(packet.getData(), password)) for (int i = 0; i < 3; i++) socket.send(packet); } } catch (IOException ioe) { if (enabled) { } } }
private void sendList(int bufferCount, byte[] buffer) throws IOException { DataOutputX outter = new DataOutputX(); outter.write(NetCafe.CAFE_N); outter.writeShort(bufferCount); outter.write(buffer); byte[] buff = outter.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); } public boolean debugWrite(String ip, int port, int length) {
private void sendList(int bufferCount, byte[] buffer) throws IOException { DataOutputX outter = new DataOutputX(); outter.write(NetCafe.CAFE_N); outter.writeShort(bufferCount); outter.write(buffer); byte[] buff = outter.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); } public boolean debugWrite(String ip, int port, int length) {
public boolean debugWrite(String ip, int port, int length) { try { DataOutputX out = new DataOutputX(); out.write("TEST".getBytes()); if (length > 4) { out.write(new byte[length - 4]); } byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(InetAddress.getByName(ip)); packet.setPort(port); datagram.send(packet); Logger.println("A124", "Sent " + length + " bytes to " + ip + ":" + port); return true; } catch (IOException e) { Logger.println("A125", "UDP " + e.toString()); return false; } }
public boolean debugWrite(String ip, int port, int length) { try { DataOutputX out = new DataOutputX(); out.write("TEST".getBytes()); if (length > 4) { out.write(new byte[length - 4]); } byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(InetAddress.getByName(ip)); packet.setPort(port); datagram.send(packet); Logger.println("A124", "Sent " + length + " bytes to " + ip + ":" + port); return true; } catch (IOException e) { Logger.println("A125", "UDP " + e.toString()); return false; } } public static void main(String[] args) {
public boolean write(List<byte[]> p) { try { if (server_host == null) return false; DataOutputX out = new DataOutputX(); out.write(NetCafe.CAFE_N); out.writeShort((short) p.size()); for (int i = 0; i < p.size(); i++) { out.write(p.get(i)); } byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); return true; } catch (IOException e) { Logger.println("A123", 10, "UDP", e); return false; } }
public boolean debugWrite(String ip, int port, int length) { try { DataOutputX out = new DataOutputX(); out.write("TEST".getBytes()); if (length > 4) { out.write(new byte[length - 4]); } byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(InetAddress.getByName(ip)); packet.setPort(port); datagram.send(packet); Logger.println("A124", "Sent " + length + " bytes to " + ip + ":" + port); return true; } catch (IOException e) { Logger.println("A125", "UDP " + e.toString()); return false; } } public static void main(String[] args) {
try { tmpPacket.setAddress(null); tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity()); socket.receive(tmpPacket);
try { tmpPacket.setAddress(null); tmpPacket.setData(data.array(), data.arrayOffset(), data.capacity()); socket.receive(tmpPacket);
public boolean write(byte[] p) { try { if (server_host == null) return false; if (p.length > conf.net_udp_packet_max_bytes) { return writeMTU(p, conf.net_udp_packet_max_bytes); } DataOutputX out = new DataOutputX(); out.write(NetCafe.CAFE); out.write(p); byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); return true; } catch (IOException e) { Logger.println("A120", 10, "UDP", e); return false; } }
tmpPacket.setAddress(null);
private void writeMTU(long pkid, int total, int num, int packetSize, byte[] data) throws IOException { DataOutputX out = new DataOutputX(); out.write(NetCafe.CAFE_MTU); out.writeInt(conf.getObjHash()); out.writeLong(pkid); out.writeShort(total); out.writeShort(num); out.writeBlob(data); byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); }
private void writeMTU(long pkid, int total, int num, int packetSize, byte[] data) throws IOException { DataOutputX out = new DataOutputX(); out.write(NetCafe.CAFE_MTU); out.writeInt(conf.getObjHash()); out.writeLong(pkid); out.writeShort(total); out.writeShort(num); out.writeBlob(data); byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); } public void close() {
private void writeMTU(long pkid, int total, int num, int packetSize, byte[] data) throws IOException { DataOutputX out = new DataOutputX(); out.write(NetCafe.CAFE_MTU); out.writeInt(conf.getObjHash()); out.writeLong(pkid); out.writeShort(total); out.writeShort(num); out.writeBlob(data); byte[] buff = out.toByteArray(); DatagramPacket packet = new DatagramPacket(buff, buff.length); packet.setAddress(server_host); packet.setPort(server_port); datagram.send(packet); } public void close() {
tmpPacket.setAddress(null);