Midlet - Midlet SocketConnection

Hi all thanks a lot for all the help you have rendered to beginers like us. Please I have a problem.

Please I want to know if its posible to make a sockect connection to a middlet installed on a phone using a different GSM provider than mine.

Please if its posible, could someone please assist me with a sample code. I have a litle know of socket connection working on the emulator, the main problem is how to get the server name or ip of the serversocket on another phone.

Please I really need help on this issues.

Thanks once again in advance.

[576 byte] By [brightokonaa] at [2007-10-3 5:12:21]
# 1

Hi.

I worked on SocketConnection, but I don't understand your request:

IP you required to know is your device public IP or just the server IP?

I've got some code sample, but you must be clearer!

Gvie me more informations about how your process works, then i'll try to help you!

ivomania

ivomaniaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Thanks for your response'

Actually what I want to know is how I can connect to a serverSocket Midlet on my brother's GSM phone from a midlet on my phone. Please note that we dont use the same provider.

I hope you understand me very now. please if its not clear enough I will expalain more.

Hope to get a response. thnx once again.

brightokonaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3
often this is not possible because the GSM opperators use NAT networks. You cannot connect to an IP inside the NAT network.Therefore you should always proxy your data though an external server with a public IP.
deepspacea at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Hi deepspace,

thanks a lot, but please could you expansiate on the issue of NAT networks.

Please could you give me a clue on how to use an external server with a public IP as you have said.

what I want to achieve is just been able to connect to the serversocket I have installed on my brother's GSM from the socket I have on my GSM phone.

Hope to get a positive response. any link for more details will be appreciated.

Thnx once again

brightokonaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5
For NAT, see: http://en.wikipedia.org/wiki/Network_address_translationDirectly connection to a serversocket on a mobile phone is therefore in a lot of cases not possible.
deepspacea at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 6

As deepspace mentioned most mobile networks are using NAT.

This is an illustration on how it looks:

Mobile phone<-> ROUTER (NAT) <->Internet

10.0.0.44:x<-> 10.0.0.1 / 219.12.56.24:y <-> internet

As you can see, the mobile phone can only see the router's private IP, not any of the IPs on the Internet. And the Internet IPs can also only see the router's IP, but they can see the public IP.

To access the mobile phones IP from the Internet you need to open a port (the port you're listening to in your ServerSocket) in the router, and forward this port to the private IP of your phone. In this case:

x and y are ports

219.12.56.24:y must be forwarded to 10.0.0.44:x

This is, however, not possible for a regular user. You need to contact your service provider to this, and even then it's very unlikely that they will do it for you.

If you want to mobile phones to communicate, you'll need a client - server - client setup up. Something like this:

GSM Network A | Internet| GSM Network B

--

your phone| your server | your brothers phone

This way, both phones register on the server, and the server acts as a relay for the mobiles by forwarding the messages.

Hope this didn't cause more confusion than it helped :-)

-Frank

Message was edited by:

frapaa

(added ports)

frapaaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 7

frapaa, I really thank you for the explanation, infact I now understand the practical issue about Midlet to Midlet comunication.

Please I hope u can also help me with these two questions;

1. How do I run the server on the internet as I dont have a live IP

2. What requirements must the two mobiles meet before they can connect to the server on the internet. Note that I can brows the internet sites using the phones.

Please I hope u have the solution to these. am really gratefull for your assistance. ... 2 dollars for you.

brightokonaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 8

I'm glad for any help I can give!

As for your new questions:

1. If your internet provider has given you a router, then the router probably has a public (or live, if you wish) IP. You can set up most routers to forward a port from the public IP to a port on a private IP (just like the example with the phones). This way you can run a server on a machine on your network.

If this isn't possible you may contact companies that offers webhosting and run your server program there, but that might be expensive :-)

2. If you can browse internet sites, everything should be ok. But just in case, you can test if this code runs on your phones. If it works you can connect to any server on the internet!

Code follows.

-Frank

import java.io.IOException;

import java.io.InputStream;

import javax.microedition.io.Connector;

import javax.microedition.io.HttpConnection;

import javax.microedition.lcdui.Command;

import javax.microedition.lcdui.CommandListener;

import javax.microedition.lcdui.Display;

import javax.microedition.lcdui.Displayable;

import javax.microedition.lcdui.Form;

import javax.microedition.midlet.MIDlet;

import javax.microedition.midlet.MIDletStateChangeException;

public class InternetMIDlet extends MIDlet implements CommandListener {

private Display d;

private Form f = new Form("InternetMIDlet");

public InternetMIDlet( ) {

d = Display.getDisplay(this);

d.setCurrent(f);

f.addCommand(new Command("Exit", Command.EXIT, 1));

f.setCommandListener(this);

}

protected void destroyApp( boolean arg0 ) throws MIDletStateChangeException {

}

protected void pauseApp( ) {

}

protected void startApp( ) throws MIDletStateChangeException {

try {

// open http connection

HttpConnection con =

(HttpConnection) Connector.open("http://checkip.dyndns.org");

// get input stream

InputStream in = con.openDataInputStream();

int b; // temp byte variable

StringBuffer sb = new StringBuffer();

// read a byte into 'b' and while b != -1 append b to the

// StringBuffer

while ((b = in.read()) != -1) {

sb.append((char) b);

}

// get only the nice text (Current IP Address: xxx.xxx.xxx.xxx)

String ip = sb.toString();

String startIndex = "<body>";

String endIndex = "</body>";

ip = ip.substring(

ip.indexOf(startIndex) + startIndex.length(),

ip.indexOf(endIndex));

// append IP to form

f.append(ip);

// close stream and connection

in.close();

con.close();

} catch (IOException e) {

e.printStackTrace();

}

}

public void commandAction( Command cmd, Displayable displayable ) {

// if command == exit -> shutdown midlet

if (cmd.getCommandType() == Command.EXIT) notifyDestroyed();

}

}

frapaaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 9

Thanks once again for the sample code. The code worked on the emulator and I believe it will also work on real device. But I dont know how to detect when the page is not available or an error has occured. when I try opening a page with scripting error, it displays the erros also.

I have awarded you some dolars.

hope to hear from you.

brightokonaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 10

Thank you for the dukes :-)

To see what errors occurs replace the content of the catch clause with this

//create an alert

Alert alert = new Alert("Error");

//set alert type to ERROR

alert.setType(AlertType.ERROR);

//make the user confirm before dismissing tha alert

alert.setTimeout(Alert.FOREVER);

//set the alert text

//alert.setString(e.getMessage()); //pretty text

alert.setString(e.toString()); //complete text

//display the alert and set 'f' as the next

//"window" after the alert is dismissed

f.append("An error has occured: " + e.getMessage());

d.setCurrent(alert, f);

I don't know what will happen with the scripting errors, but this will let you know if you were unable to connect to the site (and all other IOExceptions).

frapaaa at 2007-7-14 23:18:47 > top of Java-index,Java Mobility Forums,Java ME Technologies...