开发者

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 ::

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 :

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 Use开发者_开发知识库r 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


I would guess that isn't your real code, and that your real code initialized str to null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜