2010-12-03 2 views
0

Я хочу отправить сообщение на свой компьютер с моего телефона, используя TCP. Мой компьютер - это сервер, а мой телефон - клиент. Я могу отправить сообщение с моего телефона на свой компьютер, но на выходе я получаю нулевые символы.Почему я получаю null в своем выпуске?

Я вставляю свои коды ниже ;;

Client ::

общественного недействительный 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) {} 
} 

} 

Мой сервер:

общественного класса 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); 
    } 

} 

}

Мой выход для сервера ;;

Сервер ответил
пользователь говорит нуль H Пользователь говорит, говорит нуль H нуль Пользователь нуль H нуль е и т.д. и т.п.

Я не должен получить этот нулевой символ, почему я получаю это ?? Другое дело, мой сервер пишет в поток, но клиент не может это получить, почему? Нужно ли мне использовать отдельный поток для этого?

Спасибо айу

ответ

1

Я предположил бы, что это не ваш реальный код, и что ваш реальный код инициализации ул обнулить.

+0

Я ничего не инициализировал до нуля, но он дает мне null .. – 2010-12-05 06:05:28

Смежные вопросы