Ejemplo n.º 1
0
        private Socket socket;// = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public void Run(MainWindow CMW, CancellationToken pBacklineCanselToken)
        {
            clientMainWindow    = CMW;
            hostName            = Dns.GetHostName();
            hostEntry           = Dns.GetHostEntry(hostName);
            serverIPLocalv4     = hostEntry.AddressList.Last().MapToIPv4();
            backlineCanselToken = pBacklineCanselToken;

            while (!connected && !backlineCanselToken.IsCancellationRequested)

            {
                Thread.Sleep(1);
            }
            clientMainWindow.Set_Sys_Mes("Connected");
            while (!backlineCanselToken.IsCancellationRequested)
            {
                try
                {
                    socket.BeginReceive(Databuffer, 0, Databuffer.Length, SocketFlags.None, RecieveCallback, null);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            clientMainWindow.Dispatcher.InvokeAsync(clientMainWindow.EndAction());
        }
Ejemplo n.º 2
0
 public void Dispose()
 {
     if (socket.Connected)
     {
         Span <byte> buffer = stackalloc byte[sizeof(char)];
         buffer = Encoding.ASCII.GetBytes("~");
         socket.Send(buffer.ToArray());
         socket.Shutdown(SocketShutdown.Both);
         socket.Close();
     }
     clientMainWindow.EndAction();
 }