Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Android.App;
- using Android.Widget;
- using Android.OS;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- namespace AndroidClientChat
- {
- [Activity(Label = "AndroidClientChat", MainLauncher = true, Icon = "@drawable/icon")]
- public class Activity1 : Activity
- {
- Socket client;
- byte[] datalength = new byte[4];
- Button buttonSend;
- Button btnLayout;
- Button pencetan;
- EditText etextSend;
- EditText txtSend2;
- TextView textReceive;
- TextView txtReceive;
- bool layar2;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
- buttonSend = FindViewById<Button>(Resource.Id._buttonSend);
- btnLayout = FindViewById<Button>(Resource.Id.buttong);
- etextSend = FindViewById<EditText>(Resource.Id._etextSend);
- textReceive = FindViewById<TextView>(Resource.Id._textviewReceive);
- client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- client.Connect(IPAddress.Parse("192.168.2.104"), 8910);
- Toast.MakeText(this, "Connecting...", ToastLength.Short).Show();
- buttonSend.Click += buttonSend_Click;
- btnLayout.Click += butonLayout_Click;
- //ThreadPool.QueueUserWorkItem(o => receiveData());
- if (client.Connected)
- {
- buttonSend.Enabled = true;
- clientReceive();
- }
- }
- private void clientReceive()
- {
- try {
- new Thread(() =>
- {
- while (client.Connected && !layar2)
- {
- byte[] data = new byte[client.Available];
- client.Receive(data);
- this.RunOnUiThread(() => { this.textReceive.Text = Encoding.ASCII.GetString(data).Replace("\0", "") + this.textReceive.Text; } );
- }
- client.Send(Encoding.ASCII.GetBytes("Keluar dari thread #1\r\n"));
- //client.Close();
- }).Start();
- } catch (Exception ex) {Toast.MakeText(this, ex.Message, ToastLength.Short).Show(); }
- }
- private void ClientReceive2()
- {
- try {
- client.Send(Encoding.ASCII.GetBytes("Start thread #2\r\n"));
- new Thread(() =>
- {
- while (client.Connected)
- {
- byte[] data = new byte[client.Available];
- client.Receive(data);
- this.RunOnUiThread(() => { this.txtReceive.Text = Encoding.ASCII.GetString(data).Replace("\0", "") + this.txtReceive.Text; } );
- }
- //client.Close();
- }).Start();
- } catch (Exception ex) {Toast.MakeText(this, ex.Message, ToastLength.Short).Show(); }
- }
- void butonLayout_Click(object sender, EventArgs e)
- {
- layar2 = true;
- client.Send(Encoding.ASCII.GetBytes("Ke layar #2\r\n"));
- SetContentView(Resource.Layout.layout1);
- pencetan = FindViewById<Button>(Resource.Id.pencetan);
- txtSend2 = FindViewById<EditText>(Resource.Id.autoCompleteTextView1);
- txtReceive = FindViewById<TextView>(Resource.Id.textView2);
- pencetan.Click += btnPay_Click;
- ClientReceive2();
- }
- void buttonSend_Click(object sender, EventArgs e)
- {
- client.Send(Encoding.ASCII.GetBytes(this.etextSend.Text+"\r\n"));
- this.etextSend.Text = "";
- }
- void btnPay_Click(object sender, EventArgs e)
- {
- client.Send(Encoding.ASCII.GetBytes(this.txtSend2.Text + "\r\n"));
- this.txtSend2.Text = "";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement