Solved

Vb.net remoting with interface

Posted on 2006-06-25
5
345 Views
Last Modified: 2010-04-23
Hi, i am having alot of problems with remoting in .net. What i am trying to achieve here is to, build a interface class so the client side will reference to it and call the methods from the server side. Can this be done without compiling the source to dll? I guess i am abit lost with the many different ways of using remoting in .net. I would appreciate if someone could write me a small sample remoting app which consists of a IhelloWorld interface, hellowWorldCls the host and client to use the 2. I have searched the web, most samples dont work or they are either to complex for beginner's understanding. The simplest sample i found was at

http://samples.gotdotnet.com/QuickStart/howto/default.aspx?url=/quickstart/howto/doc/Remoting/firstserver.aspx

but it appears that it won't run correctly, a null value execptions would appear. Would anyone take a look at the sample and post what are the modifications needed to run the app.

Your help would be very much appreciated.

thank you.
0
Comment
Question by:jaxrpc
5 Comments
 
LVL 6

Accepted Solution

by:
Nandakumar Sakthivel earned 250 total points
ID: 16981728
0
 
LVL 5

Assisted Solution

by:t_itanium
t_itanium earned 250 total points
ID: 16983314
i will give you the code in c#..try to convert it else tell me..

to use remoting you can do:::


first create a project and name it BusinesssObject for example..
add to it a class name it Clas..

here is the code for it:

using System;
using System.IO;
using System.Collections;

namespace BusinessObject
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      public class Clas : System.MarshalByRefObject
      {
            public bool checking=false;
            public Clas()
            {
                  //
                  // TODO: Add constructor logic here
                  //
            }


            public bool Check()
            {
                  if(checking==true)
                  {
                        return true;
                  }
                  else
                  {
                        return false;
                  }
            }
      }
}

in this class i created a checking function that returns true or false...
build the project..

in the same solution create a project name it host
add reference to the host project..(right click references and click add reference choose projects tab and choose BusinessObject project and addit..

also add refernce to System.Runtime.Remoting from the .NET tab

then add to this project a class name it class

here is the code:

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using BusinessObject;
using System.IO;
using System.Collections;

namespace Host
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            //[STAThread]
            static void Main(string[] args)
            {
                  //
                  // TODO: Add code to start application here
                  //
                  Console.WriteLine("Host: started");
                  TcpChannel tcp =new TcpChannel(7777);
                  ChannelServices.RegisterChannel(tcp);
                  Clas s = new Clas();
                  RemotingServices.Marshal(s, "Clas");
                  string tst=Console.ReadLine();
                  while(tst.ToLower()!="exit")
                  {
                        s.checking=true;
                        tst=Console.ReadLine();
                        s.checking=false;
                  }
            
                  
            }
      }
}

then create another third project name it client: add also a reference to BusinessObject and add refernce to System.Runtime.Remoting

create a form name it form1 code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;
using BusinessObject;
using System.IO;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Drawing.Drawing2D;

namespace Client
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            const string URL = "tcp://localhost:7777/Clas";

            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Label label1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.button1 = new System.Windows.Forms.Button();
                  this.label1 = new System.Windows.Forms.Label();
                  this.SuspendLayout();
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(240, 120);
                  this.button1.Name = "button1";
                  this.button1.TabIndex = 0;
                  this.button1.Text = "button1";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // label1
                  //
                  this.label1.Location = new System.Drawing.Point(208, 48);
                  this.label1.Name = "label1";
                  this.label1.TabIndex = 1;
                  this.label1.Text = "label1";
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(608, 286);
                  this.Controls.Add(this.label1);
                  this.Controls.Add(this.button1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  Clas g =new Clas();
                  g = (Clas)RemotingServices.Connect(typeof(Clas), URL);
                  if(!g.Check())
                  {      
                        label1.Text="Printed";                
                  }
                  else
                  {
                        MessageBox.Show("Job Stopped");
                        Application.Exit();
                  }
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
            
            }

      

      
      }
}

the idea of this simple program.. is that the hot will start...then   wait for any key...the client then starts...it will allow any press on the button untill the key is pressed on the host...

when a key is pressed on the host the value of checking will be true and thus the checking will return true...

to achieve that you will have first run the host from the .exe application from the bin then run the client..

concentrate on this :::::


in the BusinessObject class you should inherit : System.MarshalByRefObject

in the host class you should have:

TcpChannel tcp =new TcpChannel(7777);//7777 specify the port the client will use
ChannelServices.RegisterChannel(tcp);
Clas s = new Clas();
RemotingServices.Marshal(s, "Clas");

and in the client:

you should have:

const string URL = "tcp://localhost:7777/Clas"; // or the ip of the computer instead localhost

Clas g =new Clas();
g = (Clas)RemotingServices.Connect(typeof(Clas), URL);
g.Check();//then you can use any function from the business object..


cheers





0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb.net checkbox 7 47
Ajax Panel Not Updating When Triggered By Timer 4 73
Help with LINQ and XML 10 36
VB.net Progress Bar - Maximum Value too large 2 12
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question