Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
Available now!
Buy at Amazon US or
Buy at Amazon UK



Articles

» Windows API reference
» Webcam streaming in VB.NET
» Remoting with firewalls
» RSA from first principles
» Key & MouseLogger in .NET
» Networking Resource Kit for .NET
» Migrating VB6 Winsock to VB.NET
» Migrating C++ sockets to C#
» RFC Reference guide
» COM Reference guide
» WMI Reference guide
» SQL stored procedures
» TCP & UDP port reference
» NET Framework reference
» Ethernet Type codes
» MAC address assignments
» DLL entry point reference
» Boost SQL performance
» Free SMS UK
» Free SMS Ireland
» Free SMS South Africa
» Internet Explorer

Contact us

Click on the request button and add the following code

C#

private void btnRequest_Click(object sender, System.EventArgs e)

{

    Tunnel.postRequest(peerName,tbFilename.Text,tbRequestFrom.Text);

    tmrCheckResponses.Enabled = true;

}

VB.NET

Private  Sub btnRequest_Click(ByVal sender As Object, ByVal e As _ System.EventArgs)

    Tunnel.postRequest(peerName,tbFilename.Text,tbRequestFrom.Text)

    tmrCheckResponses.Enabled = True

End Sub

The response-checking algorithm is quite simple. A remote call is made to checkResponse, with the details of the file expected. If there is no response, this call returns null, otherwise the file contents are displayed on-screen. Once the task is complete, the timer disables itself.

Click on the response timer, and add the following code.

C#

private void tmrCheckResponses_Tick(object sender, System.EventArgs e)

{

    string fileContents = null;

    fileContents = Tunnel.checkResponse(tbRequestFrom.Text,tbFilename.Text);

    if (fileContents == null) return;

    tbStatus.Text = fileContents;

    tmrCheckResponses.Enabled = false;

}

VB.NET

Private  Sub tmrCheckResponses_Tick(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim fileContents As String =  Nothing

    fileContents = Tunnel.checkResponse(tbRequestFrom.Text,tbFilename.Text)

    If fileContents Is Nothing Then

             Return

    End If

    tbStatus.Text = fileContents

    tmrCheckResponses.Enabled = False

End Sub

By issuing requests, we provoke responses from peers, however, requests from other peers can arrive at any moment, for this reason, a free-running timer tmrCheckRequests polls the server for new requests by calling the remote procedure checkRequests. If there are no requests, null is returned otherwise, the requested file will be read from disk and a response will be posted, with the file contents and file name.

Click on the request timer, and add the following code

C#

private void tmrCheckRequests_Tick(object sender, System.EventArgs e)

{

    string requestedFile = null;

    string fileContents = "";

    requestedFile = Tunnel.checkRequests(peerName);

    if (requestedFile==null) return;

    fileContents = (new StreamReader(requestedFile)).ReadToEnd();

    Tunnel.postResponse(peerName,requestedFile,fileContents,"");

}

VB.NET

Private  Sub tmrCheckRequests_Tick(ByVal sender As Object, ByVal e As _ System.EventArgs)

    Dim requestedFile As String =  Nothing

    Dim fileContents As String =  ""

    requestedFile = Tunnel.checkRequests(peerName)

    if requestedFile=Nothing then return

    fileContents = (New StreamReader(requestedFile)).ReadToEnd()

    Tunnel.postResponse(peerName,requestedFile,fileContents,"")

End Sub

The tmrCheckRequests_Tick event occurs periodically, and serves to execute code that makes a query against the remote object to determine if there are any new rows in the request table that correspond to this peer’s name. If there were requests made, then the name of the file requested is returned from the checkRequests method on the remote object.

Should another peer request a file, it is read from the local disk by passing the filename as a parameter to a StreamReader, and then reading to the end of the stream. The contents of the file are then posted back to the remote object.

Page 5  Page 7 



Google

Copyright 2017 Infinite Loop Ltd.