*
The moose likes Java Micro Edition and the fly likes Socket in J2ME Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Socket in J2ME" Watch "Socket in J2ME" New topic
Author

Socket in J2ME

Andrew Byrne
Greenhorn

Joined: Jan 20, 2004
Posts: 3
Hi,
I have a J2ME Client connecting to a C server using sockets.
I'm able make the socket connection and send messages from the J2ME client to the server but I don't know how to send messages back.
I want to be able to read messages coming in on the InputStream when I send them from the server but I don't seem to be able to access my InputStream,SocketConnection etc from within "public void commandAction" and therefore can't call "is.read()"
Does any one know how to do this?
Here is a sample of my code...
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Login_MIDlet implements Runnable, CommandListener {
private static Display display;
private TextBox password;
private Project_MIDlet parent;
private boolean stop;

private MainMenu_MIDlet startMenu;

private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Command startCommand = new Command("Login", Command.ITEM, 1);

InputStream is;
OutputStream os;
SocketConnection sc;
Sender sender;

public Login_MIDlet(Project_MIDlet m) {

parent = m;
display = Display.getDisplay(parent);
password = new TextBox("Enter System Password", "", 20, TextField.PASSWORD);

password.addCommand(exitCommand);
password.addCommand(startCommand);
password.setCommandListener(this);
}
public void pauseApp(){
}

public void start() {
display.setCurrent(password);
Thread t = new Thread(this);
t.start();
}

public void destroyApp(boolean unconditional) {
password = null;
exitCommand = null;
startCommand = null;
display = null;
}

public void run() {
try {
sc = (SocketConnection) Connector.open("socket://192.168.8.120:8080");

is = sc.openInputStream();
os = sc.openOutputStream();
// Start the thread for sending messages - see Sender's main
// comment for explanation
sender = new Sender(os);

// Loop forever, receiving data
while (true) {
StringBuffer sb = new StringBuffer();
int c = 0;
while (((c = is.read()) != '\n') && (c != -1)) {
sb.append((char) c);
}

}
} catch (ConnectionNotFoundException cnfe) {
Alert a = new Alert("Client", "Please run Server MIDlet first",
null, AlertType.ERROR);
a.setTimeout(Alert.FOREVER);
a.setCommandListener(this);
display.setCurrent(a);
} catch (IOException ioe) {
if (!stop) {
ioe.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
destroyApp(true);
parent.notifyDestroyed();
/**
* Close all open streams
*/
try {
stop = true;
sender.stop();
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (sc != null) {
sc.close();
}
}
catch (IOException ioe) {
}

}

else if (c == startCommand) {
String t = password.getString();
String getPassword = "1";
sender.send(getPassword);
StringBuffer PASSWORD = new StringBuffer();
int i = 0;
while (((i = is.read()) != '\n') && (i != -1)) {
PASSWORD.append((char) i);
}
if (t.equals(PASSWORD)) {
startMenu = new MainMenu_MIDlet(parent, sender, is, os, sc);
startMenu.startApp();
}
else {
Alert error = new Alert ("Incorrect Password");
error.setTimeout(Alert.FOREVER);
display.setCurrent(error, password);
}
}
}
}
serge masse
Ranch Hand

Joined: Jul 23, 2003
Posts: 102
I did not read all the code yet but based on your question, I can ask: Did you try creating a new Thread in the commandAction() method and in the new thread then you use your InputStream (is) and do is.read()? If you did not try this, then I recommend that you try, it should work :-)
for an example of an API that does this, you can look at http://leafy.dev.java.net/


serge - http://goo.gl/GgF7R - my android apps
Andrew Byrne
Greenhorn

Joined: Jan 20, 2004
Posts: 3
Thanks for the reply,
I did not create a new Thread in the commandAction() method to use the InputStream. How would I do that? I'm new to J2ME and I don't fully understand how to use sockets in MIDlets.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Originally posted by Andrew Byrne:
I'm new to J2ME and I don't fully understand how to use sockets in MIDlets.

If u've ever used socket programming in J2SE, they are a lot similar to J2ME's..... If u r still new to the sockets, then I would suggest u to learn socket programmin in J2SE first and go to J2ME later...
U might want to have a look at the following article on InformIT.com
J2ME Network Programming


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Andrew Byrne
Greenhorn

Joined: Jan 20, 2004
Posts: 3
Hi thanks for the link,
It's not that I don't know how to use sockets. Sockets aren't all that complicated. What I don't know how to do is send/recieve messages over the socket connection from within the commandAction() method. Well, I'm using this other class Sender(from example in WTK sample folder) to send messages and I can see the server recieving them. Is there perhaps a similar class for recieving messages from the server?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Socket in J2ME
 
Similar Threads
J2ME Text Editor
read line by line from file
MY server just hanged
how to send email from j2me using Servlets
How to search multiple records in RecordEnumaration