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();
}
}
}