VB.NET
Private
Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim channel
As HttpChannel = New
HttpChannel(8085)
ChannelServices.RegisterChannel(channel)
RemotingConfiguration.RegisterWellKnownServiceType(
_
(new
p2pfirewall.tunnel).GetType(), _
"Firewall_Tunnel",
_
WellKnownObjectMode.Singleton)
End Sub
Remoting code, as show above is comprehensively described
in the web services chapter. Going through the code line by line, it can be
seen that the first task to register a channel over which the remote client
will communicate with this object. In this case port number 8085 was chosen
arbitrarily, and the HTTP protocol is being used.
The remote object is then registered. Its type (or
interface) must be previously known on the client. In this case, our object
is p2pfirewall.tunnel.
It is defined as a singleton. This means that it only ever instantiates one
copy of the remote object, regardless of the number of clients.
You will need to add a reference to System.Runtime.Remoting
and the DLL you have just created in project > add references. Then add
the corresponding namespaces.
C#
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
VB.NET
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Http
Now, to implement the peer, start a new Windows forms
Visual Studio .NET project. Add four textboxes and a button, their names
should be tbRequestFrom,
tbFilename,
tbStatus,
tbName
and btnRequest
respectively. tbStatus should be set with multiline equal to true.
Then add two timers to the form, tmrCheckRequests and tmrCheckResponses.
You will need to add a reference to System.Runtime.Remoting
and the DLL you have just created in project > add references. Then add
the corresponding namespaces.
Click on the form, and add the following code
C#
p2pfirewall.tunnel Tunnel;
string peerName = "";
private void Form1_Load(object sender,
System.EventArgs e)
{
peerName
= "PEER#" + (new Random()).NextDouble();
tbName.Text
+= peerName;
//
Change http://localhost to the IP of the firewall tunnelling proxy.
Tunnel
=
(p2pfirewall.tunnel)Activator.GetObject(
typeof(p2pfirewall.tunnel),
"http://localhost:8085/Firewall_Tunnel");
if
(Tunnel==null) MessageBox.Show("cannot locate server");
}
VB.NET
Dim Tunnel As p2pfirewall.tunnel
Dim peerName As String = ""
Private
Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) peerName = "PEER#" & (New
Random).NextDouble()
tbName.Text += peerName
'
Change http://localhost to the IP of the firewall tunnelling proxy.
Tunnel = CType(Activator.GetObject( _
(New p2pfirewall.tunnel).GetType(), _
"http://localhost:8085/Firewall_Tunnel"), _
p2pfirewall.tunnel)
If
Tunnel Is Nothing Then MessageBox.Show("cannot locate server")
End Sub
When the client application first loads, it must first
generate a unique name for itself. In this case the identifier is simply the
name PEER# followed by a random number. The peerName variable is used
to differentiate peers on the network. IP addresses cannot be used because it
is possible for two computers on different stub networks to have the same IP
address. In production environments a genuinely unique identifier (GUID)
should be used. The next stage in the process is to obtain a reference to the
remote object on the server. This is achieved though the Activator.GetObject
method. The activator is passed the URL, and port of the remote object.
Note the use of http://localhost in the code
listed above. This should be changed to the public IP address of the server.
To send a request to file from a peer, we have to post a
request on the server. This is achieved by calling the postRequest method. At
any time after calling this method, we can expect a response to be posted to
the server by a peer fulfilling the request. There is no way of predicting
when, or if, a response will be posted, so we use a timer to poll the
response-checking algorithm.
Page 4
Page 6