0

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!

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by peter_budo
0

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)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.