Why I am receiving null in my output?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phpuser123
    New Member
    • Dec 2009
    • 108

    Why I am receiving null in my output?

    I want to send a message to my computer from my phone using TCP..My computer is the server and my phone is the client. I am able to send a message from my phone to my computer but in the output, I get null characters ..

    I paste my codes below;;

    Client ::

    Code:
    public void startApp() { try { // establish a socket connection with remote server streamConnection = (StreamConnection) Connector.open(connectString);
    
      // create DataOuputStream on top of the socket connection
      outputStream = streamConnection.openOutputStream();
      dataOutputStream = new DataOutputStream(outputStream);
    
      // send the HTTP request
      dataOutputStream.writeChars("Hello");
      dataOutputStream.flush();
    
      // create DataInputStream on top of the socket connection
      inputStream = streamConnection.openInputStream();
      dataInputStream = new DataInputStream(inputStream);
    
      // retrieve the contents of the requested page from Web server
      String test="";
      int inputChar;
      System.out.println("Entering read...........");
      while ( (inputChar = dataInputStream.read()) != -1) {
         // test=test+((char)inputShar);
        results.append((char) inputChar);
      }
      System.out.println("Leaving read...........");
      // display the page contents on the phone screen
      //System.out.println(" Result are "+results.toString());
      System.out.println("   ");
      resultField = new StringItem(null, results.toString());
      System.out.println("Client says "+resultField);
      resultScreen.append(resultField);
      myDisplay.setCurrent(resultScreen);
    
    } catch (IOException e) {
      System.err.println("Exception caught:" + e);
    } finally {
      // free up I/O streams and close the socket connection
      try {
        if (dataInputStream != null)
          dataInputStream.close();
      } catch (Exception ignored) {}
      try {
        if (dataOutputStream != null)
          dataOutputStream.close();
      } catch (Exception ignored) {}
      try {
        if (outputStream != null)
          outputStream.close();
      } catch (Exception ignored) {}
      try {
        if (inputStream != null)
          inputStream.close();
      } catch (Exception ignored) {}
      try {
        if (streamConnection != null)
          streamConnection.close();
      } catch (Exception ignored) {}
    }
    
    }
    My server :

    Code:
    public class Main {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)  {
        // TODO code application logic here
      try{
          ServerSocket sck=new ServerSocket(880);
          Socket client=sck.accept();
          InputStream inp= client.getInputStream();
          int  i;
          OutputStream out=client.getOutputStream();
          out.write("Testing ".getBytes());
          System.out.println("Server has responded          ");
          String str="";
          while((i=inp.read())!=-1){
    
              str=str+((char) i);
               System.out.println("USer says "+ str);
          }
    
      }
      catch(Exception e){
          System.out.println("Error "+e);
      }
    
    }
    
    }
    My output for the server ;;

    Server has responded
    USer says null H User says null H null User says null H null e etc etc

    I am not supposed to get this null character,why I am getting it?? Another thing, my server is writing to the stream but the client is not able to receive that,why is that?Do I need to use a separate thread for that?

    Thanks in adv
    Last edited by Niheel; Dec 3 '10, 03:02 PM.
  • Sean Pedersen
    New Member
    • Dec 2010
    • 30

    #2
    Have you tried buffers? It's possible your data is being dropped. Also, you could consider switching to the Writer objects if all your sending is Strings.

    Comment

    • phpuser123
      New Member
      • Dec 2009
      • 108

      #3
      Thanks I already sorted, I used the available() and read(byte[],offset,len) and this solved my problem

      Comment

      Working...