Hello everyone,
i can't understand why this doesn't work.
I get the following exception, after in try to connect to the server.
"The underlying connection was closed. The server executed a HTTP protocol violation." (I translated that from german - so it could sound similar :)
after that everything network depending takes a 10 second break before it goes. For example when i try to browse to an internet site i have to wait 10 secs. the client of my remoting application also
needs 10 seconds, after that the exception comes up.
if i reboot the 10 seconds wait is gone.
here is my code:
//Server.cs
using System;
using System.Collections;
using System.Drawing;
using RemoteInterfaces;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
namespace Server
{
class RemoteGraphics : MarshalByRefObject, IServer
{
private ArrayList points;
private Hashtable clients;
public RemoteGraphics()
{
points = new ArrayList();
clients = new Hashtable();
}
public override object InitializeLifetimeService()
{
return null;
}
public void DrawLine(Point StartPoint, Point EndPoint)
{
Point [] point = new Point[2];
point[0] = StartPoint;
point[1] = EndPoint;
points.Add(point);
foreach (IClient client in clients.Values)
client.AddLine(StartPoint, EndPoint);
}
public void DrawLines(Pen Pen, Point [] Points)
{
points.Add(Points);
foreach (IClient client in clients.Values)
client.AddLines(Pen, Points);
}
public void AddClient(IClient Client)
{
this.clients.Add(Client.Guid, Client);
}
public void RemoveClient(IClient Client)
{
this.clients.Remove(Client.Guid);
}
}
public class Server
{
private RemoteGraphics _remote;
public Server()
{
BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider();
serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
BinaryClientFormatterSinkProvider clientProv = new BinaryClientFormatterSinkProvider();
IDictionary props = new Hashtable();
props["port"] = 2005;
TcpChannel tcpChannel = new TcpChannel(props, clientProv, serverProv);
ChannelServices.RegisterChannel(tcpChannel);
_remote = new RemoteGraphics();
RemotingServices.Marshal(_remote, "Whiteboard.soap");
}
[STAThread]
static void Main()
{
new Server();
Console.WriteLine("Server started.");
Console.ReadLine();
}
}
}
//Client.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using RemoteInterfaces;
using RemotingTools;
namespace Client
{
public class Client : System.Windows.Forms.Form, IClient
{
private System.ComponentModel.Container components = null;
private IServer _server;
private Guid _guid;
private ArrayList _allMousePoints = new ArrayList();
private Point [] _linePoints;
private bool _isLeftMousePressed;
public Client()
{
RemotingConfiguration.Configure(System.Environment.CurrentDirectory + "\\xml\\Client.exe.config");
_server = (IServer) RemotingHelper.GetObject(typeof(IServer));
_guid = Guid.NewGuid();
try
{
_server.AddClient((IClient) this);
}
catch(Exception ex)
{
foreach (IChannel iChannel in ChannelServices.RegisteredChannels)
ChannelServices.UnregisterChannel(iChannel);
}
InitializeComponent();
}
public Guid Guid
{
get { return _guid; }
}
protected override void OnMouseDown(MouseEventArgs mea)
{
if(mea.Button != MouseButtons.Left) return;
_linePoints = new Point [2];
_linePoints[0] = new Point(mea.X, mea.Y);
_isLeftMousePressed = true;
}
protected override void OnMouseUp(MouseEventArgs mea)
{
if(!_isLeftMousePressed) return;
_linePoints[1] = new Point(mea.X, mea.Y);
_allMousePoints.Add(_linePoints);
_isLeftMousePressed = false;
_server.DrawLine(_linePoints[0], _linePoints[1]);
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics graphics = pea.Graphics;
Pen pen = new Pen(ForeColor);
foreach (Point [] points in _allMousePoints)
graphics.DrawLine(
new Pen(ForeColor),
points[0],
points[1]);
}
public void AddLine(Point StartPoint, Point EndPoint)
{
Point [] point = new Point[2];
point[0] = StartPoint;
point[1] = EndPoint;
_allMousePoints.Add(point);
Graphics graphics = CreateGraphics();
graphics.DrawLine(new Pen(ForeColor), StartPoint, EndPoint);
Invalidate();
}
public void AddLines(Pen Pen, Point [] Points)
{
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// Client
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(352, 318);
this.Name = "Client";
this.Text = "Client";
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Client());
}
}
}
//RemoteInterfaces.cs
using System;
using System.Drawing;
using System.Collections;
namespace RemoteInterfaces
{
public interface IServer
{
void DrawLine(Point startPoint, Point endPoint);
void DrawLines(Pen Pen, Point [] points);
void AddClient(IClient Client);
void RemoveClient(IClient Client);
}
public interface IClient
{
Guid Guid { get; }
void AddLine(Point StartPoint, Point EndPoint);
void AddLines(Pen Pen, Point []Points);
}
}
what am i doing wrong?
thanks very much for your help!