2010-12-03 17 views
0

我想用我的手机给我的电脑发送信息,使用TCP ..我的电脑是服务器,我的电话是客户端。我能够从我的手机发信息给我的电脑,但在输出中,我得到空字符..为什么我在输出中收到空?

我粘贴下面我的代码;;

客户::

公共无效的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); 
    } 

} 

}

我的服务器输出;;

服务器响应
用户说空^ h 用户说空^ h空 用户说空^ h空è 等等等等

我不应该得到这个空字符,为什么我得到它? 另一件事,我的服务器正在写入流,但客户端无法接收,为什么?我需要使用一个单独的线程?

谢谢进阶

回答

1

我猜想,是不是你真正的代码,你的实际代码初始化海峡为null。

+0

我因此未初始化什么空的,但它给了我空... – 2010-12-05 06:05:28

相关问题