문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top