Question

So I try to create a simple server client application.

I have the server, which multicast messages to the subscribed clients.

The clients can obviously subscribe or unsubscribe.

Here is the server side code for the socket

  try // create DatagramSocket for sending and receiving packets
  {
     socket = new DatagramSocket( 6666 );
  } // end try
  catch ( SocketException socketException ) 
  {
     System.exit( 1 );
  } // end catch

I have also created a thread which waits for incoming pockets

        while(true)
        {
            try // receive packet, display contents, return copy to client
            {
               byte[] data = new byte[ 100 ]; // set up packet
               DatagramPacket receivePacket = 
                  new DatagramPacket( data, data.length );

               socket.receive( receivePacket ); // wait to receive packet

               String Message = receivePacket.getData().toString();
.........

Here is the client side

  try {
      socket = new DatagramSocket();
  } catch (SocketException ex) {
      displayArea.append( ex + "\n" );
  }

Here I try to send the packet

        byte[] data = message.getBytes(); // convert to bytes

        try {
            // create sendPacket
            DatagramPacket sendPacket = new DatagramPacket( data, 
               data.length, InetAddress.getLocalHost(), 6666 );
        } catch (UnknownHostException ex) {
            displayArea.append( ex + "\n" );
        }

Well, the problem is that I don't think the packet reaches the server, I know the thread is waiting for the packet, I can see in debug that it does reaches the socket.receive part, however, nothing happens.

I'm using the client and server on the same computer.

Any idea what am I doing wrong?

Thanks.

Was it helpful?

Solution

byte[] data = message.getBytes(); // convert to bytes
try {
    DatagramPacket sendPacket =
       new DatagramPacket( data, data.length, InetAddress.getLocalHost(), 6666 );

    socket.send( sendPacket ); //<<<<<<<<<<<<<<<<<<< REQUIRED

}
catch( UnknownHostException ex ) {
   displayArea.append( ex + "\n" );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top