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空è 等等等等
我不應該得到這個空字符,爲什麼我得到它? 另一件事,我的服務器正在寫入流,但客戶端無法接收,爲什麼?我需要使用一個單獨的線程?
謝謝進階
我因此未初始化什麼空的,但它給了我空... – 2010-12-05 06:05:28