2010-12-03 25 views
0

私はTCPを使って私の電話から私のコンピュータにメッセージを送りたいと思っています。私のコンピュータはサーバで、私の電話はクライアントです。電話からコンピュータにメッセージを送信することはできますが、出力ではヌル文字が表示されます。出力にnullが表示されるのはなぜですか?

私は以下のコードを貼り付けています。

クライアント::

ます。public voidにstartApp(){ 試し{ //リモートサーバ の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) {} 
} 

} 

マイサーバ:

パブリッククラスメインサーバ用{

/** 
* @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); 
    } 

} 

}

マイ出力;;

Serverは
ユーザーがnull H ユーザーがnull Hのヌル ユーザーが言う言う言う応答したヌルHのヌル電子 は、などなど私は、なぜ私はそれを取得しています、このヌル文字を取得することになっていないのです

? 別のこととして、私のサーバーはストリームに書き込んでいますが、クライアントはそれを受け取ることができません。それはなぜですか?そのために別のスレッドを使用する必要がありますか? ADV

答えて

1

に私はそれがあなたの本当のコードではありません、そして、あなたの実際のコードがnullに初期化STRよね

感謝。

+0

nullに何も初期化していませんでしたが、それは私にnullを返します。 –

+0

完了している必要があります。投稿したコードは、あなたが記述したとおりに動作しません。 strにテキストnullを格納できる唯一の方法は、そこに置くか、最初はnullだった場合です。 – EJP

関連する問題