为什么我无法收到来自聊天服务器的消息?
我在Visual Studio C#中使用mono for android开发了Chat Client。 我想从聊天服务器接收消息,但是聊天客户端可能正在接收它们,我似乎无法在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();
}
}
}
答案 0 :(得分: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,看看它们通常是一个很好的指标。