using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Net; using System.Net.Sockets; using System.Threading; using System.Security; using System.Runtime.InteropServices; using System.Net.NetworkInformation; using System.IO; namespace WpfApplication4 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public void DoThisAllTheTime() { System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Stop(); TcpClient tcpClient = new TcpClient(); try { IPAddress ipaddr = IPAddress.Parse("xxx.xxx.x.xxx"); int port = yyyy; IPEndPoint ipEndpt = new IPEndPoint(ipaddr, port); tcpClient.Connect(ipEndpt); } catch { } NetworkStream netStream = tcpClient.GetStream(); if (netStream.CanWrite) { Byte[] sendBytes = Encoding.ASCII.GetBytes("username"); netStream.Write(sendBytes, 0, sendBytes.Length); Byte[] sendBytes1 = Encoding.ASCII.GetBytes("password" + "\r\n"); netStream.Write(sendBytes1, 0, sendBytes.Length); } else { MessageBox.Show("You cannot write data to this stream."); tcpClient.Close(); // Closing the tcpClient instance does not close the network stream. netStream.Close(); return; } Int32 bytes = 0; byte[] data = new byte[5100]; Thread.Sleep(1000); if (netStream.CanRead && netStream.DataAvailable) { var sb = new StringBuilder(); // Thread.Sleep(1000); bytes = netStream.Read(data, 0, data.Length); string s = Encoding.ASCII.GetString(data, 0, bytes); sb.Append(s); richTextBox1.AppendText(sb.ToString()); string subPath = "E:/here/output.txt"; string subPath1 = "E:/here/"; if (!Directory.Exists(subPath1)) { Directory.CreateDirectory(subPath1); } if (!File.Exists(subPath)) { File.Create(subPath); } System.IO.StreamWriter writer = new System.IO.StreamWriter(subPath); writer.Write(s); writer.Close(); writer.Dispose(); } else { MessageBox.Show("You cannot read data from this stream and/or there is no data to read"); tcpClient.Close(); // Closing the tcpClient instance does not close the network stream. netStream.Close(); return; } netStream.Close(); // System.Windows.Threading.DispatcherTimer dispatcherTimer1 = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 5); dispatcherTimer.Start(); } private void dispatcherTimer_Tick(object sender, EventArgs e) { DoThisAllTheTime(); } private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer(); dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick); dispatcherTimer.Interval = new TimeSpan(0, 0, 5); dispatcherTimer.Start(); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)