| Reply | « Previous Thread | Next Thread » |
|
Hi,
I try to develop a simple MIDlet for my Nokia 7650 that sets up a standard socket connection and sends a simple String message to the server. OutputConnection con = (OutputConnection) Connector.open("socket://addr:4711"); DataOutputStream out = con.openDataOutputStream(); out.writeUTF("Midlet says: Hi!"); out.flush(); out.close(); con.close(); In my emulator everything works fine. Running the same MIDlet on my 7650 the writeUTF blocks. After closing the application the message is sent to the server. What could be the reason for this strange behaviour? Best regards, Wolfgang |
|
Sockets are officially not supported on 7650.
[N]/Forum Nokia |
|
Thanks for your reaction!
Therefore this means than none of various the connection types provided by Connector will work (will be supported officially). Is that correct? Even it is not supported officially is there a possibility to get it work anyway? Best regards, Wolfgang |
|
Its working for me. How are you trying to read the message on other end?
|
| v_v_nataraj |
| View Public Profile |
| Find all posts by v_v_nataraj |
|
Hi,
I just open an DataInputStream and perform a standard readUTF operation. Socket connection = serverSocket.accept(); DataInputStream in = new DataInputStream(connection.getInputStream()); String str = in.readUTF(); I assume this should work. Best regards, Wolfgang |
|
Hi,
Given below is what i did. just check it out Regards Nataraj midlet: import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.io.*; import javax.microedition.io.*; public class Tcp01c extends MIDlet implements Runnable { private Thread t; private Display display; private Form form; private Command exitCommand, getCommand; private String connectionDetails = "socket://10.10.10.10:1801"; private StreamConnection sc; private DataInputStream dis; private DataOutputStream dos; private TextField tbMain; private static Tcp01c tcp01 = null; private static int count=0; public Tcp01c() { tcp01 = this; //create the backCommand, okCommand and the exitCommand exitCommand = new Command("Exit", Command.EXIT, 1); getCommand = new Command("Start", Command.SCREEN, 1); System.out.println("exit command and get command were created"); tbMain = new TextField("Enter server url", "socket://10.10.10.10:1801", 50, TextField.ANY); //creating the form form = new Form("Exchanging Msgs"); form.addCommand(exitCommand); form.append(tbMain); form.addCommand(getCommand); System.out.println("exit command and get command were appended to the form"); //instantiating the listener CommandListener listener = new CommandListener() { public void commandAction(Command c,Displayable d) { if(c==exitCommand) { System.out.println("exitCommand was pressed"); exit(); } else if(c==getCommand) { System.out.println("start command pressed"); connectionDetails = tbMain.getString(); t = new Thread(tcp01); t.start(); } } public void exit() { destroyApp(true); notifyDestroyed(); } } ; //setting the listeners form.setCommandListener(listener); //getting the display object display = Display.getDisplay(this); } public void startApp() { display.setCurrent(form); System.out.println("form was set as the current screen"); } public void pauseApp() { } public void destroyApp(boolean cond) { display = null; form = null; exitCommand = null; } public void run() { try { //establishing a connection with the remote server System.out.println("about to get a stream connection"); sc = (StreamConnection)Connector.open(connectionDetails,Connector.READ_WRITE); System.out.println("input connection was created"); //input stream is created on top //of the stream connection object dis = sc.openDataInputStream(); System.out.println("input stream was created"); dos = sc.openDataOutputStream(); System.out.println("output stream was created"); } catch(IOException ioe) { System.out.println("Unable to open conn/streams"); ioe.printStackTrace(); } while(count < 10) { try { //send data dos.write(("Message no. "+ count++ ).getBytes()); dos.write(10); dos.flush(); //receiving data byte []rb = new byte[1000]; int length = dis.read(rb); System.out.println("BYTES RECEIVED "+length+" AND "+new String(rb,0,length)); //for(int k= 0;k<length;k++) //{ // System.out.println("byte["+k+"]="+rb[k]); //} //displaying the received data if(form.size()!=0) { form.delete(0); } form.append(new String(rb,0,length)); //form.append(new StringItem("DOLLAR NIS exchange rate is ", sb.toString())); length = dis.read(rb); System.out.println("BYTES RECEIVED "+length+" AND "+new String(rb,0,length)); //displaying the received data if(form.size()!=0) { form.delete(0); } form.append(new String(rb,0,length)); //form.append(new StringItem("DOLLAR NIS exchange rate is ", sb.toString())); } catch(Exception e) { if(form.size()!=0) { form.delete(0); } form.append(new StringItem("problem:",e.getMessage())); e.printStackTrace(); } try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();} } try { //releasing resources that were allocated if(dis!=null) dis.close(); if(dos!=null) dos.close(); if(sc!=null) sc.close(); } catch(IOException ioe) { System.out.println("Could not free the resources"); ioe.printStackTrace(); } } } ------------------------------------------------------------------------------- server: import java.net.*; import java.io.*; public class Tcp01s { public static void main(String args[]) { int port; ServerSocket server_socket; BufferedReader input=null; BufferedWriter output=null;; try { port = Integer.parseInt(args[0]); } catch (Exception e) { System.out.println("port = 1801 (default)"); port = 1801; } try { server_socket = new ServerSocket(port); System.out.println("Server waiting for client on port " + server_socket.getLocalPort()); // server infinite loop while(true) { Socket socket = server_socket.accept(); System.out.println("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort()); input = new BufferedReader(new InputStreamReader(socket.getInputStream())); output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); // print received data try { String message = null; while(true) { message=""; message = input.readLine(); System.out.println("GOT MESSAGE: "+message); System.out.flush(); System.out.println("SENDING: "+"reply to:"+message); output.write("reply to:"+message); output.flush(); //output.close(); try{Thread.sleep(2000);}catch(Exception e){e.printStackTrace();} System.out.println("SENDING: "+"reply 2 to:"+message); output.write("reply 2 to:"+message); output.flush(); } } catch (IOException e) { System.out.println(e); } // connection closed by client try { socket.close(); System.out.println("Connection closed by client"); } catch (IOException e) { System.out.println(e); } } } catch (IOException e) { System.out.println(e); } } } ---------------------------------------------------------- |
| v_v_nataraj |
| View Public Profile |
| Find all posts by v_v_nataraj |
|
To wauer:
Since sockets are not officially supported you can't expect all the methods to be working properly. If you say that the problem you have is with writeUTF(), then why not try a different method. I haven't tried using sockets so I can't tell you which methods work and which don't but I see that v_v_nataraj is only using write in his code (and not writeUTF), so why not try that? |
|
To be honest I tried almost any combination of operations and connector classes. Thus it must be something else.
I'll try to get the posted source code running... Best regards, Wolfgang |
|
Wolfgang,
Did you try the code Nataraj posted ? Is it working for you ? Any other way to make the 7650 communicate over TCP/IP you have found ? Gal. |
|
Gal,
Unfortunately I didn't get the sample code working and have not found any other solution. In contrast to my own code I always get an IOException ("Unable to open conn/streams"). Strange... Wolfgang |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|