/* * 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; } }
static String printSocket(DatagramSocket socket) { return "[Socket: localPort=" + socket.getLocalPort() + ", remoteAddr=" + socket.getInetAddress() + ", localAddr=" + socket.getLocalAddress() + "]"; }
void closeSocket() { if (sock != null) { // by sending a dummy packet, the thread will terminate (if it was flagged as stopped before) sendDummyPacket(sock.getLocalAddress(), sock.getLocalPort()); sock.close(); sock = null; if (Trace.trace) { Trace.info("UDP.closeSocket()", "socket closed"); } } }
/** * The method queries a Stun server for a binding for the port and address that <tt>sock</tt> is * bound on. * * @param sock the socket whose port and address we'dlike to resolve (the stun message gets sent * trhough that socket) * @return StunAddress the address returned by the stun server or null if an error occurred or no * address was returned * @throws IOException if an error occurs while stun4j is using sockets. * @throws BindException if the port is already in use. */ private StunAddress queryStunServer(DatagramSocket sock) throws IOException, BindException { StunAddress mappedAddress = null; if (detector != null && useStun) { mappedAddress = detector.getMappingFor(sock); if (logger.isTraceEnabled()) { logger.trace( "For socket with address " + sock.getLocalAddress().getHostAddress() + " and port " + sock.getLocalPort() + " the stun server returned the " + "following mapping [" + mappedAddress + "]"); } } return mappedAddress; }
String dumpSocketInfo() throws Exception { StringBuffer sb = new StringBuffer(); sb.append("local_addr=").append(local_addr); sb.append(", mcast_addr=").append(mcast_addr); sb.append(", bind_addr=").append(bind_addr); sb.append(", ttl=").append(ip_ttl); if (sock != null) { sb.append("\nsocket: bound to "); sb.append(sock.getLocalAddress().getHostAddress()).append(":").append(sock.getLocalPort()); sb.append(", receive buffer size=").append(sock.getReceiveBufferSize()); sb.append(", send buffer size=").append(sock.getSendBufferSize()); } if (mcast_sock != null) { sb.append("\nmulticast socket: bound to "); sb.append(mcast_sock.getInterface().getHostAddress()) .append(":") .append(mcast_sock.getLocalPort()); sb.append(", send buffer size=").append(mcast_sock.getSendBufferSize()); sb.append(", receive buffer size=").append(mcast_sock.getReceiveBufferSize()); } return sb.toString(); }
/** * Create UDP sender and receiver sockets. Currently there are 2 sockets (sending and receiving). * This is due to Linux's non-BSD compatibility in the JDK port (see DESIGN). */ void createSockets() throws Exception { InetAddress tmp_addr = null; // bind_addr not set, try to assign one by default. This is needed on Windows // changed by bela Feb 12 2003: by default multicast sockets will be bound to all network // interfaces // CHANGED *BACK* by bela March 13 2003: binding to all interfaces did not result in a correct // local_addr. As a matter of fact, comparison between e.g. 0.0.0.0:1234 (on hostA) and // 0.0.0.0:1.2.3.4 (on hostB) would fail ! if (bind_addr == null) { InetAddress[] interfaces = InetAddress.getAllByName(InetAddress.getLocalHost().getHostAddress()); if (interfaces != null && interfaces.length > 0) bind_addr = interfaces[0]; } if (bind_addr == null) bind_addr = InetAddress.getLocalHost(); if (bind_addr != null && Trace.trace) { Trace.info( "UDP.createSockets()", "unicast sockets will use interface " + bind_addr.getHostAddress()); // 2. Create socket for receiving unicast UDP packets. The address and port // of this socket will be our local address (local_addr) // 27-6-2003 bgooren, find available port in range (start_port, start_port+port_range) } int rcv_port = bind_port, max_port = bind_port + port_range; while (rcv_port <= max_port) { try { sock = new DatagramSocket(rcv_port, bind_addr); break; } catch (SocketException bind_ex) { // Cannot listen on this port rcv_port++; } catch (SecurityException sec_ex) { // Not allowed to list on this port rcv_port++; } // Cannot listen at all, throw an Exception if (rcv_port == max_port + 1) { // +1 due to the increment above throw new Exception( "UDP.createSockets(): cannot list on any port in range " + bind_port + "-" + (bind_port + port_range)); } } // ucast_recv_sock=new DatagramSocket(bind_port, bind_addr); if (sock == null) { throw new Exception("UDP.createSocket(): sock is null"); } local_addr = new IpAddress(sock.getLocalAddress(), sock.getLocalPort()); if (additional_data != null) { local_addr.setAdditionalData(additional_data); // 3. Create socket for receiving IP multicast packets } if (ip_mcast) { mcast_sock = new MulticastSocket(mcast_port); mcast_sock.setTimeToLive(ip_ttl); if (bind_addr != null) { mcast_sock.setInterface(bind_addr); } tmp_addr = InetAddress.getByName(mcast_addr_name); mcast_addr = new IpAddress(tmp_addr, mcast_port); mcast_sock.joinGroup(tmp_addr); } setBufferSizes(); if (Trace.trace) { Trace.info("UDP.createSockets()", "socket information:\n" + dumpSocketInfo()); } }