Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.Threading;
  16. using System.Net.Sockets;
  17.  
  18. namespace client
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для MainWindow.xaml
  22. /// </summary>
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. private const string host = "127.0.0.1";
  30. private const int port = 8888;
  31. static TcpClient client;
  32. static NetworkStream stream;
  33.  
  34.  
  35. // получение сообщений
  36. public void ReceiveMessage()
  37. {
  38. //var outer = Task.Factory.StartNew(() =>
  39. //{
  40. while (true)
  41. // {
  42. try
  43. {
  44. byte[] data = new byte[64]; // буфер для получаемых данных
  45. StringBuilder builder = new StringBuilder();
  46. int bytes = 0;
  47. // do
  48. //{
  49. bytes = stream.Read(data, 0, data.Length);
  50. builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
  51. string message1 = builder.ToString();
  52. Dispatcher.BeginInvoke(new Action(
  53. () => lb.Items.Add(message1)));
  54. // }
  55. // while (stream.DataAvailable);
  56. }
  57. catch
  58. {
  59. Dispatcher.BeginInvoke(new Action(
  60. () => lb.Items.Add("Подключение прервано!")));
  61.  
  62. //соединение было прервано
  63. Disconnect();
  64. }
  65.  
  66. // }
  67.  
  68. //});//outer.Wait();
  69. }
  70.  
  71. public void Disconnect()
  72. {
  73.  
  74. if (stream != null)
  75. stream.Close();//отключение потока
  76. if (client != null)
  77. client.Close();//отключение клиента
  78. // Environment.Exit(0); //завершение процесса
  79. }
  80.  
  81. private void Button_Click(object sender, RoutedEventArgs e)
  82. {
  83.  
  84. string Name = tb1.Text;
  85. string Password = tb3.Text;
  86. string userName = Name + ":" + Password;
  87.  
  88. client = new TcpClient();
  89. try
  90. {
  91. client.Connect(host, port); //подключение клиента
  92. stream = client.GetStream(); // получаем поток
  93. byte[] data = Encoding.Unicode.GetBytes(Name + ":" + Password);
  94. stream.Write(data, 0, data.Length);
  95. }
  96. catch (Exception ex)
  97. {
  98. Dispatcher.BeginInvoke(new Action(
  99. () => lb.Items.Add(ex.Message)));
  100. }
  101. Thread receiveThread = new Thread(ReceiveMessage);
  102. receiveThread.Start();
  103.  
  104. // finally
  105. // {
  106. // Disconnect();
  107. // }
  108.  
  109.  
  110. }
  111.  
  112.  
  113. private void Button_Click_1(object sender, RoutedEventArgs e)
  114. {
  115. string message = tb2.Text;
  116. Dispatcher.BeginInvoke(new Action(
  117. () => lb.Items.Add("Вы: " + message)));
  118. byte[] data = Encoding.Unicode.GetBytes(message);
  119.  
  120. stream.Write(data, 0, data.Length);
  121. }
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement