Search code examples
javasocketsdatagram

Open an UDP channel in java


I want to open an UDP channel between the Client and the server with UDP. I have two questions. When i write

DatagramSocket serverSocket = new DatagramSocket(port);; 

is the channel opened or it will be open when I start sending ? and How can I specify the IP adress of the server ?


Solution

  • DatagramSocket serverSocket = new DatagramSocket(port);

    Constructs a datagram socket and binds it to the specified port on the local host machine.

    it does not create a channel between client and server.

    when the server starts to listen, client can send udp packet to this udp port number.

    for example if you want to bind a udp socket to a specific ip and port number use below method

    bindAddress="192.148.1.0";

    DatagramSocket socket=new (bindPort,InetAddress.getByName(bindAddress));