943,168 Members | Top Members by Rank

Nov 24th, 2009
0

MIDlet - using socket and databases

Expand Post »
I'm starting to learn MIDlet and I need a little help with a project. I have a MIDlet which would be a client. This client has to connect to a database - it will send some information to it and also receive an answer back. I have to use Socket for the connection. My problem is that the Server won't see the Client connected even though I use the same port.
So here is the code for the server:
public class Server {
      
	public static void main(String[] args) {
		
		try{
                    while(true){
			ServerSocket ss=new ServerSocket(5000);
			System.out.println("Server started...");
			Socket s=ss.accept();

			s.getReceiveBufferSize();
			System.out.println("Client connected");
			BufferedReader in=new BufferedReader(new InputStreamReader(s.getInputStream()));
			String mesaj="";
			while(!mesaj.equals("end")){
				mesaj=in.readLine();
				System.out.println(mesaj);
			}
                }
		}catch(Exception e){e.printStackTrace();}
	}

}

The Client:
public class Client extends MIDlet implements CommandListener,Runnable{
    private boolean isPaused;
    private static Display display;
    private Form f;
    private StringItem si;
    private TextField tf1;
    private TextField tf2;
    private boolean stop;
    private Command sendCommand = new Command("Send", Command.ITEM, 0);
    private Command exitCommand = new Command("Exit", Command.EXIT, 0);
    InputStream is;
    OutputStream os;
    SocketConnection sc;

    private Client client;
    //Sender sender;

    public Client(){
        display = Display.getDisplay(this);
        f = new Form("Socket Client");
        si = new StringItem("Comanda:", " ");
        tf1 = new TextField("Nr zbor:", "", 30, TextField.ANY);
        tf2 = new TextField("Nr bilete:","",30,TextField.ANY);
        f.append(si);
        f.append(tf1);
        f.append(tf2);
        f.addCommand(exitCommand);
        f.addCommand(sendCommand);
        f.setCommandListener(this);
        display.setCurrent(f);
    }

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

    public void run() {
        try {
            sc = (SocketConnection)Connector.open("socket://localhost:5000");
            si.setText("Connected to server");
            is = sc.openInputStream();
            os = sc.openOutputStream();

I didn't put the rest of the code cause it's long and the connection is the only problem I have. I need the server to recognize the client. Don't tell me to use Http or what else there is cause it's school assignment and I have to use sockets.

Thanks in advance!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
laura_v is offline Offline
1 posts
since Dec 2007
Nov 24th, 2009
-1
Re: MIDlet - using socket and databases
Just do not tell my you are westminster university student or I will start rolling on the floor laughing.

Anyway back to topic. I bet my salary that as server your teacher/tutor meant some J2EE server or web container such as Tomcat, JBoss etc that is able to run servlet which is supposed to received data from your mobile application, process it and return something back to your mobile apps. For this reason, the application must be server based where your server application as provided above is actually stand-alone desktop application with zero outside world communication.
You correctly understood that midlet is supposed to output data as stream and server need to read the stream and process it. On top of that similar process will happen from server side, server will produce stream with reply, midlet will receive it handle it appropriately. You can see example of midlet sending data here. You may also find more detailed explanation in books like J2ME in a Nutshell and Learning Wireless Java Help for New J2ME Developers (which are little out of date, not many developers use socket on jme these days)
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,650 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Mobile Development Forum Timeline: J2me List
Next Thread in Mobile Development Forum Timeline: Java Bluetooth Chat - help on detection please





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC