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

VB.NET

Imports System

Imports System.Data.OleDb

Namespace p2pfirewall

Public Class tunnel

Inherits System.MarshalByRefObject

Dim DSN as string = "Provider=Microsoft.Jet.OLEDB.4.0;" + _

†††††††††††††††††††††† "Data Source=C:\filetransfers.mdb";

PublicSub postRequest(ByVal requestingPeer As String, ByVal _

†††††††††††††††††††† filename As String, ByVal respondingPeer As String)†††† Dim SQL As String

††† SQL = "insert into requests "

††† SQL = SQL +"(requester,filename,respondent) values ("

††† SQL = SQL + "'" + requestingPeer + "',"

††† SQL = SQL + "'" + filename + "',"

††† SQL = SQL + "'" + respondingPeer + "')"

††† nonQuery(SQL,DSN)

†† End Sub

†† PublicSub postResponse(ByVal respondingPeer As String, _

††† ByVal filename As String, ByVal fileContents As String, _

††† ByVal requestingPeer As String)

†††† Dim SQL As String

††††† SQL = "insert into responses "

††††† SQL = SQL +"((requester,fileContents,filename,respondent) values ("

††† SQL = SQL +"'" + requestingPeer + "',"

††††† SQL = SQL +"'" + fileContents + "',"

††††† SQL = SQL +"'" + filename + "',"

††† SQL = SQL +"'" + respondingPeer + "')"

††† nonQuery(SQL,DSN)

††† End Sub

†††

††† Public Function checkRequests(ByVal peer As String) As String

††† Dim SQL As String

††† Dim filename As String

††† SQL = "select top 1 * from requests where "

††† SQL = SQL +" respondent='" + peer + "'"

††† filename = Query("filename",SQL,DSN)

††† ' don't return the same request twice.

†††

††††† SQL = "delete from requests where "

††† †† SQL = SQL +" respondent='" + peer + "' and "

††† †† SQL = SQL +" filename='" + filename + "'"

††† †† nonQuery(SQL,DSN)

††† †† Return filename

††† End Function

†††

††† Public Function checkResponse(ByVal respondingPeer As String, _

††††††††††††††††††††††††††††† ByVal filename As String) As String

†††† Dim SQL As String

†††† SQL = "select top 1 * from responses where "

†††† SQL = SQL + " respondent='" + respondingPeer + "' and "

†††† SQL = SQL + " filename='" + filename + "'"

†††† Return Query("fileContents",SQL,DSN)

††† End Function

††† End Class

End Namespace

This class will be used in remoting, so therefore it must inherit from System.MarshalByRefObject. More information on this can be seen in the web services chapter. The public methods will be exposed to the client, but not the private methods and properties. The private string variable DSN is used to determine the location and type of the local database. This information is not relevant to the remote client.

Going through the functions one by one, letís look at postRequest first. What the function does is it adds a new row into the request table, populating the requester, filename and respondent fields. It calls the nonQuery function, which as we shall see later is used to run statements against the database that do not return result sets.

PostResponse is quite similar to PostRequest in that it adds a new row into the response table, populating the requester, fileContents, filename, and respondent fields. Once again, it calls the nonQuery function to process the statement.

CheckRequests performs two functions. Firstly it retrieves the last row in the requests table that pertains to a particular specified peer. It does this by executing a select statement against the requests table in the database, filtering by respondent. The second task performed by this function is to remove rows in the requests table pertaining to a particular peer. This prevents requests been handled more than once. This action is performed by the nonQuery function processing a SQL delete statement.

To complete the class, we need to implement the Query and nonQuery methods. These are taken from chapter 2, with some modification to the Query method, such that it returns a string that corresponds to the specified field as returned in the first row of data.

The fourth function, checkResponse reads the topmost row from the responses table that pertains to the specified peer. It returns the filecCntents field to the calling function.

Page 2  Page 4 



Google

Copyright 2017 Infinite Loop Ltd.