DatagramSocket connect - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for DatagramSocket connect method.

This code example shows how to use the following methods:connect, setSoTimeout
public class RtpSocket { 
  private final byte [] buf = new byte[4096];
  private DatagramSocket socket;
 
  public RtpSocket(int localPort, InetSocketAddress remoteAddress) throws SocketException {
    socket = new DatagramSocket(localPort);
    socket.setSoTimeout(1);
    socket.connect(new InetSocketAddress(remoteAddress.getAddress().getHostAddress(), remoteAddress.getPort()));
    Log.d( "RtpSocket", "Connected to: " + remoteAddress.getAddress().getHostAddress() );
  } 
 
  public void setTimeout(int timeoutMillis) {
    try { 
      socket.setSoTimeout(timeoutMillis);
    } catch (SocketException e) {
      Log.w("RtpSocket", e);
    } 
  } 
 
  private long totalSendTime = 0;
8
CodeRank
This code example shows how to use the following methods:close, connect, receive, send, setSoTimeout
        } catch (IllegalArgumentException e) {
            // jndi.40=Unable to connect: bad IP address 
            throw new DomainProtocolException(Messages.getString("jndi.40")); //$NON-NLS-1$ 
        } 
        try { 
            dSocket = new DatagramSocket();
            srvAddr = InetAddress.getByAddress(srvAddrArr);
            dSocket.connect(srvAddr, serverPort);
            outPacket = new DatagramPacket(outBuf, outBufLen, srvAddr,
                    serverPort);
            dSocket.setSoTimeout(timeout);
            dSocket.send(outPacket);
            inPacket = new DatagramPacket(inBuf, inBufLen, srvAddr, serverPort);
            dSocket.receive(inPacket);
        } catch (IllegalStateException e) {
            // jndi.41=Error while querying DNS server 
            throw new DomainProtocolException(Messages.getString("jndi.41"), e); //$NON-NLS-1$
        } catch (SocketTimeoutException e) {
            throw (e);
        } catch (IOException e) {

6
CodeRank
This code example shows how to use the following methods:connect, isBound, isConnected
/** 
 * A simple test designed to conduct a DTLS handshake with an external DTLS server. 
 * <p/> 
 * Please refer to GnuTLSSetup.txt or OpenSSLSetup.txt, and x509-*.pem files in this package for 
 * help configuring an external DTLS server. 
 */ 
public class DTLSClientTest 
{ 
    private static final SecureRandom secureRandom = new SecureRandom();
 
    public static void main(String[] args)
        throws Exception 
    { 
        InetAddress address = InetAddress.getLocalHost();
        int port = 5556;
 
        TlsSession session = createSession(address, port);
 
        MockDTLSClient client = new MockDTLSClient(session);
 

7
CodeRank
This code example shows how to use the following methods:close, connect, getLocalPort, receive, setSoTimeout
    return result;
  } 
 
  private int attemptConnection( int timeout ) {
    try { 
      socket = new DatagramSocket();
      socket.connect(new InetSocketAddress(server, port));
      socket.setSoTimeout(timeout);
      sendSignal(new OpenPortSignal(sessionId));
 
      SignalResponse response = readSignalResponse();
 
      if (response.getStatusCode() != 200) {
        Log.e("NetworkConnector", "Bad response from server.");
        socket.close();
        return -1; 
      } 
 
      int localPort = socket.getLocalPort();
      socket.close();

7
CodeRank
This code example shows how to use the following methods:close, connect, disconnect, setSoTimeout
			free(); 
		} 
 
		try 
		{ 
			socket = new DatagramSocket();
			socket.connect(new InetSocketAddress(ip, port));
			socket.setSoTimeout(timeOut);
			return true; 
		} 
		catch (Exception e)
		{ 
			errmsg = e.getMessage();
			return false; 
		} 
		finally 
		{ 
			free(); 
		} 
	} 

6
CodeRank
This code example shows how to use the following methods:connect, setSoTimeout
     * @param addr 
     * @return connected Transport 
     * @throws IOException  
     */ 
    public static Transport UDPInstance(SocketAddress addr) throws IOException {
        DatagramSocket sock = new DatagramSocket();
        sock.setSoTimeout(getTimeout());
        sock.connect(addr);
 
        StreamTransport trans = new StreamTransport();
        trans.setStreams(new UDPInputStream(sock), new UDPOutputStream(sock));
        trans.setAddressLabel(addr.toString());
        return trans;
    } 
} 
6
CodeRank
This code example shows how to use the following methods:close, connect, getLocalAddress
    } 
    public static void showWelcomeInfo(int tcpPort1,int tcpPort2){
        boolean usePublicDNS = false;
        try { 
            DatagramSocket udpSocket = null;
            udpSocket = new DatagramSocket();
            udpSocket.connect(InetAddress.getByName("8.8.8.8"), 53); // Using public DNS google
            IP = udpSocket.getLocalAddress();
            udpSocket.close();
            usePublicDNS = true;
        } catch (SocketException e1) {
            System.out.println("cannot get IP address through 8.8.8.8");
        } 
        catch (UnknownHostException e1) {
            System.out.println("cannot get IP address through 8.8.8.8");
        } 
        if(!usePublicDNS){ // no internet connection or goolge 8.8.8.8 down
            try { 
                IP = InetAddress.getLocalHost();
            } catch (UnknownHostException e) {
6
CodeRank
This code example shows how to use the following methods:connect, isConnected
	 
	public boolean isConnected(String addr){
		InetAddress serverAddress;
		try { 
			serverAddress = InetAddress.getByName(addr);
			socket.connect(serverAddress, port);
			return socket.isConnected();
		} catch (UnknownHostException e) {
			return false; 
		} 
 
	} 
	 
	public int send(byte[] data, String addr){
    	try { 
			InetAddress serverAddress = InetAddress.getByName(addr);
			DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,port);			
			socket.send(packet);
		} catch (SocketException e) {
			// TODO Auto-generated catch block 
5
CodeRank
This code example shows how to use the following methods:connect
		private byte[] data = new byte[50];
	    private String ipAddress = "192.168.1.12";
 
	public ConnectionManager() { 
		try { 
			socket = new DatagramSocket(8889);
			socket.connect(InetAddress.getByName(ipAddress), 9999);
 
		} catch (SocketException e) {
			Log.e(TAG, e.getMessage());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} 
	} 
	 
	public void send(){ 
		DatagramPacket pk = new DatagramPacket(data, data.length);
		Log.d(TAG, "Packet sending");
		try { 
			socket.send(pk);
5
CodeRank
This code example shows how to use the following methods:connect
  public UdpInputOutput(Env env, String host, int port, Domain domain)
    throws IOException 
  { 
    super(env);
     
    _socket = new DatagramSocket();
    _socket.connect(InetAddress.getByName(host), port);
     
    _domain = domain;
  } 
   
  public UdpInputOutput(Env env, DatagramSocket socket, Domain domain)
  { 
    super(env);
 
    _socket = socket;
    _domain = domain;
  } 
 
  public void bind(SocketAddress address)
4
CodeRank
connect Method Overview
Connects this datagram socket to the address and port specified by peer.
See Code Examples for other Java 8 DatagramSocket Methods: