views:

40

answers:

1

i have a client thats sending a message to the ejbserver using UDP, i want the server(stateless bean) to echo back this message to the client but i cant seem to do this.

or can i implement the same logic by using JMS? please help and enlighten. this is just a test, in the end i want a midp to be sending the message to the ejb using datagrams.

here is my code.

@Stateless
public class SessionFacadeBean implements SessionFacadeRemote
{

 public SessionFacadeBean()
 {
     DatagramSocket aSocket = null;
     byte[] buffer = null;

     try
     {
        while(true)
        {
           DatagramPacket request = new DatagramPacket(buffer, buffer.length);
           aSocket.receive(request);

           DatagramPacket reply = new DatagramPacket(request.getData(),
           request.getLength(), request.getAddress(), request.getPort());
           aSocket.send(reply);
        }
    }
    catch (SocketException e)
    {
        ;
    }
    catch (IOException e)
    {
        ;
    }
 }

}

and the client:

public static void main(String[] args)
{
   DatagramSocket aSocket = null;
   try
   {

       aSocket = new DatagramSocket();
       byte [] m = "Test message!".getBytes();
       InetAddress aHost = InetAddress.getByName("localhost");

       int serverPort = 6789;
       DatagramPacket request = new DatagramPacket(m,  m.length, aHost, serverPort);
       aSocket.send(request);

       byte[] buffer = new byte[1000];
       DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
       aSocket.receive(reply);

       System.out.println("Reply: " + new String(reply.getData()));
   }
   catch (SocketException e)
   {
       System.out.println("Socket: " + e.getMessage());
   }
   catch (IOException e)
   {
       System.out.println("IO: " + e.getMessage());
   }
   finally
   {
       if(aSocket != null) aSocket.close();
   }

}

please help.

A: 

If you want to send UDP messages, you'll have to either run a standalone server or to implement a JCA connector. EJB or MDB are not the right technology for that.

And no offense but from what I can see from your code, I don't think that you're much experienced with Java EE server-side APIs and my recommendation would be to implement a standalone server.

Just in case, have a look at Thread: create UDP server with glassfish/ejb.

Pascal Thivent
i am new to JEE so please bear with me if you can.what do you mean by "run a standalone server"?in this articles: http://www.softwired.ch/pdf/technology/4931_11.pdf they were providing a solution using JMS and UDP. please enlighten.
Kemmal