Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-25-2010, 06:37 AM
Member
 
Join Date: Jan 2010
Posts: 2
Rep Power: 0
arun10427 is on a distinguished road
Default Socket connection - MIDlet
Hi I am new to using MIDlets, and I wanted to create a simple TCP client server program.. I found a tutorial in J2me forums and I am able to send a single message from server(PC) to client(Phonemulator) and from client to server. But I want to send a stream of messages to the server. Here is my program and I am stuck in the last step wher i want to send lot of messages to server. Here is my program, Could any one of u tell me how to do it? Or where am i going wrong in thsi pgm?

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
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.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeExcepti on;

public class SocketMIDlet extends MIDlet
implements CommandListener, Runnable {
private Display display;
private Form addressForm;
private Form connectForm;
private Form displayForm;
private TextField serverName;
private TextField serverPort;
private StringItem messageLabel;
private StringItem errorLabel;
private Command okCommand;
private Command exitCommand;
private Command backCommand;

protected void startApp() throws MIDletStateChangeException {
if (display == null) {
initialize();
display.setCurrent(addressForm);
}
}

protected void pauseApp() {
}

protected void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
}

public void commandAction(Command cmd, Displayable d) {
if (cmd == okCommand) {
Thread t = new Thread(this);
t.start();
display.setCurrent(connectForm);
} else if (cmd == backCommand) {
display.setCurrent(addressForm);
} else if (cmd == exitCommand) {
try {
destroyApp(true);
} catch (MIDletStateChangeException ex) {
}
notifyDestroyed();
}
}

public void run() {
InputStream is = null;
OutputStream os = null;
StreamConnection socket = null;
try {
String server = serverName.getString();
String port = serverPort.getString();
String name = "socket://" + server + ":" + port;
socket = (StreamConnection)Connector.open(name, Connector.READ_WRITE);
} catch (Exception ex) {
Alert alert = new Alert("Invalid Address",
"The supplied address is invalid\n" +
"Please correct it and try again.", null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
return;
}
try {
// Send a message to the server
String request = "Hello\n\n";
//StringBuffer b = new StringBuffer();
os = socket.openOutputStream();
//for (int i=0;i<10;i++)
//{
os.write(request.getBytes());
//}
os.close();
// Read the server's reply, up to a maximum
// of 128 bytes.
is = socket.openInputStream();
final int MAX_LENGTH = 128;
byte[] buf = new byte[MAX_LENGTH];
int total = 0;

while (total<=5)
{
int count = is.read(buf, total, MAX_LENGTH - total);
if (count < 0)
{
break;
}
total += count;
}
is.close();
String reply = new String(buf, 0, total);
messageLabel.setText(reply);
socket.close();
display.setCurrent(displayForm);
} catch (IOException ex) {
Alert alert = new Alert("I/O Error",
"An error occurred while communicating with the server.",
null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert, addressForm);
return;
} finally {
// Close open streams and the socket
try {
if (is != null) {
is.close();
is = null;
}
} catch (IOException ex1) {
}
try {
if (os != null) {
os.close();
os = null;
}
} catch (IOException ex1) {
}
try {
if (socket != null) {
socket.close();
socket = null;
}
} catch (IOException ex1) {
}
}
}

private void initialize() {
display = Display.getDisplay(this);

// Commands
exitCommand = new Command("Exit", Command.EXIT, 0);
okCommand = new Command("OK", Command.OK, 0);
backCommand = new Command("Back", Command.BACK, 0);

// The address form
addressForm = new Form("Socket Client");
serverName = new TextField("Server name:", "", 256, TextField.ANY);
serverPort = new TextField("Server port:", "", 8, TextField.NUMERIC);
addressForm.append(serverName);
addressForm.append(serverPort);
addressForm.addCommand(okCommand);
addressForm.addCommand(exitCommand);
addressForm.setCommandListener(this);

// The connect form
connectForm = new Form("Connecting");
messageLabel = new StringItem(null, "Connecting...\nPlease wait.");
connectForm.append(messageLabel);
connectForm.addCommand(backCommand);
connectForm.setCommandListener(this);

// The display form
displayForm = new Form("Server Reply");
messageLabel = new StringItem(null, null);
displayForm.append(messageLabel);
displayForm.addCommand(backCommand);
displayForm.setCommandListener(this);
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 01-25-2010, 11:08 AM
Darryl.Burke's Avatar
Senior Member
 
Join Date: Sep 2008
Location: Madgaon, Goa, India
Posts: 726
Rep Power: 2
Darryl.Burke is on a distinguished road
Default
Sun Java Wireless Toolkit - Help with MIDlets - TCP client server program
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-26-2010, 03:51 AM
Member
 
Join Date: Jan 2010
Posts: 2
Rep Power: 0
arun10427 is on a distinguished road
Default
Hello all,
I was wondering if someone found a solution to this..I would really appreciate it if u could post one...Thanks a lot..Cheerz
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Tags
j2me, midp, sockets

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bluetooth connection not working on US Nokia phones from J2ME Midlet (MIDP 2.0) yash2009 CLDC and MIDP 1 08-07-2009 10:30 AM
Error in establishing socket connection saso1310 Database 2 05-18-2009 02:00 PM
[SOLVED] problem in socket connection neeraj.singh Networking 2 02-20-2009 02:33 PM
problem in socket connection in sending images vibhor Networking 2 02-20-2009 06:39 AM
How To Make Socket Connection madhumurundi Networking 5 04-21-2008 07:05 PM


All times are GMT +2. The time now is 07:39 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org