client server programming in j2me
hi,
I am having one client program running in mobile and i created a server in the desktop.and i tried to send hi message from the client running in the mobile to the server.
For client i created a socket like this(sample coding given along with the sun java WTK toolkit)
SocketConnection sc = (SocketConnection) Connector.open("socket://59.144.3.15:5000");
59.144.3.15 is the ip address of the machine in which my server is running.
i wrote my server codings in java and the following is the code
import java.io.*;
import java.net.*;
class Server {
public static void main(String args[]) {
String data = "Data sent by server to client";
String line;
try {
ServerSocket srvs = new ServerSocket(5000);
Socket skt = srvs.accept();
System.out.print("Server has connected!\n");
InputStream sin = skt.getInputStream();
OutputStream sout = skt.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
System.out.println("REceievd");
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer)) > 0) {
String req = new String(buffer,0,len);
System.out.print(req);
}
}
catch(Exception e) {
System.out.print(e.toString());
}
}
}
In emulator this program is working fine and i am able to send message from the client to the server.
I installed my client program in the Nokia6630 mobile and tried to connect to the server,at that time client-server connection itself not establishing.
Can anyone tell me how to establish a simple client server connection?
Thanks a lot
[1753 byte] By [
parvathya] at [2007-10-3 3:55:42]

59.144.3.15 is this external ip?
yes it is external ip only.it is remote desktop machine ip addres
here giving a simple program just change server ip to yours
/*
* Tester.java
*
* Created on July 10, 2006, 3:33 AM
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Administrator
* @version
*/
public class Tester extends MIDlet {
Form fmMain;
Display ds;
public Tester() throws Exception
{
ds=Display.getDisplay(this);
fmMain=new Form("Geting Verified ");
this.caller();
}
public void startApp() {
ds.setCurrent(fmMain);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void caller()
{
Threader th=new Threader(this,fmMain);
}
}
class Threader extends Thread implements CommandListener {
String str="VER 1 MSNP8 CVR0";
String str2="CVR 2 0x0409 win 2000 i386 MSNMSGR 5.0.0544 MSMSGS cybrog_ed@hotmail.com";
String str3="USR 3 TWN I example@passport.com\r\n";
Command start;
SocketConnection sc=null;
ChoiceGroup cg;
InputStream in=null;
OutputStream out=null;
private boolean stop;
Form fmMain;
Tester test;
TextField t ;
Sender sender;
public Threader(Tester test,Form fmMain)
{
this.test=test;
start=new Command("Go",Command.SCREEN,1);
this.fmMain =fmMain;
fmMain.addCommand(start);
String [] choices ={str,str2,str3};
cg = new ChoiceGroup("select",Choice.POPUP,choices,null);
fmMain.setCommandListener(this);
fmMain.append(cg);
t = new TextField("enter data",str,100,TextField.ANY);
fmMain.append(t);
start();
}
public void commandAction(Command command, Displayable displayable) {
if(command==start)
{
sender.send(cg.getString(cg.getSelectedIndex()));
/* if(t.getString().trim().equals(str))
t.setString(str2);
else{
t.setString(str);
}
//sender.send(str2);
*/
}
}
public void run(){
try {
getVarified();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void getVarified() throws IOException{
try
{
try{
String url = "socket://" +"messenger.hotmail.com"+ ":" +1863;
sc = (SocketConnection) Connector.open(url);
in = sc.openInputStream();
out = sc.openOutputStream();
sender = new Sender(out);
}
catch(Exception e)
{
System.out.println(e.toString());
throw e;
}
//out.flush();
///////////////frist string sent to the server
//out.write((str2+ "\r\n").getBytes());
//out.flush();
StringBuffer sb = new StringBuffer();
int c = 0;
while(true){
c=in.read();
while (((c = in.read()) != '\n') && (c != -1)) {//response of the first string
sb.append((char) c);
}
// out.flush();
System.out.println(sb.toString());
fmMain.append("response-->"+sb.toString()+"\n");
//Thread.sleep(1000);
////////////second string to be sent to server is below
//Thread.sleep(1000);
//out.write((str +"\r\n").getBytes());
/*StringBuffer sb1 = new StringBuffer();
int c1 = 0;
while (((c1 = in.read()) != -1) ) {//response of the second string
sb1.append((char) c1);
}
System.out.println(sb1.toString());
*/
if (c == -1){
break;
}
}
stop();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
/**
* Close all open streams
*/
public void stop() {
try {
stop = true;
if (sender != null) {
sender.stop();
}
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (sc != null) {
sc.close();
}
} catch (IOException ioe) {}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* @(#)Sender.java1.6 04/04/25
*
* Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Sender extends Thread {
private OutputStream os;
private String message;
public Sender(OutputStream os) {
this.os = os;
start();
}
public synchronized void send(String msg) {
message = msg;
notify();
}
public synchronized void run() {
while (true) {
// If no client to deal, wait until one connects
if (message == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
if (message == null) {
break;
}
try {
os.write(message.getBytes());
os.write("\r\n".getBytes());
} catch (IOException ioe) {
ioe.printStackTrace();
}
// Completed client handling, return handler to pool and
// mark for wait
message = null;
}
}
public synchronized void stop() {
message = null;
notify();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ok thanks for ur reply
String str="VER 1 MSNP8 CVR0";
String str2="CVR 2 0x0409 win 2000 i386 MSNMSGR 5.0.0544 MSMSGS cybrog_ed@hotmail.com";
String str3="USR 3 TWN I example@passport.com\r\n";
what these strings represent?
is it neccessary to use these string after changing the socket creation line with my ip address
String url = "socket://" +"59.144.3.15"+ ":" +8080;
No they are for msn messengerjust modify your code and these strings are like data send by client
Please is it posible to use another Midlet as the server running on a real device, please give me a sample code how to connect to it.
I don't think soAll the devices I have seen only run one JVM instance at a time
you can run server on other deviceyou need to have external ip so that client can connect to the server from other devices
hello im an electronics student and im doing my final project. A part of my project consist in having a mobile thats sends a text message to a computer via internet. the coding you gave below is it good for my project pls ?
*
* Tester.java
*
* Created on July 10, 2006, 3:33 AM
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.SocketConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author Administrator
* @version
*/
public class Tester extends MIDlet {
Form fmMain;
Display ds;
public Tester() throws Exception
{
ds=Display.getDisplay(this);
fmMain=new Form("Geting Verified ");
this.caller();
}
public void startApp() {
ds.setCurrent(fmMain);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void caller()
{
Threader th=new Threader(this,fmMain);
}
}
class Threader extends Thread implements CommandListener {
String str="VER 1 MSNP8 CVR0";
String str2="CVR 2 0x0409 win 2000 i386 MSNMSGR 5.0.0544 MSMSGS cybrog_ed@hotmail.com";
String str3="USR 3 TWN I example@passport.com\r\n";
Command start;
SocketConnection sc=null;
ChoiceGroup cg;
InputStream in=null;
OutputStream out=null;
private boolean stop;
Form fmMain;
Tester test;
TextField t ;
Sender sender;
public Threader(Tester test,Form fmMain)
{
this.test=test;
start=new Command("Go",Command.SCREEN,1);
this.fmMain =fmMain;
fmMain.addCommand(start);
String [] choices ={str,str2,str3};
cg = new ChoiceGroup("select",Choice.POPUP,choices,null);
fmMain.setCommandListener(this);
fmMain.append(cg);
t = new TextField("enter data",str,100,TextField.ANY);
fmMain.append(t);
start();
}
public void commandAction(Command command, Displayable displayable) {
if(command==start)
{
sender.send(cg.getString(cg.getSelectedIndex()));
/* if(t.getString().trim().equals(str))
t.setString(str2);
else{
t.setString(str);
}
//sender.send(str2);
*/
}
}
public void run(){
try {
getVarified();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void getVarified() throws IOException{
try
{
try{
String url = "socket://" +"messenger.hotmail.com"+ ":" +1863;
sc = (SocketConnection) Connector.open(url);
in = sc.openInputStream();
out = sc.openOutputStream();
sender = new Sender(out);
}
catch(Exception e)
{
System.out.println(e.toString());
throw e;
}
//out.flush();
///////////////frist string sent to the server
//out.write((str2+ "\r\n").getBytes());
//out.flush();
StringBuffer sb = new StringBuffer();
int c = 0;
while(true){
c=in.read();
while (((c = in.read()) != '\n') && (c != -1)) {//response of the first string
sb.append((char) c);
}
// out.flush();
System.out.println(sb.toString());
fmMain.append("response-->"+sb.toString()+"\n");
//Thread.sleep(1000);
////////////second string to be sent to server is below
//Thread.sleep(1000);
//out.write((str +"\r\n").getBytes());
/*StringBuffer sb1 = new StringBuffer();
int c1 = 0;
while (((c1 = in.read()) != -1) ) {//response of the second string
sb1.append((char) c1);
}
System.out.println(sb1.toString());
*/
if (c == -1){
break;
}
}
stop();
}
catch(Exception e)
{
System.out.println(e.toString());
}
}
/**
* Close all open streams
*/
public void stop() {
try {
stop = true;
if (sender != null) {
sender.stop();
}
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (sc != null) {
sc.close();
}
} catch (IOException ioe) {}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/*
* @(#)Sender.java 1.6 04/04/25
*
* Copyright (c) 2000-2004 Sun Microsystems, Inc. All rights reserved.
* PROPRIETARY/CONFIDENTIAL
* Use is subject to license terms
*/
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Sender extends Thread {
private OutputStream os;
private String message;
public Sender(OutputStream os) {
this.os = os;
start();
}
public synchronized void send(String msg) {
message = msg;
notify();
}
public synchronized void run() {
while (true) {
// If no client to deal, wait until one connects
if (message == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
if (message == null) {
break;
}
try {
os.write(message.getBytes());
os.write("\r\n".getBytes());
} catch (IOException ioe) {
ioe.printStackTrace();
}
// Completed client handling, return handler to pool and
// mark for wait
message = null;
}
}
public synchronized void stop() {
message = null;
notify();
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
No this is not for text messages this class is for socket communicationssearch this forum on sending text messages
sory it was a wrong post! desole guysMessage was edited by: Rawn
Rawna at 2007-7-14 21:53:49 >

does any1 know whether msn still support httpsConnection to login2.srf, i always get back a html page in the content header.
have tried the new SOAP tweener...works fine in normal java when using sslsocket but j2me doesnt have sslsockets, does any1 know of any WORKING alternative? please help
im beginning to think that without a server running on a normal jvw which does the authentication, theres no other way of connecting to messenger. but if i do this wont it be creating a weakpoint in security?