Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using Android.App;
  4. using Android.Widget;
  5. using Android.OS;
  6. using System.Text;
  7. using System.Net;
  8. using System.Net.Sockets;
  9.  
  10. namespace AndroidClientChat
  11. {
  12. [Activity(Label = "AndroidClientChat", MainLauncher = true, Icon = "@drawable/icon")]
  13.  
  14. public class Activity1 : Activity
  15. {
  16. Socket client;
  17. byte[] datalength = new byte[4];
  18. Button buttonSend;
  19. Button btnLayout;
  20. Button pencetan;
  21. EditText etextSend;
  22. EditText txtSend2;
  23. TextView textReceive;
  24. TextView txtReceive;
  25. bool layar2;
  26.  
  27. protected override void OnCreate(Bundle bundle)
  28. {
  29. base.OnCreate(bundle);
  30. SetContentView(Resource.Layout.Main);
  31. buttonSend = FindViewById<Button>(Resource.Id._buttonSend);
  32.  
  33. btnLayout = FindViewById<Button>(Resource.Id.buttong);
  34. etextSend = FindViewById<EditText>(Resource.Id._etextSend);
  35.  
  36. textReceive = FindViewById<TextView>(Resource.Id._textviewReceive);
  37.  
  38.  
  39.  
  40. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  41. client.Connect(IPAddress.Parse("192.168.2.104"), 8910);
  42. Toast.MakeText(this, "Connecting...", ToastLength.Short).Show();
  43.  
  44. buttonSend.Click += buttonSend_Click;
  45. btnLayout.Click += butonLayout_Click;
  46.  
  47.  
  48.  
  49.  
  50. //ThreadPool.QueueUserWorkItem(o => receiveData());
  51. if (client.Connected)
  52. {
  53. buttonSend.Enabled = true;
  54. clientReceive();
  55. }
  56.  
  57.  
  58.  
  59. }
  60.  
  61.  
  62.  
  63. private void clientReceive()
  64. {
  65. try {
  66. new Thread(() =>
  67. {
  68. while (client.Connected && !layar2)
  69. {
  70. byte[] data = new byte[client.Available];
  71. client.Receive(data);
  72.  
  73. this.RunOnUiThread(() => { this.textReceive.Text = Encoding.ASCII.GetString(data).Replace("\0", "") + this.textReceive.Text; } );
  74. }
  75. client.Send(Encoding.ASCII.GetBytes("Keluar dari thread #1\r\n"));
  76. //client.Close();
  77. }).Start();
  78. } catch (Exception ex) {Toast.MakeText(this, ex.Message, ToastLength.Short).Show(); }
  79. }
  80. private void ClientReceive2()
  81. {
  82. try {
  83. client.Send(Encoding.ASCII.GetBytes("Start thread #2\r\n"));
  84. new Thread(() =>
  85. {
  86. while (client.Connected)
  87. {
  88. byte[] data = new byte[client.Available];
  89. client.Receive(data);
  90.  
  91. this.RunOnUiThread(() => { this.txtReceive.Text = Encoding.ASCII.GetString(data).Replace("\0", "") + this.txtReceive.Text; } );
  92. }
  93.  
  94. //client.Close();
  95. }).Start();
  96. } catch (Exception ex) {Toast.MakeText(this, ex.Message, ToastLength.Short).Show(); }
  97. }
  98.  
  99. void butonLayout_Click(object sender, EventArgs e)
  100. {
  101. layar2 = true;
  102. client.Send(Encoding.ASCII.GetBytes("Ke layar #2\r\n"));
  103.  
  104. SetContentView(Resource.Layout.layout1);
  105. pencetan = FindViewById<Button>(Resource.Id.pencetan);
  106. txtSend2 = FindViewById<EditText>(Resource.Id.autoCompleteTextView1);
  107. txtReceive = FindViewById<TextView>(Resource.Id.textView2);
  108.  
  109. pencetan.Click += btnPay_Click;
  110.  
  111. ClientReceive2();
  112. }
  113.  
  114. void buttonSend_Click(object sender, EventArgs e)
  115. {
  116. client.Send(Encoding.ASCII.GetBytes(this.etextSend.Text+"\r\n"));
  117. this.etextSend.Text = "";
  118. }
  119.  
  120. void btnPay_Click(object sender, EventArgs e)
  121. {
  122. client.Send(Encoding.ASCII.GetBytes(this.txtSend2.Text + "\r\n"));
  123. this.txtSend2.Text = "";
  124. }
  125.  
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement