为什么我无法从聊天服务器收到消息?

问题描述:

为什么我无法从聊天服务器收到消息?为什么我无法从聊天服务器收到消息?

我开发的聊天客户端在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

你似乎在试图更新从一个非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