c# - Why I can't recieved messages from Chat Server? -
why can't recieved messages chat server ?
i developed chat client in visual studio c# mono android. want receive mesagens chat server sent chat client may receiving them , can not seem show in text1.text
the source code chat client receiving messages:
//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 "main" layout resource setcontentview(resource.layout.main); // our button layout resource, // , attach event 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 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 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(); } }
}
you seem trying update text non ui thread (if follow calls stack see method triggered dedicated thread spawn):
private void updatetextbox(string text) { text1.text += "\r\n"; text1.text += text; }
instead use runonuithread()
schedule text change run on ui thread:
private void updatetextbox(string text) { runonuithread(() => { text1.text += "\r\n"; text1.text += text; }); }
also should rid of empty exception catching along way - masked problem.
also check catlog exceptions indicator.
Comments
Post a Comment