?
Solved

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

Posted on 2003-03-30
26
Medium Priority
?
1,051 Views
Last Modified: 2011-10-03
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
Comment
Question by:davidmwilliams
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 6
26 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 1500 total points
ID: 8233818
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 8235209
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8235378
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 4

Author Comment

by:davidmwilliams
ID: 8235577
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 8236312
VB.NET says it can't find the namespace System.Runtime.Remoting.Channels.Tcp and that it doesn't know what TcpChannel is ...
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8238074
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 8242138
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8242306
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 9725690
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9728975
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9731290
It works perfectly on my PC's
0
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9735870
Let me try it again in VS2003 ....
0
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9777347
YensidMod, you've done the wrong thing.
0
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9801796
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9802012
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9804600
Thanks Computer101.
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 9817455
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
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9820883
I do have those imports.

However, Lunchy's comment may well be the answer. I will try adding the reference s/he suggests.
0
 
LVL 4

Author Comment

by:davidmwilliams
ID: 9840400
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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

765 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