File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Socket in J2ME

Andrew Byrne

Joined: Jan 20, 2004
Posts: 3
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 ""
Does any one know how to do this?
Here is a sample of my code...
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
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);

public void pauseApp(){

public void start() {
Thread t = new Thread(this);

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

public void run() {
try {
sc = (SocketConnection)"socket://");

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 = != '\n') && (c != -1)) {
sb.append((char) c);

} catch (ConnectionNotFoundException cnfe) {
Alert a = new Alert("Client", "Please run Server MIDlet first",
null, AlertType.ERROR);
} catch (IOException ioe) {
if (!stop) {
} catch (Exception e) {
public void commandAction(Command c, Displayable d) {
if (c == exitCommand) {
* Close all open streams
try {
stop = true;
if (is != null) {
if (os != null) {
if (sc != null) {
catch (IOException ioe) {


else if (c == startCommand) {
String t = password.getString();
String getPassword = "1";
StringBuffer PASSWORD = new StringBuffer();
int i = 0;
while (((i = != '\n') && (i != -1)) {
PASSWORD.append((char) i);
if (t.equals(PASSWORD)) {
startMenu = new MainMenu_MIDlet(parent, sender, is, os, sc);
else {
Alert error = new Alert ("Incorrect Password");
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 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

serge - - my android apps
Andrew Byrne

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
J2ME Network Programming

Co-author of SCMAD Exam Guide, Author of JMADPlus
Andrew Byrne

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:
subject: Socket in J2ME