private void Listen_Click(object sender, RoutedEventArgs e)
 {
     Utils_.ActionInThread(() =>
     {
         try
         {
             string ListenPortStr = "";
             string ListenIPStr   = "";
             Utils_.ActionWithGuiThreadInvoke(Ports, () =>
             {
                 ListenPortStr = Ports.Text;
             });
             Utils_.ActionWithGuiThreadInvoke(IP, () =>
             {
                 ListenIPStr = IP.Text;
             });
             Int32 Port         = Convert.ToInt32(ListenPortStr);
             TcpListener server = null;
             try
             {
                 IPAddress LocalAddr = IPAddress.Parse(ListenIPStr);
                 server = new TcpListener(LocalAddr, Port);
                 server.Start();// начинаем ожидание подсоединений клиентов .
                 Utils_.ActionWithGuiThreadInvoke(Comments, () =>
                 {
                     Comments.Text += "Waiting for a connection... ";
                 });
                 while (true)
                 {
                     TcpClient acceptedClient = server.AcceptTcpClient();
                     Utils_.ActionWithGuiThreadInvoke(Comments, () =>
                     {
                         Comments.Text += "\r\n Connected!";
                     });
                     Utils_.ActionInThread(() =>
                     {
                         ByteBuffer bytebuf   = new ByteBuffer();
                         byte[] tempBuffer    = new byte[4096];
                         TcpClient client     = acceptedClient;
                         NetworkStream stream = client.GetStream();
                         int i;
                         while ((i = stream.Read(tempBuffer, 0, tempBuffer.Length)) != 0)
                         {
                             bytebuf.Append(tempBuffer, i);
                             Mysignature_start = "<sended_sign>";
                             Mysignature_end   = "</sended_sign>";
                             if (IsReceiveNetworkDataComplete(bytebuf) == true)
                             {
                                 string bufStr = "";
                                 string ResStr = "";
                                 bufStr        = bytebuf.ToAnsiString(0, bytebuf.Length);
                                 ResStr        = Extract.Between(bufStr, Mysignature_start, Mysignature_end);
                                 WorkInMessage(ResStr);
                             }
                         }
                         client.Close();
                     });
                 }
             }
             catch (SocketException err)
             {
                 Utils_.ActionWithGuiThreadInvoke(Comments, () =>
                 {
                     Comments.Text = "\r\n failed:" + err.ToString();
                 });
             }
             finally
             {
                 server.Stop();
             }
         }
         catch (Exception exc)
         {
             Utils_.ActionWithGuiThreadInvoke(Comments, () =>
             {
                 Comments.Text += "\r\nAdress IP or Port is empty or incorrect..." + exc.ToString();
             });
             return;
         }
     });
 }
Ejemplo n.º 2
0
 public string ToAnsiString(int offset, int len)
 {
     return(Utils_.BytesToAnsiString(Read(offset, len)));
 }