Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How- To Remoting – Client Server Using VB.Net (Part 1)

Author: DevASP
Download Source Code : 542_Client.zip

This Article is designed to show how to use the .NET Remoting architecture using three types of remote objects. This topic is divided into three parts and This Article is the Part One.

This Article is designed to show how to use the .NET Remoting architecture using three types of remote objects. In addition, it uses XML configuration files to specify available objects and server location information instead of hard-coding them into the binaries or putting the data into the system registry.

Featured Highlights

The Source Code contains three solutions:

  • VB.NET How-To Remoting - Server
  • VB.NET How-To Remoting - Host
  • VB.NET How-To Remoting - Client

Each solution has one project.

One of the projects (RemoteCustomer) is a class library that exposes three creatable types:

  • Customer (client activated)
  • SingleCallCustomer (WellKnown singlecall)
  • SingletonCustomer (WellKnown singleton)

To make it easier to configure, each class lives in the same namespace, RemotingSample, which is defined in each class source file.

The Client project is  a Windows Forms application. It relies upon a configuration file named Client.exe.config to know where to look for the remote objects.

<System.Diagnostics.DebuggerStepThrough()> has been added to some procedures since they are not the focus of the demo. Remove them if you wish to debug the procedures. This code simply shows the About form.

 

#Region " Standard Menu Code "

<System.Diagnostics.DebuggerStepThrough()> Private Sub mnuAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAbout.Click

End Sub

' This code will close the form.

<System.Diagnostics.DebuggerStepThrough()> Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click

' Close the current form

Me.Close()

End Sub

#End Region

 

To be used to obtain a reference to a Client Activated type. This is similar to DCOM style programming.

 

Private mCustomer As Customer

 

This is to be used when we want multiple clients to connect to the same object instance on the server.

 

Private mSCustomer As SingletonCustomer

 

Now write the routine below.This routine infact ' Ensable the create command button Disable other command buttons Set the radio buttons.

 

Private Sub SetCtlState(ByVal NewState As Boolean)

            Me.cmdCreate.Enabled = NewState

 

            Me.cmdGet.Enabled = (Not NewState)

            Me.cmdUpdate.Enabled = (Not NewState)

            Me.cmdUpdateAndGet.Enabled = (Not NewState)

            Me.cmdDebugData.Enabled = (Not NewState)

            Me.cmdRelease.Enabled = (Not NewState)

Me.rbClient.Enabled = NewState

            Me.rbSingle.Enabled = NewState

End Sub

This Routine clear all items from the listbox.

Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As

System.EventArgs) Handles cmdClear.Click

                        Me.lstResponses.Items.Clear()

End Sub

This routine Helps to Used in the catch handlers.Use Client Activated Type Notice that in this call to New you can pass parameters to the constructor.This type of object is very similar to a DCOM style object.

Use Server Activated Type Notice that you can not pass values to a constructor in this case. this is becuase the server creates the instance and makes it available to all as a singleton.  All we are doing is obtaining a reference to the running instance via a proxy. It also Change the command buttons state

 

Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click

      Dim txt As String Try

            Dim objNotCreated As Boolean = False

            If Me.rbClient.Checked = True Then

      mCustomer = New Customer(Me.lblDefNameValue.Text, CByte(Me.lblDefAgeValue.Text))

            objNotCreated = (mCustomer Is Nothing)

      Else

      Dim args() As Object

      mSCustomer = CType(Activator.CreateInstance(GetType(RemotingSample.SingletonCustomer), args), RemotingSample.SingletonCustomer)

                        mSCustomer.NewClient()

                        objNotCreated = (mSCustomer Is Nothing)

                  End If

 

                  SetCtlState(objNotCreated)

 

            Catch exp As RemotingException

                  txt = "I was unable to access the remote customer object." & vbCrLf & vbCrLf & _

                    "Detailed Error Information below:" & vbCrLf & vbCrLf & _

                    "  Message: " & exp.Message & vbCrLf & _

                    "  Source: " & exp.Source & vbCrLf & vbCrLf & _

                    "  Stack Trace:" & vbCrLf & _

                    exp.StackTrace()

 

                  MessageBox.Show(txt, "Remoting Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

Catch exp As System.Net.Sockets.SocketException

txt = "I was unable to access the remote customer object." & vbCrLf & _

                    "Is it possible the server is not running?" & vbCrLf & vbCrLf & _

                    "Detailed Error Information below:" & vbCrLf & vbCrLf & _

                    "  Message: " & exp.Message & vbCrLf & _

                    "  Source: " & exp.Source & vbCrLf & _

                    "  Error Code: " & exp.ErrorCode.ToString() & vbCrLf & _

                    "  Native Error Code: " & exp.NativeErrorCode.ToString() & vbCrLf & vbCrLf & _

                    "  Stack Trace:" & vbCrLf & _

                    exp.StackTrace

 

                  MessageBox.Show(txt, "Socket Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

            Catch exp As Exception

                  ' Will catch any generic exception

                  txt = "I was unable to access the remote customer object." & vbCrLf & vbCrLf & _

                    "Detailed Error Information below:" & vbCrLf & vbCrLf & _

                    "  Message: " & exp.Message & vbCrLf & _

                    "  Source: " & exp.Source & vbCrLf & vbCrLf & _

                    "  Stack Trace:" & vbCrLf & _

                    exp.StackTrace

 

                  MessageBox.Show(txt, "Generic Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)

End Try

End Sub

This Routine Update the values on the server using a function that accepts both values and get the return data of the Client Activated type. This is generally a better format the code in cmdUpdate since it gets more work done in fewer round-trips.

Private Sub cmdUpdateAndGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdateAndGet.Click

            If ((Not mCustomer Is Nothing) Or (Not mSCustomer Is Nothing)) Then

                  Try

                        If Me.rbClient.Checked = True Then

                              With Me.lstResponses.Items

                                    .Add(mCustomer.UpdateCustomerInfo(Me.txtNewName.Text, CByte(Me.txtNewAge.Text)))

                              End With

                        Else

                              With Me.lstResponses.Items

                                    .Add(mSCustomer.UpdateCustomerInfo(Me.txtNewName.Text, CByte(Me.txtNewAge.Text)))

                              End With

                        End If

            Catch exp As Exception

                        Dim txt As String

                        txt = "I was unable to access the remote customer object." & vbCrLf & vbCrLf & _

"Detailed Error Information below:" & vbCrLf & vbCrLf & _

"  Message: " & exp.Message & vbCrLf & _

 "  Source: " & exp.Source & vbCrLf & vbCrLf & _

"  Stack Trace:" & vbCrLf & exp.StackTrace

MessageBox.Show(txt, "Generic Exception", MessageBoxButtons.OK, MessageBoxIcon.Stop)

End Try

End If

End Sub

Instructions for Running the Sample

Before running the demo, you need to build all three projects. You should build them in this order:

  1. RemoteCustomer
  2. Host
  3. Client

Once the binaries have been build, you will need to start the Host application first to make sure your objects are available for remoting. Next start your client.

If you wish to test on two different machines, you will need to make changes to the client's .config file changing localhost to an IP address or DNS name for the server machine. In addition, you might need to change the server's file if you change the port number. See the comments in both the client's .config file and the host's .config file for examples.

I Explain Some of Important functions here Source code is given with that article. Comments are very Important With Code.this is the Part one of  How To TCP Remoting Using VB.Net

Article Comments
how to conncet the client machine with server in the same LAN using asp.net

Posted on 3/13/2007 12:55:21 PM by karthik

Before running the demo, you need to build all three projects. You should build them in this order:

RemoteCustomer (NOT AVAILABLE)
Host (NOT AVAILABLE)
Client

NOW WAT??????

Posted on 3/31/2007 7:35:38 AM by Shiraz

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How we can Handle Parent, Child Class Relationship in VB.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net