我如何做一个功能C#我可以从Android设备的PC和聊天客户端的服务器聊天接收消息?

时间:2011-09-27 19:05:51

标签: c# android visual-studio tcp chat

在我的C#应用​​程序聊天客户端中,我不知道如何使用C#从聊天服务器接收消息并在TextView聊天客户端中显示它们

我使用Mono for Android在C#中使用Visual Studio进行编程。

问题是......我如何执行功能,我可以从Android设备中的PC和聊天客户端的聊天服务器接收消息?当我在Windows 7中使用聊天客户端工作正常,但当我为Android设备制作时,我不能从服务器聊天到聊天客户端接收到的分数。

我认为问题如公共void RecievedMessage和private void UpdateTextbox。

抱歉,我的英语不好。 谢谢你的关注。

源代码,聊天客户端:

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Widget.EditText;
using Android.Widget.Button;
using Android.Widget.TextView;


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;

    string msgrecebida;        

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


        SetContentView(Resource.Layout.Main);


        Button enviar = FindViewById<Button>(Resource.Id.btenviar);
        Button ligar = FindViewById<Button>(Resource.Id.btligar);
        TextView text1 = FindViewById<TextView>(Resource.Id.text1);
        EditText text2 = FindViewById<EditText>(Resource.Id.text2);

          //Conexão com o servidor 
        ligar.Click += delegate
        {
            Connect();
            ligar.Enabled = false;
            text1.Text = "Connected";
        };



        enviar.Click += delegate
        {

            if (text2.Text != "")
            {
                if (text2.Text.ToLower().StartsWith("/"))
                {
                    text2.Text = "";
                }
                else
                {
                    SendMessage("MSG :" + text2.Text);
                    text2.Text = "";
                }
            }

        };



    }


   private void Invoke(OnRecievedMessage onRecievedMessage, string message)
    {
        throw new NotImplementedException();
    }

    public bool InvokeRequired { get; set; }

    public void RecievedMessage(string message)
    {
        if (InvokeRequired)
        {
            this.Invoke(new OnRecievedMessage(RecievedMessage), message);
        }
        else
        {
            UpdateTextbox(message);
        }
    }
    private void UpdateTextbox(string text)
    {
        msgrecebida += "\r\n";
        msgrecebida += text;

    }


   //Interligações Classes MainChat & Connection
    public void Disconnected(string reason)
    {
        form.Disconnected(reason);
    }





   //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')
        {
            //m_ParentForm.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 个答案:

答案 0 :(得分:2)

在我看来,(一个)问题是,当它是onCreate中声明的局部变量时,您试图访问text1,就像它是一个字段或属性一样。您需要将text1推送到范围内的某个级别,或者在Rec​​ievedMessage中获取对它的新引用。具体来说,这不是Android的问题,但与使用Mono for Android设置代码的方式不同。

通常,在Windows中使用Winforms时,您将使用在designer.cs文件中定义的所有控件作为字段的部分类。但是在Mono代码中,您将绑定到TextViews,而是绑定到OnCreate而不是实例范围。复制/粘贴代码时,您可能没有考虑到这种差异。

顺便说一句,我非常怀疑你导入的user32.dll函数会在Android上做任何有用的事情。除非Mono人员做了一些神奇的事情,否则win32 API将无法在Android上使用。