Hello,
I´m currently developing an application which sends data to my Windows server.
I already wrote the following code (server-side)

namespace server
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Server s = new Server(3000);
            s.ExceptionReceived += new Server.ExceptionReceivedHandler(s_ExceptionReceived);
        }

        void s_ExceptionReceived(exception e)
        {
            MessageBox.Show(string.Format("DateTime: {0} - UserName: {1} - OperatingSystem: {2} - MachineHash: {3} - Title: {4} - StackTrace: {5}", e.DateTime, e.UserName, e.OperatingSystem, e.MachineHash, e.Title, e.StackTrace));
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            Environment.Exit(0);
        }
    }

    public class Server
    {
        private TcpListener listener;
        public Thread listenthread;
        public delegate void ExceptionReceivedHandler(exception e);
        public event ExceptionReceivedHandler ExceptionReceived;

        public Server(int port)
        {
            listener = new TcpListener(IPAddress.Any, port);
            listenthread = new Thread(new ThreadStart(ListenForClients));
            listenthread.Start();
        }

        private void ListenForClients()
        {
            listener.Start();
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                Thread clientthread = new Thread(new ParameterizedThreadStart(HandleClient));
                clientthread.Start(client);
            }
        }

        private void HandleClient(object tcpclient)
        {
            TcpClient client = (TcpClient)tcpclient;
            NetworkStream stream = client.GetStream();
            while (true)
            {
                try
                {
                    BinaryReader br = new BinaryReader(stream);
                    exception e = new exception(br.ReadBytes(client.Available));
                    ExceptionReceived(e);
                    br.Close();
                    stream.Close();
                    break;
                }
                catch
                {
                    // socket error
                    break;
                }
            }

            stream.Close();
            client.Client.Close();
        }
    }

    public class exception
    {
        public string DateTime;
        public string UserName;
        public string OperatingSystem;
        public string MachineHash;
        public string Title;
        public string StackTrace;

        public exception(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            BinaryReader br = new BinaryReader(ms);

            Int16 DateTimeLength = br.ReadByte();
            Int16 UserNameLength = br.ReadByte();
            Int16 OperatingSystemLength = br.ReadByte();
            Int16 MachineHashLength = br.ReadByte();
            Int16 TitleLength = br.ReadByte();
            Int16 StackTraceLength = br.ReadByte();

            DateTime = Encoding.UTF8.GetString(br.ReadBytes(DateTimeLength));
            UserName = Encoding.UTF8.GetString(br.ReadBytes(UserNameLength));
            OperatingSystem = Encoding.UTF8.GetString(br.ReadBytes(OperatingSystemLength));
            MachineHash = Encoding.UTF8.GetString(br.ReadBytes(MachineHashLength));
            Title = Encoding.UTF8.GetString(br.ReadBytes(TitleLength));
            StackTrace = Encoding.UTF8.GetString(br.ReadBytes(StackTraceLength));
        }
    }
}

When using this on my PC (localhost) it´s working flawlessy, but when I upload the server application to my Windows server I can send the data (exception) just once.
I mean just one MessageBox opens and shows the exception sent.
After this nothing more happens until I close both applications and try again.

Has someone a solution to this problem?
Thanks in advance,
Eric

P.S. client-side code following

namespace client
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            exception ex = new exception();
            ex.DateTime = DateTime.Now.ToString();
            ex.UserName = System.Windows.Forms.SystemInformation.UserName;
            ex.OperatingSystem = DeployLX.Licensing.v4.OSRecord.ThisMachine.Product.ToString();
            ex.MachineHash = DeployLX.Licensing.v4.MachineProfile.Profile.Hash;
            ex.Title = "Critical";
            ex.StackTrace = "StackTrace";
            SendException("localhost", 3000, ex);
        }

        private void SendException(string host, int port, exception e)
        {
            TcpClient client = new TcpClient();
            client.Connect(host, port);
            NetworkStream stream = client.GetStream();
            byte[] data = e.GetData();
            stream.Write(data, 0, data.Length);
            stream.Close();
            client.Client.Close();
        }
    }

    class exception
    {
        public string DateTime;
        public string UserName;
        public string OperatingSystem;
        public string MachineHash;
        public string Title;
        public string StackTrace;

        public byte[] GetData()
        {
            List<byte> result = new List<byte>();

            result.Add((byte)Convert.ToInt16(DateTime.Length));
            result.Add((byte)Convert.ToInt16(UserName.Length));
            result.Add((byte)Convert.ToInt16(OperatingSystem.Length));
            result.Add((byte)Convert.ToInt16(MachineHash.Length));
            result.Add((byte)Convert.ToInt16(Title.Length));
            result.Add((byte)Convert.ToInt16(StackTrace.Length));

            string[] strings = new string[6] { DateTime, UserName, OperatingSystem, MachineHash, Title, StackTrace };
            foreach (string s in strings)
            {
                foreach (byte b in Encoding.UTF8.GetBytes(s))
                    result.Add(b);
            }

            return result.ToArray();
        }
    }
}

Hey man, the whole thing looks a bit suspicious to me.
First you fire messagebox without testing InvokeRequired,
second you close at least twice stream,
finally - why on the God's green Earth you raise the exception ?

while (true)
            {
                try
                {
                    BinaryReader br = new BinaryReader(stream);
                    exception e = new exception(br.ReadBytes(client.Available));
                    ExceptionReceived(e); //???
                    br.Close();
                    stream.Close(); // 1st
                    break;
                }
                catch
                {
                    // socket error
                    break;
                }
            }
 
            stream.Close(); // 2nd

The exception mentioned in the code is not the System.Exception class, it´s a custom class created by me. If an exception occures at the client side the client application creates the custom class containing username, operating system and so on, then the custom exception gets sent to the server.

However, I solved the problem myself using following code on server-side:

namespace server
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Server s = new Server(3000);
            s.ExceptionReceived += new Server.ExceptionReceivedHandler(s_ExceptionReceived);
        }

        void s_ExceptionReceived(exception e)
        {
            MessageBox.Show(string.Format("DateTime: {0} - UserName: {1} - OperatingSystem: {2} - MachineHash: {3} - Title: {4} - StackTrace: {5}", e.DateTime, e.UserName, e.OperatingSystem, e.MachineHash, e.Title, e.StackTrace));
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            Environment.Exit(0);
        }
    }

    public class Server
    {
        private TcpListener listener;
        public Thread listenthread;
        public delegate void ExceptionReceivedHandler(exception e);
        public event ExceptionReceivedHandler ExceptionReceived;

        public Server(int port)
        {
            listener = new TcpListener(IPAddress.Any, port);
            listenthread = new Thread(new ThreadStart(ListenForClients));
            listenthread.Start();
        }

        private void ListenForClients()
        {
            listener.Start();
            while (true)
            {
                TcpClient client = listener.AcceptTcpClient();
                Thread clientthread = new Thread(new ParameterizedThreadStart(HandleClient));
                clientthread.Start(client);
            }
        }

        private void HandleClient(object tcpclient)
        {
            TcpClient client = (TcpClient)tcpclient;
            NetworkStream stream = client.GetStream();
            while (true)
            {
                try
                {
                    if (stream.DataAvailable && client.Available != 0)
                    {
                        BinaryReader br = new BinaryReader(stream);
                        exception e = new exception(br.ReadBytes(client.Available));
                        ExceptionReceived(e);
                        br.Close();
                    }
                }
                catch
                {
                    // socket error
                    break;
                }
            }
        }
    }

    public class exception
    {
        public string DateTime;
        public string UserName;
        public string OperatingSystem;
        public string MachineHash;
        public string Title;
        public string StackTrace;

        public exception(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            BinaryReader br = new BinaryReader(ms);

            Int16 DateTimeLength = br.ReadByte();
            Int16 UserNameLength = br.ReadByte();
            Int16 OperatingSystemLength = br.ReadByte();
            Int16 MachineHashLength = br.ReadByte();
            Int16 TitleLength = br.ReadByte();
            Int16 StackTraceLength = br.ReadByte();

            DateTime = Encoding.UTF8.GetString(br.ReadBytes(DateTimeLength));
            UserName = Encoding.UTF8.GetString(br.ReadBytes(UserNameLength));
            OperatingSystem = Encoding.UTF8.GetString(br.ReadBytes(OperatingSystemLength));
            MachineHash = Encoding.UTF8.GetString(br.ReadBytes(MachineHashLength));
            Title = Encoding.UTF8.GetString(br.ReadBytes(TitleLength));
            StackTrace = Encoding.UTF8.GetString(br.ReadBytes(StackTraceLength));
        }
    }
}

... and following code client-site

namespace client
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            exception ex = new exception();
            ex.DateTime = DateTime.Now.ToString();
            ex.UserName = System.Windows.Forms.SystemInformation.UserName;
            ex.OperatingSystem = DeployLX.Licensing.v4.OSRecord.ThisMachine.Product.ToString();
            ex.MachineHash = DeployLX.Licensing.v4.MachineProfile.Profile.Hash;
            ex.Title = "Critical";
            ex.StackTrace = "StackTrace";
            SendException("eric.sh", 3000, ex);
        }

        private void SendException(string host, int port, exception e)
        {
            TcpClient client = new TcpClient();
            client.Connect(host, port);
            NetworkStream stream = client.GetStream();
            byte[] data = e.GetData();
            stream.Write(data, 0, data.Length);
            stream.Close();
            client.Client.Close();
        }
    }

    class exception
    {
        public string DateTime;
        public string UserName;
        public string OperatingSystem;
        public string MachineHash;
        public string Title;
        public string StackTrace;

        public byte[] GetData()
        {
            List<byte> result = new List<byte>();

            result.Add((byte)Convert.ToInt16(DateTime.Length));
            result.Add((byte)Convert.ToInt16(UserName.Length));
            result.Add((byte)Convert.ToInt16(OperatingSystem.Length));
            result.Add((byte)Convert.ToInt16(MachineHash.Length));
            result.Add((byte)Convert.ToInt16(Title.Length));
            result.Add((byte)Convert.ToInt16(StackTrace.Length));

            string[] strings = new string[6] { DateTime, UserName, OperatingSystem, MachineHash, Title, StackTrace };
            foreach (string s in strings)
            {
                foreach (byte b in Encoding.UTF8.GetBytes(s))
                    result.Add(b);
            }

            return result.ToArray();
        }
    }
}

Thanks anyway,
Eric

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.