Reputation: 33
I'm coding a simple application for mobile phones (using j2me) with client-server interaction using the datagrams protocol. The mobile application sends some text to a server and the server prints that text to the standard output. Here is the code for sending datagrams (with the mobile phone):
String ip;
String port;
String msg;
//Loading ip, port and msg from the user input, I won't write it here, 'cause it
//does not matter
//Below is a simple code snippet for sending msg to the ip:port address
connection = (DatagramConnection) Connector.open(
"datagram://" + ip + ":" + port);
Datagram datagram = connection.newDatagram(msg.getBytes(),
msg.getBytes().length);
connection.send(datagram);
Here is the code for receiving the datagrams (with the PC):
DatagramSocket s = new DatagramSocket(7777);
DatagramPacket p = new DatagramPacket(new byte[1024], 1024);
s.receive(p);
System.out.println(new String(p.getData()));
So, when I launch the first code snippet (for sending datagrams) using an emulator, everything works fine: the server successfully receives and prints datagrams. But when the program for sending datagrams is launched from the mobile phone, the datagrams don't reach the server.
When testing the program via the emulator, the IP address was my local network IP, and when testing it via the real mobile phone, the IP was taken from the http://www.whatismyip.com/. When using the former IP for the emulator tests, the datagrams also don't reach the server. The port was always set to 7777.
So, can I solve that problem?
Upvotes: 2
Views: 786
Reputation: 4043
Your server must have real a public IP for it to be reached. If it is behind a proxy/firewall it won't be possible for the Java ME app to reach it.
If possible try to host your server on some virtualisation service (Amazon, Google, etc.).
Upvotes: 1