[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UDP socket creation



Here is the code and output from using 0.

import java.io.*;
import java.net.*;

public class UDP {

  public static void main(String args[]) {
    DatagramSocket ds;

    try {
      ds = new DatagramSocket();
      ds.bind(new InetSocketAddress(0));
      System.out.println("Got Port: " + ds.getPort());
    } catch (Exception e) {
      System.err.println("Error: " + e.getMessage());
    }

    System.exit(0);
  }
}

[cfowler@cfowler - CHROOT outpost-apps]$ java UDP
Error: already bound



On Thu, 2002-12-05 at 15:30, Curt Smith wrote:
> Have your tried specifying port 0 (zero)?
> 
> This may be an IP convention to assign a system generated ephemeral port
> that I've seen.
> 
> curt
> 
> cfowler wrote:
> 
> >private void openSocket() {
> >    try {
> >      ds = new DatagramSocket();
> >      ds.bind(new InetSocketAddress(6090));
> >    } catch (Exception e) { System.err.println(e.getMessage());}
> >  }
> >
> >For some reson ds.getPort() returns -1.  I do not want to use 6090.  I
> >want to do a ds.receive() on a UDP socket that the OS has available.  If
> >I specify the prot 6090 and it is not available, I will get an
> >Exception.  But I need ds.getPort() so I can tell the remote what port
> >the OS has given me to listen on.  Is there a simple easy way I'm not
> >seeing to do this?
> >
> >Thanks,
> >Chris
> >
> >  
> >
> 
> -- 
> 
> Curt Smith
> chsmith@speakeasy.net
> (w) 404-463-0973
> (h) 404-294-6686
> 
> 
> 
> 
> 
> 
>