Solved

Vb.net remoting with interface

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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