×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    40

    Question problem Socket recieve data

    Hello, I am new in this area, I ty to develope J2ME program in NetBeans 5.5 that connect by socket and I send data, my program send the data perfectly but when I try to recieve the data it seems to be that the telephone movil disconect, I send and recieve data 4 time.
    I use Visual MIDLET to make the J2ME program in NetBeans 5.5, here my code:

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    import javax.microedition.io.Connector;
    import javax.microedition.io.SocketConnection;

    public class J2MESocket extends MIDlet implements CommandListener {

    /** Creates a new instance of J2MESocket */
    public J2MESocket() {
    initialize();
    }

    private Form form1;
    private Command CmdSend;
    private Command CmdClose;
    private TextField TxtTexto;
    private StringItem StrItmRespuesta;
    private String StrIp = "";
    private String StrPort = "";
    private String StrEnviarCampos = "";
    private String StrValue = "";

    public String getStrIp() {
    StrIp = "220.168.292.150";
    return StrIp;
    }

    public String getStrPort() {
    StrPort = "5002";
    return StrPort;
    }

    public String getStrEnviarCampos() {
    return StrEnviarCampos;
    }

    public void setStrValue(String StrValue) {
    this.StrValue = StrValue;
    StrItmRespuesta.setText(StrValue);
    }

    /**
    * utility method to show modal alerts
    */
    public void showMessage(String title, String txt){
    Alert a = new Alert(title);

    a.setString(txt);
    a.setTimeout(Alert.FOREVER);
    Display.getDisplay(this).setCurrent(a);
    }



    /** Called by the system to indicate that a command has been invoked on a particular displayable.
    * @param command the Command that ws invoked
    * @param displayable the Displayable on which the command was invoked
    */
    public void commandAction(Command command, Displayable displayable) {
    // Insert global pre-action code here
    if (displayable == form1) {
    if (command == CmdSend) {
    // Insert pre-action code here
    // Do nothing
    // Insert post-action code here
    StrEnviarCampos = TxtText.getString();
    ChannelComunication ChannelComunication = new ChannelComunication(this);
    ChannelComunication.start();
    ChannelComunication = null;
    Runtime.getRuntime().gc();
    } else if (command == CmdClose) {
    // Insert pre-action code here
    // Do nothing
    // Insert post-action code here
    exitMIDlet();
    }
    // Insert global post-action code here
    }

    /** This method initializes UI of the application.
    */
    private void initialize() {
    // Insert pre-init code here
    getDisplay().setCurrent(get_form1());
    // Insert post-init code here
    }

    /**
    * This method should return an instance of the display.
    */
    public Display getDisplay() {
    return Display.getDisplay(this);
    }

    /**
    * This method should exit the midlet.
    */
    public void exitMIDlet() {
    getDisplay().setCurrent(null);
    destroyApp(true);
    notifyDestroyed();
    }

    /** This method returns instance for form1 component and should be called instead of accessing form1 field directly.
    * @return Instance for form1 component
    */
    public Form get_form1() {
    if (form1 == null) {
    // Insert pre-init code here
    form1 = new Form(null, new Item[] {
    get_TxtTexto(),
    get_StrItmRespuesta()
    });
    form1.addCommand(get_CmdSend());
    form1.addCommand(get_CmdClose());
    form1.setCommandListener(this);
    // Insert post-init code here
    }
    return form1;
    }

    /** This method returns instance for CmdSend component and should be called instead of accessing CmdSend field directly.
    * @return Instance for CmdSend component
    */
    public Command get_CmdSend() {
    if (CmdSend == null) {
    // Insert pre-init code here
    CmdSend = new Command("Send", Command.OK, 1);
    // Insert post-init code here
    }
    return CmdSend;
    }

    /** This method returns instance for CmdClose component and should be called instead of accessing CmdClose field directly.
    * @return Instance for CmdClose component
    */
    public Command get_CmdClose() {
    if (CmdClose == null) {
    // Insert pre-init code here
    CmdClose = new Command("Close", Command.OK, 1);
    // Insert post-init code here
    }
    return CmdClose;
    }

    /** This method returns instance for TxtTexto component and should be called instead of accessing TxtTexto field directly.
    * @return Instance for TxtTexto component
    */
    public TextField get_TxtTexto() {
    if (TxtTexto == null) {
    // Insert pre-init code here
    TxtTexto = new TextField("Texto:", "Hello Computer.", 120, TextField.ANY);
    // Insert post-init code here
    }
    return TxtTexto;
    }

    /** This method returns instance for StrItmRespuesta component and should be called instead of accessing StrItmRespuesta field directly.
    * @return Instance for StrItmRespuesta component
    */
    public StringItem get_StrItmRespuesta() {
    if (StrItmRespuesta == null) {
    // Insert pre-init code here
    StrItmRespuesta = new StringItem("Respuesta:", "");
    // Insert post-init code here
    }
    return StrItmRespuesta;
    }

    public void startApp() {
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    40

    Re: problem Socket recieve data

    Here continue my code:
    class ChannelComunication extends Thread implements Runnable{
    private J2MESocket midlet;
    private InputStream s = null;
    private StringBuffer Sb = new StringBuffer();
    private String StrValue = "";
    private boolean BlnFinishString = false;
    private int iTotalStringFinal = 0;
    private String StrParam = "";
    private String StrSendCmp = "";

    public String getStrValue() {
    return StrValue;
    }

    public boolean GetBlnFinishString() {
    return BlnFinishString;
    }

    public CanalComunicacion(J2MESocket midlet) {
    this.midlet = midlet;
    StrParam = "socket://" + midlet.getStrIp() + ":" + midlet.getStrPort();
    StrSendCmp = midlet.getStrEnviarCampos();
    }

    public void run() {
    try {
    SendAndRecieveMessage(StrParam.trim());
    } catch(IOException MyIOException){
    System.err.println("No se pudo realizar la conección");
    }//Try... catch(IOException MyIOException)
    }

    synchronized void SendAndRecieveMessage(String url) throws IOException {
    SocketConnection c = null;
    OutputStream o = null;
    Sender sender;
    try {
    //READ_WRITE
    c = (SocketConnection)Connector.open(url, Connector.READ_WRITE, true);

    c.setSocketOption(SocketConnection.DELAY, 5);
    c.setSocketOption(SocketConnection.LINGER, 25);
    c.setSocketOption(SocketConnection.KEEPALIVE, 1);

    o = c.openOutputStream();
    s = c.openInputStream();


    //synchronized(o){
    if(c.getLocalAddress().compareTo("") != 0)
    {
    sender = new Sender(o);
    sender.send(StrSendCmp);

    } else {
    }
    StrValue = "";
    synchronized(s){
    while (true){
    int ch = 0;
    while (((ch = s.read()) != -1) && (ch != -1)) {
    try
    {
    Sb.append((char) ch);
    } catch (Exception e) {
    }
    try{
    StrValue = Sb.toString();
    } catch (Exception e) {
    }
    try{
    midlet.setStrValue(StrValue);
    } catch (Exception e) {
    }
    }//while (((ch = s.read()) != -1) && (ch != -1))
    }//while (true)
    }//synchronized(s)
    } finally {
    if (s != null)
    s.close();
    if (c != null)
    c.close();
    }//Try... synchronized(s)}
    }

    public synchronized String Recieve()
    {
    return StrValue;
    }
    }

    class Sender extends Thread {

    private OutputStream os;
    private String message;

    public Sender(OutputStream os) {
    this.os = os;
    }

    public synchronized void send(String msg) {
    message = msg;
    notify();
    start();
    }

    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(("<html>" + message + "\n\13").getBytes());
    os.flush();
    } catch (IOException ioe) {
    ioe.printStackTrace();
    }

    // Completed client handling, return handler to pool and
    // mark for wait
    message = null;
    break;
    }
    }

    public synchronized void stop() {
    message = null;
    notify();
    }
    }

    When I insert this code "midlet.setStrValue(StrValue);" I began to lose the connection, and when I remove this code "midlet.setStrValue(StrValue);" it works perfectly, send and recieve the data perfectly.
    This code: "midlet.setStrValue(StrValue);" let me print in the screen of my telephone mobile and see the data I recieve.
    How can I see the data without lose the connection?.
    Thanks in advance.

  3. #3
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: problem Socket recieve data

    I recommend you don't attempt to send and receive on the same socket from different threads.

    Also.. I don't see where you close the OutputStream. Both streams must be closed, as must the connection, or you will run out of resources.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    40

    Re: problem Socket recieve data

    I correct all items and I send and recieve data four times then I lose the connection, and this happen When I insert this code "midlet.setStrValue(StrValue);" I lose the connection, and when I remove this code "midlet.setStrValue(StrValue);" it works perfectly, send and recieve the data perfectly.
    How can I see the data without lose the connection?.
    Thanks in advance.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    40

    Re: problem Socket recieve data

    I did it, but sometime I get this message "Network bussy", I try Nokia 5200, Nokia 6070 and Nokia N95, but in Nokia 5200, Nokia 6070 sometime I get this message "Network bussy", in Nokia N95 I don't get this message "Network bussy", what happen in Nokia 5200, Nokia 6070 in that moment?, I don't understand, can somebody help me?.
    Thank in advance.
    Last edited by aeramcm; 2009-01-14 at 14:58.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    40

    Re: problem Socket recieve data

    Hello, resolve this problem, thanks, now I have problem with the connection, I want to know if I can know if the application is still connected before try to send some data, there's some device when the application is not connected and try to send data, the application run an exeption that I can not read because the devices Shutdown and other devices the application get deadlock, here the supplier connection are very unstable, please can somebody help with this issue?, thanks in advance.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    40

    Re: problem Socket recieve data

    Thanks, I resolved the problem.

Similar Threads

  1. Problem with socket sending/receiving data
    By chycai in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2008-03-24, 10:26
  2. Problem with sending data on connected UDP socket (S60 3rd MR2)
    By jeeza2006 in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2008-01-04, 08:54
  3. Socket TCP/IP data transmission failed..on 6680
    By yilunyou in forum Symbian Networking & Messaging (Closed)
    Replies: 0
    Last Post: 2006-11-19, 04:12
  4. Emulator gets closed when data reaches on socket....
    By Amit Mahajan in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2006-05-04, 07:50
  5. problem with socket write
    By srkreddy999 in forum Symbian Networking & Messaging (Closed)
    Replies: 1
    Last Post: 2005-11-02, 14:33

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×