开发者

How I do a function C# for i can receive messages from Server Chat in PC and Chat Client in Android device?

In my C # application Chat Client I don`t not know how to do a function to receive messages from the Chat Server in C # and presents them in a TextView Chat Client

I'm programming in Visual Studio in C # using Mono for Android.

The question is... How I do a function for i can receive messages from Chat Server in PC and Chat Client in Android device? When i use Chat Client in windows 7 works good but when i make for a android device i can`t receive mensages from Server Chat to Chat Client.

I think the problem as in public void RecievedMessage and private void UpdateTextbox.

Sorry, my bad english. Thanks your atention.

The Source Code, Chat Client:

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<Ed开发者_运维百科itText>(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();
    }

}
}


It looks to me like (one) problem is that you are trying to access text1 as if it is a field or property when it is a local variable declared in onCreate. You either need to push text1 up a level in scope, or grab a new reference to it in RecievedMessage. This is not a problem with Android, specifically, but is a difference with how the code is set up with Mono for Android.

Typically, when working with Winforms in Windows, you'll be working in a partial class with all your controls defined in the designer.cs file as fields. But in your Mono code, you are binding to TextViews instead, and in your OnCreate instead of at instance scope. When you copy/pasted your code, you likely didn't account for that difference.

As an aside, I highly doubt that the user32.dll function you are importing is going to do anything useful on Android. Unless the Mono folks are doing something magical, the win32 API will not be available on Android.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜