Solved

Vb.net remoting with interface

Posted on 2006-06-25
5
336 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now