2013-10-01 77 views
0

正如標題所說:我可以從客戶端接收服務器上的消息,但是我無法從服務器上檢索到客戶端的回覆。 任何想法爲什麼?這是客戶端和服務器的代碼。Java Android - 可以發送消息到服務器,但不能從它接收

客戶端代碼

package com.example.clienttest; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.net.Socket; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    Thread m_objThreadClient; 
    Socket clientSocket; 
    TextView serverMessage; 
    EditText clientMessage; 
    String sIn = "Nothing", sOut; 
    DataOutputStream oos; 
    DataInputStream ois; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     serverMessage = (TextView) findViewById(R.id.textView1); 
     clientMessage = (EditText) findViewById(R.id.editText1); 


     m_objThreadClient = new Thread(new Runnable(){ 
      public void run() 
      { 
       try { 
        clientSocket = new Socket("192.168.1.55", 4000); 
        oos = new DataOutputStream (clientSocket.getOutputStream()); 
        ois = new DataInputStream (clientSocket.getInputStream()); 


       } catch (Exception e) { 
        serverMessage.setText(e.getMessage()); 
       } 

      } 
     }); 
     m_objThreadClient.start(); 


    } 

    public void Start(View view) { 
     sOut = clientMessage.getText().toString(); 
      try { 
       oos.writeUTF(sOut); 

       ServerConn sc = new ServerConn(clientSocket); 
       Thread t = new Thread(sc); 
       t.start(); 

       serverMessage.setText("Reply: " + sc.msg + "\n"); 

       } catch (Exception e) { 
       serverMessage.setText(e.getMessage()); 
       } 

    } 

    public void onStop(){ 
     try { 
      oos.close(); 
      ois.close(); 
      clientSocket.close(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

class ServerConn implements Runnable { 
    private DataInputStream ois = null; 
    public String msg; 

    public ServerConn(Socket server) throws IOException { 
     ois = new DataInputStream (server.getInputStream()); 
    } 

    public void run() { 
     try { 

      while ((msg = ois.readUTF()) != null) { 
      } 
     } catch (IOException e) { 
      msg = e.toString(); 
     } 
    } 
} 

Server代碼

import java.net.ServerSocket; 
import java.net.Socket; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.ObjectInputStream; 
import java.io.ObjectOutputStream; 
import java.io.PrintWriter; 

import java.util.Hashtable; 

public class Server2 { 

    @SuppressWarnings("resource") 
    public static void main (String[] args) throws IOException { 

     ServerSocket server = null; 
     try { 
      server = new ServerSocket(4000); 
     } catch (IOException e) { 
      System.err.println("Could not start up on: " + "4000" + "Maby server is already open? Or a portforwording messup?"); 
      System.err.println(e); 
      System.exit(1); 
     } 

     Socket client = null; 
     while(true) { 
      try { 
       client = server.accept(); 
       System.out.print("Connected "); 
      } catch (IOException e) { 
       System.err.println("Accept failed."); 
       System.err.println(e); 
      } 

      Thread t = new Thread(new ClientConn(client)); 
      t.start(); 
     } 
    } 
} 


class ClientConn implements Runnable { 
    private Socket client; 
    String Recv, Send; 
    DataInputStream inFromClient; 
    DataOutputStream outToClient; 

    ClientConn(Socket client) { 
     this.client = client; 
     try { 

      inFromClient = new DataInputStream(client.getInputStream()); 
      outToClient = new DataOutputStream(client.getOutputStream()); 
      } catch (IOException e) { 
      e.printStackTrace(); 
      } 
    } 

    public void run() { 
     try { 
      while ((Recv = inFromClient.readUTF()) != null) { 

       System.out.print("Msg: " + Recv + " \n"); 
       if(Recv.equals("Hi")){ 
        Send = "Wa alaikum"; 
       } 
       else{ 
        Send = "Wat?"; 
       } 

       outToClient.writeUTF(Send); 
       outToClient.flush(); 
       System.out.print("Replying:" + Send + "\n"); 
      } 
     } catch (IOException e) { 
      System.out.print("No input "); 
      System.err.println(e); 
     } 
    } 

} 
+0

客戶端的Start()方法是如何調用的? – NormR

+0

當我按下按鈕 –

回答

0

當它從該線程啓動後沒有在服務器讀取ServerConn msg變量給出的值運行ServerConn對象。需要有一些代碼(Handler ??)在GUI線程上運行,它在獲取消息時從ServerConn類中調用。 在run()方法中添加一些調試代碼,可以在讀取msg時打印出msg的值,以便您可以查看msg中的值。打印輸出將在LogCat中可見

+0

您是否暗示我無法在正確的時間檢索msg變量,因爲它在代碼中使用了一點「稍後」? Fyi我垃圾郵件的「開始」功能,仍然沒有改變。 –

+0

當調用setText()方法時,msg可能不存在。添加一些println語句來查看msg何時可用以及何時調用setText()方法? – NormR

+0

我忘了添加...我嘗試了模擬器上的程序,它的功能就像一個魅力...我可以接收和發送消息 –

相關問題