• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1056
  • Last Modified:

Need VB.Net code - client/server system. Server must be multi-threaded.

I need to implement a client/server system in VB.NET.

The bulk of my experience is in C on UNIX, Java and Delphi.  What I'd do in any of those languages/environments is make a multi-threaded server that takes socket connections via TCP/IP.  On a Windows environment, I'd make the server a service so it is always available and running.  Now, I have done this sort of thing before in those other languages, but I'm having trouble getting started, in VB.NET.

I also need the server to perform periodic work on the file system - and not just process incoming TCP/IP connections.  Presumably this should be in a thread of its own.

There are various code samples on MSDN for socket servers and simple services but I'm having trouble particularly working out how to create and manage threads in VB.NET.

What I would like is working code for a client/server system that meets these requirements.  That is, I want the server to be a service and to be multithreaded.  It must be capable of listening for incoming socket connections and to perform other work apart from the socket side of things.

If EE allows me to, I'll award extra points for swift and helpful solutions!!
0
davidmwilliams
Asked:
davidmwilliams
  • 12
  • 6
1 Solution
 
iboutchkineCommented:
You can use .NET remoting. Here is an example of Client/Server app


Get message like data

'Both Server and client are build as WinForms App

=================================
1.Server Form   '1 label on a Server form
=================================
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim channel As New TcpChannel(9999)
            ChannelServices.RegisterChannel(channel)
            RemotingConfiguration.ApplicationName = "SimpleRemotingServer"
'Server is the name of the root namespace. It must be the same for client and server. It is in
'Progect properties - general          
            Dim t As Type = Type.GetType("Server.SimpleClass")
            RemotingConfiguration.RegisterWellKnownServiceType(t, "SimpleClass", WellKnownObjectMode.SingleCall)
            Label1.Text = "SimpleClass registered." & vbCrLf & "Remoting running..."
        Catch ex As Exception
            Label1.Text = "Remoting initilization failed."
        End Try
    End Sub
End Class

'Server Class (SimpleClass)
Public Class SimpleClass
    Inherits MarshalByRefObject

    Public Function GetDateTime() As Date
        MsgBox("I've got request for Date/Time")
        Return Now()
    End Function

    Public Function Hello() As String
        MsgBox("I've got request for Hello World")
'here server returns data to client request
        Return "Hello World!"
    End Function
End Class

=================================
2.Client Form' 2 buttons - Get TDate/Time and HelloWorld and txtIP where to enter IP Address
=================================

Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Dim sc As SimpleClass
    Private Sub DateTimeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimeButton.Click
        If Not Initialize() Then Return

        MsgBox(sc.GetDateTime(), MsgBoxStyle.DefaultButton1, "GetDateTime()")
    End Sub

    Private Sub HelloButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelloButton.Click
        If Not Initialize() Then Return

        MsgBox(sc.Hello(), MsgBoxStyle.DefaultButton1, "Hello()")
    End Sub

    Public Function Initialize() As Boolean
        If Not ValidateIP Then
            MsgBox("Not Valid IP Address")
            Initialize = False
            Exit Function
        End If

        If sc Is Nothing Then
            Dim chan As New TcpChannel()
            ChannelServices.RegisterChannel(chan)
            Dim t As Type = Type.GetType("Server.SimpleClass")


            sc = Activator.GetObject(t, "tcp://" & txtIP.Text.ToString & ":9999/SimpleClass")


            If sc Is Nothing Then
                MsgBox("Could not initialize the remoting client. Check your configuration.")
                Return False
            End If
        End If

        Return True
    End Function
End Class


'Client Class' we need it only for initialization (SimpleClass)

Public Class SimpleClass
    Inherits MarshalByRefObject

    Public Function GetDateTime() As Date
    End Function

    Public Function Hello() As String
    End Function

    Private Function ValidateIP()
        If Len(txtIP.Text.ToString) = 0 Then
            Return False
        Else
            Return True
        End If
    End Function

End Class
0
 
davidmwilliamsAuthor Commented:
Looks interesting!  I'll try that out.

Although -- how easy would it be to turn the server into a service?

Can the server handle multiple simultaneous client requests?

Is the server is 'free' to do other tasks, and isn't blocked by listening for remoting requests?
0
 
iboutchkineCommented:
Yes it can. I even tried multiple requests from the same client. I mean that IP of the requestor is thte same. I wrote a prog that returns dataset and from the same client I have requested different datasets that i bound to the different datagrids. Because it created a channel, the server knows through which channel to send the respond (even if it is the same IP)
0
[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

 
davidmwilliamsAuthor Commented:
Looks interesting!  I'll try that out.

Although -- how easy would it be to turn the server into a service?

Can the server handle multiple simultaneous client requests?

Is the server is 'free' to do other tasks, and isn't blocked by listening for remoting requests?
0
 
davidmwilliamsAuthor Commented:
VB.NET says it can't find the namespace System.Runtime.Remoting.Channels.Tcp and that it doesn't know what TcpChannel is ...
0
 
iboutchkineCommented:
Did you include the following imports?
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

Where do you get error?

I have this code running fine on my computer.
0
 
davidmwilliamsAuthor Commented:
I get the error on the Imports System.Runtime.Remoting.Channels.Tcp line - this namespace isn't recognised.

However, I do have Visual Studio.NET 2002 Enterprise.  Are you using the 2003 beta?
0
 
iboutchkineCommented:
No  I am using VS2002 Enterprise.
When you are typing on the top of the form
Imports System.Runtime.Remoting.Channels.Tcp
after each period you can see intellisense. What namespaces you cannot find (Runtime, Remoting etc)

Also check what references do you have. Click on the + in references and check what do you have there
I have the following
System
System.Data
System.Drawing
System.Windows.Form
System.Runtime.Remoting
System.Runtime.Serialization.Formatters.Soap
System.XML
0
 
Bob LearnedCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by iboutchkine

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TheLearnedOne
EE Cleanup Volunteer
0
 
davidmwilliamsAuthor Commented:
Hmmm, the trouble is, I couldn't get iboutchkine's code to work and nor was it socket-related ....

If it worked, it may well proved to have solved my problem ...... but as it all stands, I'm not overly keen ...
0
 
iboutchkineCommented:
It works perfectly on my PC's
0
 
davidmwilliamsAuthor Commented:
Let me try it again in VS2003 ....
0
 
davidmwilliamsAuthor Commented:
YensidMod, you've done the wrong thing.
0
 
davidmwilliamsAuthor Commented:
PRECISELY AS BEFORE ...

"Namespace or type 'TCP' for the Imports 'System.Runtime.Remoting.Channels.Tcp' cannot be found.

Type 'TcpChannel' is not defined.

I'm using Visual Studio.NET 2003 Enterprise.
0
 
davidmwilliamsAuthor Commented:
How arrogant!

I did not 'abandon' the question at all. It simply did not receive any responses!

Nevertheless, there were only SIX DAYS between the question firing up again and your force accept. Your accept was simply a knee-jerk reaction by a wannabe moderator.

The code presented does not compile and nor does it address the question. It may well provide an alternative method, but it is not what I asked for - and it doesn't work.

On what basis do you say it is "technically correct"?

Certainly, people who come to E-E and PAY to see the answers to questions are going to feel mightily ripped off by this one!

The correct thing to do here is to simple DELETE the question and REFUND my points.

I am not at all impressed.
0
 
davidmwilliamsAuthor Commented:
Thanks Computer101.
0
 
iboutchkineCommented:
I mentioned before that davidwilliams must include the following imports

Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp

The code is working fine. I run it on XP and Win98
0
 
davidmwilliamsAuthor Commented:
I do have those imports.

However, Lunchy's comment may well be the answer. I will try adding the reference s/he suggests.
0
 
davidmwilliamsAuthor Commented:
What Lunchy said was correct (although the reference is actually System.Runtime.Remoting). Once this reference has been added, the code compiles.

I do not know why iboutchkine could not say this eight months ago.

I'm closing this now to let it die, but I can't award an A (as YensidMod arrogantly did) because the code does not provide the solution I requested. Using sockets I could make a client which did not require the .NET framework. The code provided constrains me to only have .NET clients.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 12
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now