Domanda

Im trying to write server and client for service which would tell the current time. My code:

SERVER

public class TimeServer {

   public static void main(String[] args) {
      try {
         final DatagramSocket so = new DatagramSocket(8189);
         new Thread(new Runnable() {
            @Override
            public void run() {
               try {
                  while (true) {
                     try {
                        InetAddress group = InetAddress.getByName("200.20.2.0");
                        byte[] buf = new byte[256];
                        Calendar cal = Calendar.getInstance();
                        buf = cal.toString().getBytes();
                        DatagramPacket packet = new DatagramPacket(buf, buf.length,
                                group, 8189);
                        so.send(packet);
                     } catch (IOException ex) {
                        ex.printStackTrace();
                     }
                     Thread.sleep((int)(Math.random() * 5));
                  }
               } catch (InterruptedException ex) {
                  ex.printStackTrace();
               }
            }
         }).start();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

CLIENT

public class TimeClient {

   public static void main(String[] args) {
      try {
         MulticastSocket so = new MulticastSocket(8190);
         InetAddress group = InetAddress.getByName("200.20.2.0");
         so.joinGroup(group);

         //5 razy czekamy na otrzymanie od serwera wiadomosci
         for (int a=0; a<5; a++) {
            byte[] buf = new byte[256];
            DatagramPacket packet = new DatagramPacket(buf, buf.length);
            so.receive(packet);

            String recv = new String(packet.getData());
            System.out.println("RECEIVED: " + recv);
         }

         so.leaveGroup(group);
         so.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      } 
   }

}

When launching server and then client I get the following error:

java.net.BindException: Address already in use
    at java.net.PlainDatagramSocketImpl.bind0(Native Method)
    at java.net.AbstractPlainDatagramSocketImpl.bind(AbstractPlainDatagramSocketImpl.java:95)
    at java.net.DatagramSocket.bind(DatagramSocket.java:376)
    at java.net.MulticastSocket.<init>(MulticastSocket.java:172)
    at java.net.MulticastSocket.<init>(MulticastSocket.java:137)
    at timeclient.TimeClient.main(TimeClient.java:13)

Please help, thanks

EDIT: I changed client port and now I get exception:

java.net.SocketException: Not a multicast address
    at java.net.MulticastSocket.joinGroup(MulticastSocket.java:306)
    at timeclient.TimeClient.main(TimeClient.java:15)

What's now?

È stato utile?

Soluzione

Every Port can only be used by a single application. In your case, your server and your client (2 applications) are trying to use the same port which is causing the exception.

To fix it, change for example the port for the client from 8189 to 8190

Altri suggerimenti

You must change the client port (or the server port) because every port only can be used by a single process.

try DatagramSocket.setReuseAddress(true) and see what happens.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top