| | |
MIDlet - using socket and databases
![]() |
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Solved Threads: 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:
The Client:
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!
So here is the code for the server:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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!
-1
#2 Nov 24th, 2009
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)
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)
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- socket exception (C#)
- socket minimum recieve (C#)
- Socket (C++)
- Socket programming + porting Unix to Win32 (C)
- Linking errors of socket functions (C)
- AMD socket 754 and 939 processors (Motherboards, CPUs and RAM)
- IE TCP/IP Socket Issue? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Hibernate does not return any result
- Next Thread: Writing to DB9 port
Views: 420 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Java
access actionlistener android api apple applet application arguments array arrays automation binary bluetooth c++ class classes client code combobox component coordinates data database design desktop detection draw eclipse error event exception fast file fractal game givemetehcodez graphics gridlayout gui helpwithhomework html ide image inheritance input integer interface j2me java javaprojects jmf jni jpanel jtextfield julia linux list loop method methods mobile netbeans newbie number object objects oracle os phone pixel print problem program programming project read recursion remote remove robot scanner screen search server set size sms socket sort sql string swing test text threads time transfer tree update windows






