Java Code Examples for java.net.DatagramSocket#isConnected()

The following examples show how to use java.net.DatagramSocket#isConnected() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PortScanUDP.java    From HttpInfo with Apache License 2.0 6 votes vote down vote up
public static PortBean.PortNetBean scanAddress(InetAddress ia, int portNo, int timeoutMillis) {
    PortBean.PortNetBean portNetBean = new PortBean.PortNetBean();
    Long time = System.currentTimeMillis();
    try {
        byte[] bytes = new byte[128];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

        DatagramSocket ds = new DatagramSocket();
        ds.setSoTimeout(timeoutMillis);
        ds.connect(ia, portNo);
        ds.send(dp);
        ds.isConnected();
        ds.receive(dp);
        ds.close();
        portNetBean.setConnected(true);
    } catch (SocketTimeoutException e) {

    } catch (Exception ignore) {

    }
    portNetBean.setPort(portNo);
    portNetBean.setDelay(System.currentTimeMillis() - time);
    return portNetBean;
}
 
Example 2
Source File: PortScanUDP.java    From AndroidNetworkTools with Apache License 2.0 6 votes vote down vote up
/**
 * Check if a port is open with UDP, note that this isn't reliable
 * as UDP will does not send ACKs
 *
 * @param ia            - address to scan
 * @param portNo        - port to scan
 * @param timeoutMillis - timeout
 * @return - true if port is open, false if not or unknown
 */
public static boolean scanAddress(InetAddress ia, int portNo, int timeoutMillis) {

    try {
        byte[] bytes = new byte[128];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);

        DatagramSocket ds = new DatagramSocket();
        ds.setSoTimeout(timeoutMillis);
        ds.connect(ia, portNo);
        ds.send(dp);
        ds.isConnected();
        ds.receive(dp);
        ds.close();

    } catch (SocketTimeoutException e) {
        return true;
    } catch (Exception ignore) {

    }

    return false;
}
 
Example 3
Source File: UDPTransport.java    From RipplePower with Apache License 2.0 5 votes vote down vote up
public UDPTransport(DatagramSocket socket, int mtu)
    throws IOException
{
    if (!socket.isBound() || !socket.isConnected())
    {
        throw new IllegalArgumentException("'socket' must be bound and connected");
    }

    this.socket = socket;

    // NOTE: As of JDK 1.6, can use NetworkInterface.getMTU

    this.receiveLimit = mtu - MIN_IP_OVERHEAD - UDP_OVERHEAD;
    this.sendLimit = mtu - MAX_IP_OVERHEAD - UDP_OVERHEAD;
}
 
Example 4
Source File: UDPChecker.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean check(InetSocketAddress address) throws IOException {
    DatagramSocket socket = null;
    try {
        socket = createSocket(address);

        return socket.isConnected();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(socket);
    }
    return false;
}
 
Example 5
Source File: UDPTransport.java    From ripple-lib-java with ISC License 5 votes vote down vote up
public UDPTransport(DatagramSocket socket, int mtu)
    throws IOException
{
    if (!socket.isBound() || !socket.isConnected())
    {
        throw new IllegalArgumentException("'socket' must be bound and connected");
    }

    this.socket = socket;

    // NOTE: As of JDK 1.6, can use NetworkInterface.getMTU

    this.receiveLimit = mtu - MIN_IP_OVERHEAD - UDP_OVERHEAD;
    this.sendLimit = mtu - MAX_IP_OVERHEAD - UDP_OVERHEAD;
}
 
Example 6
Source File: UdpClient.java    From mts with GNU General Public License v3.0 4 votes vote down vote up
public void run()
{
	try
	{
		DatagramSocket datagramSocket = new DatagramSocket(0/*(int) UdpTest.SERVER_PORT + 1*/, Inet4Address.getByName(UdpTest.SERVER_HOST));
		//DatagramSocket datagramSocket = new DatagramSocket(null); // null->unbound
		System.out.println("UdpClient: client ready");

		boolean connect = false; // true ; "connect�" ou non 

		byte[] data = new byte[(int)UdpTest.MSG_SIZE];

		for(int i=0; i<data.length; i++)
		{
			data[i] = (byte)i;// 0;
		}

		DatagramPacket datagramPacket = new DatagramPacket(data,data.length,Inet4Address.getByName(UdpTest.SERVER_HOST),(int) UdpTest.SERVER_PORT); 
		//DatagramPacket datagramPacket = new DatagramPacket(data,data.length,Inet4Address.getByName(UdpTest.SERVER_HOST),3333);
		//DatagramPacket datagramPacket = new DatagramPacket(data,data.length);
		
		System.out.println("UdpClient: data initialized");
		
		if (connect){
			datagramSocket.connect(Inet4Address.getByName(UdpTest.SERVER_HOST),(int) UdpTest.SERVER_PORT);	
			if (datagramSocket.isConnected()) System.out.println("UdpClient: datagramSocket connected");
		}
		
		UdpTest.start = System.currentTimeMillis();
					
		//for(int i=0; i<UdpTest.MSG_NUMBER; i++)
		{

			//System.out.println( "sending: " + i);

			//for(int j=0; j<data.length; j++)
			//	System.out.print(datagramPacket.getData()[j]+", ");
			//System.out.println( "");

			System.out.println("client: localport :"+datagramSocket.getLocalPort());
			datagramSocket.send(datagramPacket);	// le send
			System.out.println("client: localport :"+datagramSocket.getLocalPort());

			if(datagramPacket.getLength() != UdpTest.MSG_SIZE)
				System.out.println(datagramPacket.getLength() + " != " +UdpTest.MSG_SIZE);

			UdpTest.total_sent ++;
		}
		
		datagramSocket.receive(datagramPacket);
		
		System.out.println("client: portsource paquet recu :"+ datagramPacket.getPort());

		UdpTest.end = System.currentTimeMillis();


	}
	catch(Exception e)
	{
		e.printStackTrace();
	}


}