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

Create a new Class library project in Visual Studio .NET, set the namespace to p2pfirewall and the class as tunnel. Since the class will be remoted, we need to derive the tunnel class from System.MarshallByRef Object.

The post methods (postRequest, postResponse) are fairly straightforward. An SQL query is constructed that inserts a new row into the corresponding table, and then executed.

The check methods are slightly more complex. checkRequests for instance, pulls the first pending request from the database that is marked for the attention of the peer making the call. That request is then deleted from the database. checkResponses is similar, where the first row from the responses table that have come from the requested peer, and contains the correct file is returned.

C#

using System;

using System.Data.OleDb;

namespace p2pfirewall

{

    public class tunnel : System.MarshalByRefObject

    {

            private string DSN = "Provider=Microsoft.Jet.OLEDB.4.0;" +

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

            public void postRequest(string requestingPeer, string filename,

                                      string respondingPeer)

            {                      

                    string SQL;

                    SQL = "insert into requests";

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

                    SQL += "'" + requestingPeer + "',";

                    SQL += "'" + filename + "',";

                    SQL += "'" + respondingPeer + "')";

                    nonQuery(SQL,DSN);

            }

            public void postResponse(string respondingPeer, string filename,

                                     string fileContents,string requestingPeer)

            {

                    string SQL;

                    SQL = "insert into responses ";

                    SQL += " (requester,fileContents,filename,respondent) ";

                    SQL += " values (";

                    SQL += "'" + requestingPeer + "',";

                    SQL += "'" + fileContents + "',";

                    SQL += "'" + filename + "',";

                    SQL += "'" + respondingPeer + "')";

                    nonQuery(SQL,DSN);

            }

            public string checkRequests(string peer)

            {

                    string SQL;

                    string filename;

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

                    SQL += " respondent='" + peer + "'";

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

                    // don't return the same request twice.

                    SQL = "delete from requests where ";

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

                    SQL += " filename='" + filename + "'";

                    nonQuery(SQL,DSN);

                    return filename;

            }

            public string checkResponse(string respondingPeer,

                                          string filename)

            {

                    string SQL;

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

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

                    SQL += " filename='" + filename + "'";

                    return Query("fileContents",SQL,DSN);

            }

           

    }

}

Page 1  Page 3 



Google

Copyright 2017 Infinite Loop Ltd.