/* * Connect a UDP socket, disconnect it, then send and recv on it. * It will fail on Linux if we don't silently bind(2) again at the * end of DatagramSocket.disconnect(). */ private static void testConnectedUDP(InetAddress addr) throws Exception { try { DatagramSocket s = new DatagramSocket(0, addr); DatagramSocket ss = new DatagramSocket(0, addr); System.out.print("\tconnect..."); s.connect(ss.getLocalAddress(), ss.getLocalPort()); System.out.print("disconnect..."); s.disconnect(); byte[] data = {0, 1, 2}; DatagramPacket p = new DatagramPacket(data, data.length, s.getLocalAddress(), s.getLocalPort()); s.setSoTimeout(10000); System.out.print("send..."); s.send(p); System.out.print("recv..."); s.receive(p); System.out.println("OK"); ss.close(); s.close(); } catch (Exception e) { e.printStackTrace(); throw e; } }
public void FlushConnection() { try { socket.disconnect(); } catch (Exception e) { e.printStackTrace(); } }
// Discovery thread @Override public void run() { while (!Thread.currentThread().isInterrupted()) { try { socket = new DatagramSocket(DISCOVERY_PORT, InetAddress.getByName("0.0.0.0")); socket.setBroadcast(true); // Keep an UDP Socket open while (!Thread.currentThread().isInterrupted() && socket.isBound()) { // Ready to receive sockets // Receive a packet byte[] rcvBuff = new byte[15000]; DatagramPacket packet = new DatagramPacket(rcvBuff, rcvBuff.length); socket.receive(packet); // Packet received String msg = new String(packet.getData()).trim(); if (msg.equals("REMOTECRAFT_DISCOVERY_REQUEST")) { // Attach world name String msgResponse = "REMOTECRAFT_DISCOVERY_RESPONSE:" + String.valueOf(this.worldSeed) + "_" + this.worldName; byte[] sendData = msgResponse.getBytes(); // Send response DatagramPacket sendPacket = new DatagramPacket( sendData, sendData.length, packet.getAddress(), packet.getPort()); if (socket.isBound()) { try { socket.send(sendPacket); } catch (Exception e) { e.printStackTrace(); } } else { continue; } } } } catch (IOException e) { e.printStackTrace(); } finally { try { socket.disconnect(); socket.close(); } catch (NullPointerException e) { e.printStackTrace(); } } } // while !threadIsInterrupted }
/** * Forces the instant sending of a packet, ignoring the present packet queue. * * @param packet */ public void forceInstantSend(DatagramPacket packet) { if (m_socket.isConnected() == true && !interrupted()) { try { m_socket.send(packet); m_packetsSent++; } catch (Exception e) { m_socket.disconnect(); } } }
/** stop the UDP server, clean up and delete its reference. */ public void dispose() { isRunning = false; _myThread = null; if (_myDatagramSocket != null) { if (_myDatagramSocket.isConnected()) { Logger.printDebug("UdpServer.dispose()", "disconnect()"); _myDatagramSocket.disconnect(); } Logger.printDebug("UdpServer.dispose()", "close()"); _myDatagramSocket.close(); _myDatagramSocket = null; Logger.printDebug("UdpServer.dispose()", "Closing unicast datagram socket."); } }
/** * A synchronized function for closing the UPD socket. It prevents the situation in which both the * listener thread and the GUI thread close the socket at the same time. */ private synchronized void closeSocket() { if (ds != null) { if (ds.isConnected()) { ds.disconnect(); } if (!ds.isClosed()) { ds.close(); } ds = null; if (Debug.SHOW_PROXY_VEHICLE_DEBUG_MSG) { System.err.println("The UDP socket is closed."); } } }
/** * Disconnects from Avatar. * * @throws IOException if the connection to Avatar could not be disconnected. */ private void disconnect() throws IOException { // Do nothing if we are already disconnected if (socket == null) { return; } // disconnect. try { feedback.interrupt(); feedback = null; socket.disconnect(); socket.close(); } finally { socket = null; feedback = null; } LOGGER.info("diconnected from Avatar"); }
/** * Returns an InetAddress instance that represents the localhost, and that a socket can bind upon * or distribute to peers as a contact address. * * @param intendedDestination the destination that we'd like to use the localhost address with. * @return an InetAddress instance representing the local host, and that a socket can bind upon or * distribute to peers as a contact address. */ public synchronized InetAddress getLocalHost(InetAddress intendedDestination) { // no point in making sure that the localHostFinderSocket is initialized. // better let it through a NullPointerException. InetAddress localHost = null; localHostFinderSocket.connect(intendedDestination, this.RANDOM_ADDR_DISC_PORT); localHost = localHostFinderSocket.getLocalAddress(); localHostFinderSocket.disconnect(); // windows socket implementations return the any address so we need to // find something else here ... InetAddress.getLocalHost seems to work // better on windows so lets hope it'll do the trick. if (localHost.isAnyLocalAddress()) { try { // all that's inside the if is an ugly IPv6 hack // (good ol' IPv6 - always causing more problems than it solves.) if (intendedDestination instanceof Inet6Address) { // return the first globally routable ipv6 address we find // on the machine (and hope it's a good one) Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface iface = (NetworkInterface) interfaces.nextElement(); Enumeration addresses = iface.getInetAddresses(); while (addresses.hasMoreElements()) { InetAddress address = (InetAddress) addresses.nextElement(); if (address instanceof Inet6Address) { if (!address.isAnyLocalAddress() && !address.isLinkLocalAddress() && !address.isSiteLocalAddress() && !address.isLoopbackAddress()) { return address; } } } } } else localHost = InetAddress.getLocalHost(); /** @todo test on windows for ipv6 cases */ } catch (Exception ex) { // sigh ... ok return 0.0.0.0 logger.warn("Failed to get localhost ", ex); } } return localHost; }
/** Checks the queue for packets waiting to be sent, and sends any that are in waiting. */ public void run() { while (m_socket.isConnected() == true && !interrupted()) { try { if (m_highPriorityPackets.isEmpty() == false) { m_socket.send(m_highPriorityPackets.remove(0)); m_packetsSent++; Thread.sleep(1); } else if (m_packets.isEmpty() == false) { m_socket.send(m_packets.remove(0)); m_packetsSent++; Thread.sleep(2); } else { Thread.sleep(5); } } catch (Exception e) { m_socket.disconnect(); } } }
public void closeSocket() { // s4s: add by snmp4sdn, because in junit test, occurs // "java.net.BindException: Address already in use: Cannot bind" if (dSocket == null) return; if (dSocket.isConnected()) dSocket.disconnect(); dSocket.close(); }
@Override public void disconnect() { clientSide.disconnect(); getCallback().onDisconnect(); }