Click here to Skip to main content
15,881,561 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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();
        }
    }
   
  
}
Posted
Updated 30-Dec-13 2:16am
v4
Comments
Sergey Alexandrovich Kryukov 30-Dec-13 2:59am    
Your time says "Forms", but the code is WPF.
The problem is not explained. Anyway, working by timer looks like a bad idea. Better use threads.
You mix up WPF and network events/transport, this is very bad.
—SA
Sergey Alexandrovich Kryukov 30-Dec-13 3:01am    
You are welcome.
I added some more points...
—SA
Richard MacCutchan 30-Dec-13 9:24am    
Why do you want to delete it? The question has received some suggested solutions so should remain for others' benefit.

1 solution

We can't do that.
Think about it: it's your software, written to your specification. We don't know what it is supposed to do, so we can't say "this is a bug" because we don't have any idea what is a correct output, or even a correct input!
We don't have access to the rest of your code let alone your data, so we can't even run it and see if anything happens!

But... I have so say that a very quick glance sees maintenance problems, what with hard coded paths, magic numbers, redundant code commented out, and a complete lack of any documentation, combined with coffee that isn't particularly self documenting.
 
Share this answer
 
Comments
BillWoodruff 30-Dec-13 2:44am    
"combined with coffee that isn't particularly self documenting." I think I use that brand :)
OriginalGriff 30-Dec-13 3:49am    
Elephanting auto correct! :laugh:
It does that every time - I swipe "code" is says "coffee"...I think Google is trying to tell me something!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900