|
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Serialization.Formatters.Binary;
namespace RemotingServerClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ServerTalk _ServerTalk = null; // this object lives on the server
private CallbackSink _CallbackSink = null; // this object lives here on the client
private void btnRegister_Click(object sender, EventArgs e)
{
// creates a client object that 'lives' here on the client.
_CallbackSink = new CallbackSink();
// hook into the event exposed on the Sink object so we can transfer a server
// message through to this class.
_CallbackSink.OnHostToClient += new delCommsInfo(CallbackSink_OnHostToClient);
// Register a client channel so the server can communicate back - it needs a channel
// opened for the callback to the CallbackSink object that is anchored on the client!
HttpChannel channel = new HttpChannel(9003);
ChannelServices.RegisterChannel(channel, false);
// now create a transparent proxy to the server component
object obj = Activator.GetObject(typeof(ServerTalk), "http://marcelxp:9000/TalkIsGood");
// cast returned object
_ServerTalk = (ServerTalk)obj;
// Register ourselves to the server with a callback to the client sink.
_ServerTalk.RegisterHostToClient(this.txtUserID.Text, new delCommsInfo(_CallbackSink.HandleToClient));
// make sure we can't register again!
btnRegister.Enabled = false;
}
void CallbackSink_OnHostToClient(CommsInfo info)
{
if (this.txtFromServer.InvokeRequired)
this.txtFromServer.Invoke(new delCommsInfo(CallbackSink_OnHostToClient), new object[] { info });
else
this.txtFromServer.Text = "From server: " + info.Message + Environment.NewLine + this.txtFromServer.Text;
}
private void btnSend_Click(object sender, EventArgs e)
{
_ServerTalk.SendMessageToServer(new CommsInfo(this.txtToServer.Text));
this.txtFromServer.Text = "To server: " + this.txtToServer.Text + Environment.NewLine + this.txtFromServer.Text;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Ever since my dad bought me a Commodore 64 (some years back) I have been hooked to programming. For most of my working career I have worked intensely with C# (WinForms and WPF) but a few years back I started to investigate browser technologies and frameworks which are much more powerful than I thought at first!
I studied International Marketing but found the IT sector much more challenging. So straight from uni I took on some IT contract work in London in a buoyant market and never looked back.
If you wish to contact me then please do so on heeremans.marcel@gmail.com