title: A connection is made between server and client but unable to send data via OutputStream in J2ME
body: <p>I started to program client/server applications in J2ME recently.Now I'm working with c++ builder 2010 indy components (e.g. TidTTCPServer) and J2ME. My application is designed to restart the kerio winroute firewall service from a remote machine. 
  My server application is written in c++ builder 2010, I've put a TidTCTServer component into a form which binded to 127.0.0.1:4500. That's listening on port 4500 in local machine.
  Then i've added a listbox that i need to add every upcoming packets converted to UnicodeString. </p>
  
  <pre><code>//void __fastcall TForm1::servExecute(TIdContext *AContext)
  
          UnicodeString s;
   UnicodeString txt;
   txt=Trim(AContext-&gt;Connection-&gt;IOHandler-&gt;ReadLn());
   otvet-&gt;Items-&gt;Add(txt);
   otvet-&gt;ItemIndex=otvet-&gt;Items-&gt;Count-1;
   if (txt=="1") {
     AContext-&gt;Connection-&gt;IOHandler-&gt;WriteLn("Suhrob");
     AContext-&gt;Connection-&gt;Disconnect();
   }
   if (txt=="2") {
     AContext-&gt;Connection-&gt;IOHandler-&gt;WriteLn("Shodi");
     AContext-&gt;Connection-&gt;Disconnect();
   }
  </code></pre>
  
  <p>//----------------------------------------------------------------------------</p>
  
  <pre><code>// void __fastcall TForm1::servConnect(TIdContext *AContext)
  
    ++counter;
   status-&gt;Panels-&gt;Items[0]-&gt;Text="Connections:" + IntToStr(counter);
   status-&gt;Panels-&gt;Items[1]-&gt;Text="Connected to " + AContext-&gt;Connection-&gt;Socket-&gt;Binding-&gt;PeerIP + ":" + AContext-&gt;Connection-&gt;Socket-&gt;Binding-&gt;PeerPort;
  </code></pre>
  
  <p>and my client side code looks smth like this:</p>
  
  <pre><code>else if (command == send) {                                          
       // write pre-action user code here
              InputStream is=null;
              OutputStream os=null;
              SocketConnection client=null;
              ServerSocketConnection server=null;
              try {
                  server = (ServerSocketConnection) Connector.open("socket://"+IP.getString()+":"+PORT.getString());
                  // wait for a connection
                  client = (SocketConnection) Connector.open("socket://"+IP.getString()+":"+PORT.getString());
                  // set application-specific options on the socket. Call setSocketOption to set other options
                  client.setSocketOption(SocketConnection.DELAY, 0);
                  client.setSocketOption(SocketConnection.KEEPALIVE, 0);
                  is = client.openInputStream();
                  os = client.openOutputStream();
                  // send something to server
                  os.write("texttosend".getBytes());
                  // read server response
                  int c = 0;
                  while((c = is.read()) != -1) {
                     // do something with the response
                      System.out.println((char)c);
                  }
  
                  // close streams and connection
              }
               catch( ConnectionNotFoundException error )
             {
                   Alert alert = new Alert(
                      "Error", "Not responding!", null, null);
                   alert.setTimeout(Alert.FOREVER);
                   alert.setType(AlertType.ERROR);
                   switchDisplayable(alert, list);
              }
              catch (IOException e)
              {
                   Alert alert = new Alert("ERror", e.toString(), null, null);
                   alert.setTimeout(Alert.FOREVER);
                   alert.setType(AlertType.ERROR);
                   switchDisplayable(alert, list);
                   e.printStackTrace();
  
              }
  
            finally {
            if (is != null) {
              try {
                is.close();
              } catch (Exception ex) {
                System.out.println("Failed to close is!");
              }
              try {
                os.close();
              } catch (Exception ex) {
                System.out.println("Failed to close os!");
              }
            }
            if (server != null) {
              try {
                server.close();
              } catch (Exception ex) {
                System.out.println("Failed to close server!");
              }
            }
            if (client != null) {
              try {
                client.close();
              } catch (Exception ex) {
                System.out.println("Failed to close client!");
              }
            }
          }
  </code></pre>
  
  <p>my client application gets connected with the server but when i try to send data such as </p>
  
  <pre><code>os.write("texttosend".getBytes());
  </code></pre>
  
  <p>I cannot get text data on the server using. That's I am not getting sent packets in the server from client.</p>
  
  <pre><code>txt=Trim(AContext-&gt;Connection-&gt;IOHandler-&gt;ReadLn()); 
  </code></pre>
  
  <p>Guys, where am I wrong? is the way i'm doing is ok?</p>
  
  <p>Or do I need to use StreamConnection instead of SocketConnection?</p>
  
  <p>And when i use telnet to send data it works cool, strings will be added to listbox</p>
  
  <pre><code>telnet 127.0.0.1 4500
  
  texttosend
  23
  asf
  </code></pre>
  
  <p>Any help is appreciated !!!
  Thanks in advance!</p>