Вопрос

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 ?

Это было полезно?

Решение 2

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));

Другие советы

I find information about it

Here is how you open a DatagramChannel:

   DatagramChannel channel = DatagramChannel.open();
   channel.socket().bind(new InetSocketAddress(9999));

more infor here http://tutorials.jenkov.com/java-nio/datagram-channel.html

Take a look at the following tutorial example for writing UDP client/servers in java. The IP address only needs to be specified by the client in order to connect to the server. However the port is needed by both parts.

http://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top