Hi folks,
I've been browsing this and also other discussion boards for days in order to find a solution or helpful answer for my following problem. My searches were without success and I hope that some Nokia guy (or any other expert) will read this thread and take care of this.
I'm developing a J2ME application for my Nokia N70 smartphone (firmware version V 2.0536.0.2 12-09-05 RM-84) an run into trouble opening and using a plain socket connection. The midlet runs very well both on Sun's WTK Wireless Toolkit Version 2.2 and Nokia's Prototype SDK 2.2 for J2ME emulator.
Here's what I'm doing step by step:
I'm opening a socket connection to a local web server which runs on a well defined tcp port (say 8888) - other than port 80 in order to bypass midlet signment:
SocketConnection conn = (SocketConnection)Connector.open("socket://server.my.domain:8888"😉;
After setting some socket options I retrieve the input and output streams associated with that socket:
DataInputStream input = conn.openDataInputStream();
DataOutputStream output = conn.openDataOutputStream();
Then I send a simple http request to my server as follows:
String webRequest = "GET / HTTP/1.1\n\n";
output.write(webRequest.getBytes());
Having sent the request to the server I simply read from the input stream in a loop:
byte[] inbuf = new byte[512];
do {
int nCount = input.read(inbuf);
result.append(new String(inbuf, 0, nCount));
}
while (input.available() > 0);
As I said before this application runs well on both Sun's and Nokia's emulators - I'm getting the expected answer from the server. As soon as I run the midlet on the Nokia N70 the application seams to hang right at the statement "input.read()". Using the network protocol analyzer ethereal I can clearly see my request arriving at the web server and the expected result beeing sent to my phone. But the phone does not return from the read() method.
My provider's GPRS setting are fine since the very same midlet runs properly on my old Siemens S65 cell phone using the very same SIM card. Can anyone (Nokia experts, do you hear me?) explain this weird behaviour? Does anyone out there use socket connections on a Nokia N70 phone?
Here is the full midlet sample code which opens a TextBox in order to view the result read from the server. Any help is appreciated.
public class SocketMidlet extends MIDlet
{
private static final String socketURL = "socket://server.my.domain:8888";
private static final String webRequest = "GET / HTTP/1.1\n\n";
private TextBox viewer = null; public SocketMidlet() {}
public void pauseApp() {}
public void startApp()
{
viewer = new TextBox("SocketMidlet Test", null, 1024, TextField.ANY );
viewer.setString("SocketMidlet:"😉;
Display.getDisplay(this).setCurrent(viewer);
connect(socketURL);
}
public void destroyApp(boolean cond)
{
notifyDestroyed();
}
void connect(String url)
{
try
{
SocketConnection conn = (SocketConnection)Connector.open(url);
conn.setSocketOption(SocketConnection.LINGER, 5);
DataInputStream input = conn.openDataInputStream();
DataOutputStream output = conn.openDataOutputStream();
output.write(webRequest.getBytes());
StringBuffer result = new StringBuffer("Content:\n"😉;
byte[] inbuf = new byte[512];
do
{
int nCount = input.read(inbuf);
result.append(new String(inbuf, 0, nCount));
}
while (input.available() > 0);
viewer.setString(viewer.getString()+result.toString());
output.close();
input.close();
conn.close();
}
catch (IOException e)
{
e.printStackTrace();
}
Display.getDisplay(this).setCurrent(viewer);
}
}