Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6660793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:11:58+00:00 2026-05-26T02:11:58+00:00

Why can’t I receive messages from the server chat? I developed the client chat

  • 0

Why can’t I receive messages from the server chat?

I developed the client chat in Visual Studio C# with mono for Android.
I want to receive messages from the server chat. They are sent but the client chat may be receiving them and I cannot show them in Text1.Text

The chat client source code for receiving messages:

  //Criado por EcoDuty

  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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:11:59+00:00Added an answer on May 26, 2026 at 2:11 am

    You seem to be trying to update the text from a non UI thread (if you follow the calls stack you see that the method is triggered from a dedicated thread you spawn):

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

    Instead use RunOnUiThread() to schedule the text change to run on the UI thread:

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

    Also you should get rid of the empty exception catching you do along the way – this most likely masked the problem.

    Also always check the catlog for exceptions they usually are a good indicator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone guide me on a possible solution? I don't want to use /bin/cp
Can't work out a way to make an array of buttons in android. This
can someone show me the regex for this preg_match. I want to make sure
Does anyone know how can I replace this 2 symbol below from the string
Can I open my custom developed form [instead of native one] when user opens
Can the Application Name passed to SQL Server (via the ADO Connection string) be
Can we do following in SQL Server 2005 query with convert function. MaxRegID =
Can somebody point me to a resource that explains how to go about having
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
Can you cast a List<int> to List<string> somehow? I know I could loop through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.