2011-10-05 138 views
0

爲什麼我無法從聊天服務器收到消息?爲什麼我無法從聊天服務器收到消息?

我開發的聊天客戶端在Visual Studio C#與mono爲Android。 我希望收到來自聊天服務器mesagens他們被送到但是他聊天客戶端可以接收他們,我似乎無法在Text1.Text

的源代碼聊天客戶端,以示對接收消息:

//Criado por EcoDuty, Frederico Vaz 

    using System; 
    using Android.App; 
    using Android.Content; 
    using Android.Runtime; 
    using Android.Views; 
    using Android.Widget; 
    using Android.OS; 

    // 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Text; 
using System.Net; 
using System.Net.Sockets; 
using System.IO; 
using System.Threading; 
using System.Runtime.InteropServices; 

namespace ChatClient_Android 
{ 
[Activity(Label = "ChatClient_Android", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainChat : Activity 
{ 

    public delegate void OnRecievedMessage(string message); 

    public MainChat form; 
    const int WM_VSCROLL = 0x115; 
    const int SB_BOTTOM = 7; 

    TextView text1; 

    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 

     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     // Get our button from the layout resource, 
     // and attach an event to it 
     Button ligar = FindViewById<Button>(Resource.Id.btligar); 
     text1 = FindViewById<TextView>(Resource.Id.text1); 

     //Conexão com o servidor 
     ligar.Click += delegate 
     { 
      Connect(); 
      ligar.Enabled = false; 

     }; 

    } 

    //Função Actualizar a Caixa de Entrada de Mensagens 
    private void UpdateTextbox(string text) 
    { 
     text1.Text += "\r\n"; 
     text1.Text += text; 
    } 

    //Recieved Mesages 
    public void RecievedMessage(string message) 
    { 
     UpdateTextbox(message);  
    } 

    //TCP Connection 
    public StreamWriter Outgoing; 
    private StreamReader Incoming; 
    private TcpClient Connection; 
    private Thread Messages; 
    private IPAddress IP; 
    //public string host; 
    //public string nick; 
    //MainChat m_ParentForm; 
    bool isConnected; 

    //Função Conectar 
    public void Connect() 
    { 
     try 
     { 
      IP = IPAddress.Parse("10.0.2.2"); 
      Connection = new TcpClient(); 
      Connection.Connect(IP, 1986); 
      Outgoing = new StreamWriter(Connection.GetStream()); 
      Outgoing.WriteLine("EcoDuty"); 
      Outgoing.Flush(); 
      //m_ParentForm.Vis(true); 
      Messages = new Thread(new ThreadStart(this.Communication)); 
      Messages.Start(); 
     } 
     catch (Exception e) { Disconnected(e.Message); } 
    } 
    private void Communication() 
    { 
     Incoming = new StreamReader(Connection.GetStream()); 
     string check = Incoming.ReadLine(); 
     if (check[0] == '1') 
     { 
      //Vis(true); 
      isConnected = true; 
     } 
     else 
     { 
      string Reason = "Disconnected: "; 
      Reason += check.Substring(2, check.Length - 2); 
      Disconnected(Reason); 
      return; 
     } 
     while (isConnected == true) 
     { 
      try 
      { 
       ServerMessage(Incoming.ReadLine()); 
      } 
      catch (Exception e) 
      { 
       if (isConnected == true) 
       { 
        Disconnected("Connection to server lost"); 
        Console.WriteLine("Connection Lost: " + e.ToString()); 
       } 
       break; 
      } 
     } 
    } 
    private void ServerMessage(string message) 
    { 
     try 
     { 
      RecievedMessage(message); 
     } 
     catch { } 
    } 
    public void CloseConnection() 
    { 
     isConnected = false; 
     Incoming.Close(); 
     Outgoing.Close(); 
     Connection.Close(); 
     Messages.Abort(); 
    } 
    public void SendMessage(string message) 
    { 
     Outgoing.WriteLine(message); 
     Outgoing.Flush(); 
    } 



} 

}

+1

很容易問爲什麼?我們應該怎麼知道?這就像問「爲什麼我不是milionaire?」你期望我們複製你的代碼,設置Android開發環境並回復你嗎?你有沒有嘗試調試你的代碼?你有沒有試圖找到你的代碼不工作的最小部分? –

+0

是的...它是代碼的一部分,也不工作:P – Zav

+0

@ user971350 - 您*需要*清楚地解釋*關於代碼不能工作。 – TheCloudlessSky

回答

1

你似乎在試圖更新從一個非UI線程的文本(如果按照呼叫棧你看到的方法是從你生成一個專門的線程觸發):

private void UpdateTextbox(string text) 
{ 
    text1.Text += "\r\n"; 
    text1.Text += text; 
} 

而是使用RunOnUiThread()安排文本換到UI線程上運行:

private void UpdateTextbox(string text) 
{ 
    RunOnUiThread(() => 
    { 
     text1.Text += "\r\n"; 
     text1.Text += text; 
    }); 
} 

你也應該擺脫空異常醒目的你前進的道路上做的 - 這很可能掩蓋了問題。

也總是檢查catlog的例外,他們通常是一個很好的指標。

+0

好吧..我去試試:P – Zav

+0

謝謝......這很好:P – Zav

相關問題