Client - Server vb.net 2008

Associate
Joined
19 Dec 2005
Posts
641
Location
Perth, Western Australia
Hi all,

I am looking to create a client - server in vb.net 2008. I am not a programmer and i am fairly crap at it.

Does anyone know how to do this or how to go about creating it.

I want the client side to read a reg key and sent the data to the server side.

Thanks
 
Well you'll need to learn to program for a start, surely?

You'll want to have a look at the System.Net.Sockets namespace, specifically the TcpClient and TcpListener classes.

Beyond that, just Google it; there's a plethora of resources on it.
 
Last edited:
Could you not just use the remote connection feature of regedit, or is it for something else?
 
To get you started pop this in a form. This will read a registry key in this case MenuShowDelay.

Code:
Imports Microsoft.Win32

Public Class FormReadRegistry
    Private Sub ButtonReadRegistry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonReadRegistry.Click
        Dim sAns As String
        Dim sErr As String = ""

        sAns = RegValue(RegistryHive.CurrentUser, "Control Panel\Desktop", "MenuShowDelay", sErr)
        If sAns <> "" Then
            MsgBox(sAns)
        Else
            MsgBox("This error occurred: " & sErr)
        End If
    End Sub

    Public Function RegValue(ByVal Hive As RegistryHive, ByVal Key As String, ByVal ValueName As String, Optional ByRef ErrInfo As String = "") As String
        Dim objParent As RegistryKey = Nothing
        Dim objSubkey As RegistryKey = Nothing
        Dim sAns As String = ""
        Select Case Hive
            Case RegistryHive.ClassesRoot
                objParent = Registry.ClassesRoot
            Case RegistryHive.CurrentConfig
                objParent = Registry.CurrentConfig
            Case RegistryHive.CurrentUser
                objParent = Registry.CurrentUser
            Case RegistryHive.DynData
                objParent = Registry.DynData
            Case RegistryHive.LocalMachine
                objParent = Registry.LocalMachine
            Case RegistryHive.PerformanceData
                objParent = Registry.PerformanceData
            Case RegistryHive.Users
                objParent = Registry.Users
        End Select
        Try
            objSubkey = objParent.OpenSubKey(Key)
            'if can't be found, object is not initialized
            If Not objSubkey Is Nothing Then
                sAns = (objSubkey.GetValue(ValueName))
            End If
        Catch ex As Exception
            ErrInfo = ex.Message
        Finally
            'if no error but value is empty, populate errinfo
            If ErrInfo = "" And sAns = "" Then
                ErrInfo = _
                   "No value found for requested registry key"
            End If
        End Try
        Return sAns
    End Function
End Class
 
Reading the registry key is hardly the most complex part of what he wants to do though ;)

Neither is a client / server app I have one here but I'm not going to post it. I'm just getting him started. I suggest if you aren't going to make a valid contribution to the thread you don't bother posting. ;)
 
Neither is a client / server app I have one here but I'm not going to post it. I'm just getting him started. I suggest if you aren't going to make a valid contribution to the thread you don't bother posting. ;)

Apologies; wasn't intended as a jab, but it's a bit of a difficult request to give a useful answer to :)

I'd beg to differ regarding the difficulty of a client/server app for a beginner though; it requires asynchrony and multithreading, which is a somewhat difficult area for beginners to get to grips with.
 
Last edited:
Apologies; wasn't intended as a jab, but it's a bit of a difficult request to give a useful answer to :)

I'd beg to differ regarding the difficulty of a client/server app for a beginner though; it requires asynchrony and multithreading, which is a somewhat difficult area for beginners to get to grips with.

Apology accepted. I agree it's not easy but I think this is a nice little project to get stuck in there. Client / Server stuff is so much easier in .NET compared to VB6.

One of my first projects back all them years ago was a C / ASM app to stop people copying my friend's software (CDs). It worked for just under two months then got cracked which I thought was quite an accomplishment.
 
Ah i can program a bit but i am not very good at it.

I have some examples for client-server, i just need to set up a client - server and the data read from certain registry keys on the client side will be passed to the server side.

Where it will be put into a sql db. I am also going to run the client side as a windows service but i am okay with that part.

This is what i have so far but i am unsure if it works right.

Client
Code:
[COLOR=white][SIZE=2]Imports[/SIZE][SIZE=2] System[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Imports[/SIZE][SIZE=2] Microsoft.Win32[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Class[/SIZE][SIZE=2] Form1[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] client [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Net.Sockets.TcpClient[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Const[/SIZE][SIZE=2] BYTES_TO_READ [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Integer[/SIZE][SIZE=2] = 255[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] readBuffer(BYTES_TO_READ) [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Byte[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] sender [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] e [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2]Handles[/SIZE][SIZE=2]MyBase[/SIZE][SIZE=2].Load[/SIZE][/COLOR]
[COLOR=white][SIZE=2]client = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] System.Net.Sockets.TcpClient([/SIZE][SIZE=2]"localhost"[/SIZE][SIZE=2], 43001)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, [/SIZE][SIZE=2]AddressOf[/SIZE][SIZE=2] doRead, [/SIZE][SIZE=2]Nothing[/SIZE][SIZE=2])[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] doRead([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] ar [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.IAsyncResult)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] totalRead [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Integer[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Try[/COLOR][/SIZE]
[COLOR=white][SIZE=2]totalRead = client.GetStream.EndRead(ar) [/SIZE][SIZE=2]'Ends the reading and returns the number of bytes read. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Catch[/SIZE][SIZE=2] ex [/SIZE][SIZE=2]As[/SIZE][SIZE=2] Exception[/SIZE][/COLOR]
[SIZE=2][COLOR=white]'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections [/COLOR][/SIZE]
[SIZE=2][COLOR=white]'to this client and remove it from the list of connected clients. [/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Try[/SIZE][/COLOR]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] totalRead > 0 [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[SIZE=2][COLOR=white]'the readBuffer array will contain everything read from the client. [/COLOR][/SIZE]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] receivedString [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2] = System.Text.Encoding.UTF8.GetString(readBuffer, 0, totalRead)[/SIZE][/COLOR]
[SIZE=2][COLOR=white]messageReceived(receivedString)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Try[/COLOR][/SIZE]
[COLOR=white][SIZE=2]client.GetStream.BeginRead(readBuffer, 0, BYTES_TO_READ, [/SIZE][SIZE=2]AddressOf[/SIZE][SIZE=2] doRead, [/SIZE][SIZE=2]Nothing[/SIZE][SIZE=2]) [/SIZE][SIZE=2]'Begin the reading again. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Catch[/SIZE][SIZE=2] ex [/SIZE][SIZE=2]As[/SIZE][SIZE=2] Exception[/SIZE][/COLOR]
[SIZE=2][COLOR=white]'The underlying socket have probably been closed OR an error has occured whilst trying to access it, either way, this is where you should remove close all eventuall connections [/COLOR][/SIZE]
[SIZE=2][COLOR=white]'to this client and remove it from the list of connected clients. [/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Try[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white] [/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] messageReceived([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] message [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2])[/SIZE][/COLOR]
[SIZE=2][COLOR=white]MessageBox.Show(message)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] SendMessage([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] msg [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2])[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] sw [/SIZE][SIZE=2]As[/SIZE][SIZE=2] IO.StreamWriter[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Try[/COLOR][/SIZE]
[COLOR=white][SIZE=2]sw = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] IO.StreamWriter(client.GetStream)[/SIZE][/COLOR]
[SIZE=2][COLOR=white]sw.Write(msg)[/COLOR][/SIZE]
[SIZE=2][COLOR=white]sw.Flush()[/COLOR][/SIZE]
[COLOR=white][SIZE=2]Catch[/SIZE][SIZE=2] ex [/SIZE][SIZE=2]As[/SIZE][SIZE=2] Exception[/SIZE][/COLOR]
[SIZE=2][COLOR=white]MessageBox.Show(ex.ToString)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Try[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Class[/SIZE][/COLOR]
[COLOR=white]
[/COLOR]

Server
Code:
[/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Class[/SIZE][SIZE=2] Form1[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] listener [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Net.Sockets.TcpListener[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] listenThread [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Threading.Thread[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] clients [/SIZE][SIZE=2]As[/SIZE][SIZE=2]New[/SIZE][SIZE=2] List([/SIZE][SIZE=2]Of[/SIZE][SIZE=2] ConnectedClient) [/SIZE][SIZE=2]'This list will store all connected clients. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] sender [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] e [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2]Handles[/SIZE][SIZE=2]MyBase[/SIZE][SIZE=2].Load[/SIZE][/COLOR]
[COLOR=white][SIZE=2]listener = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, 43001) [/SIZE][SIZE=2]'The TcpListener will listen for incoming connections at port 43001 [/SIZE][/COLOR]
[COLOR=white][SIZE=2]listener.Start() [/SIZE][SIZE=2]'Start listening. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]listenThread = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] System.Threading.Thread([/SIZE][SIZE=2]AddressOf[/SIZE][SIZE=2] doListen) [/SIZE][SIZE=2]'This thread will run the doListen method [/SIZE][/COLOR]
[COLOR=white][SIZE=2]listenThread.IsBackground = [/SIZE][SIZE=2]True[/SIZE][SIZE=2]'Since we dont want this thread to keep on running after the application closes, we set isBackground to true. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]listenThread.Start() [/SIZE][SIZE=2]'Start executing doListen on the worker thread. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] doListen()[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] incomingClient [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Net.Sockets.TcpClient[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Do[/COLOR][/SIZE]
[COLOR=white][SIZE=2]incomingClient = listener.AcceptTcpClient [/SIZE][SIZE=2]'Accept the incoming connection. This is a blocking method so execution will halt here until someone tries to connect. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] connClient [/SIZE][SIZE=2]As[/SIZE][SIZE=2]New[/SIZE][SIZE=2] ConnectedClient(incomingClient, [/SIZE][SIZE=2]Me[/SIZE][SIZE=2]) [/SIZE][SIZE=2]'Create a new instance of ConnectedClient (check its constructor to see whats happening now). [/SIZE][/COLOR]
[COLOR=white][SIZE=2]AddHandler[/SIZE][SIZE=2] connClient.dataReceived, [/SIZE][SIZE=2]AddressOf[/SIZE][SIZE=2]Me[/SIZE][SIZE=2].messageReceived[/SIZE][/COLOR]
[COLOR=white][SIZE=2]clients.Add(connClient) [/SIZE][SIZE=2]'Adds the connected client to the list of connected clients. [/SIZE][/COLOR]
[SIZE=2][COLOR=white]Loop[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] removeClient([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] client [/SIZE][SIZE=2]As[/SIZE][SIZE=2] ConnectedClient)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] clients.Contains(client) [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[SIZE=2][COLOR=white]clients.Remove(client)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] messageReceived([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] sender [/SIZE][SIZE=2]As[/SIZE][SIZE=2] ConnectedClient, [/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] message [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2]) [/SIZE][SIZE=2]'A message has been received from one of the clients. 'To determine who its from, use the sender object. 'sender.SendMessage can be used to reply to the sender. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] data() [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2] = message.Split([/SIZE][SIZE=2]"|"c[/SIZE][SIZE=2]) [/SIZE][SIZE=2]'Split the message on each | and place in the string array. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Select[/SIZE][SIZE=2]Case[/SIZE][SIZE=2] data(0)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Case[/SIZE][SIZE=2]"CONNECT"[/SIZE][SIZE=2]'We use GetClientByName to make sure no one else is using this username. 'It will return Nothing if the username is free. 'Since the client sent the message in this format: CONNECT|UserName, the username will be in the array on index 1. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] GetClientByName(data(1)) [/SIZE][SIZE=2]Is[/SIZE][SIZE=2]Nothing[/SIZE][SIZE=2]Then[/SIZE][SIZE=2]'The username is not taken, we can safely assign it to the sender. [/SIZE][/COLOR]
[SIZE=2][COLOR=white]sender.Username = data(1)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Case[/SIZE][SIZE=2]"DISCONNECT"[/SIZE][/COLOR]
[SIZE=2][COLOR=white]removeClient(sender)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Select[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Function[/SIZE][SIZE=2] GetClientByName([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] name [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2]) [/SIZE][SIZE=2]As[/SIZE][SIZE=2] ConnectedClient[/SIZE][/COLOR]
[COLOR=white][SIZE=2]For[/SIZE][SIZE=2]Each[/SIZE][SIZE=2] cc [/SIZE][SIZE=2]As[/SIZE][SIZE=2] ConnectedClient [/SIZE][SIZE=2]In[/SIZE][SIZE=2] clients[/SIZE][/COLOR]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] cc.Username = name [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Return[/SIZE][SIZE=2] cc [/SIZE][SIZE=2]'client found, return it. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Next[/SIZE][SIZE=2]'If we've reached this part of the method, there is no client by that name [/SIZE][/COLOR]
[COLOR=white][SIZE=2]Return[/SIZE][SIZE=2]Nothing[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Function[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Class[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Class[/SIZE][SIZE=2] ConnectedClient[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] mClient [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Net.Sockets.TcpClient[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] mUsername [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] mParentForm [/SIZE][SIZE=2]As[/SIZE][SIZE=2] Form1[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2] readThread [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Threading.Thread[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Const[/SIZE][SIZE=2] MESSAGE_DELIMITER [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Char[/SIZE][SIZE=2] = ControlChars.Cr[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Event[/SIZE][SIZE=2] dataReceived([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] sender [/SIZE][SIZE=2]As[/SIZE][SIZE=2] ConnectedClient, [/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] message [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2])[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Sub[/SIZE][SIZE=2]New[/SIZE][SIZE=2]([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] client [/SIZE][SIZE=2]As[/SIZE][SIZE=2] System.Net.Sockets.TcpClient, [/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] parentForm [/SIZE][SIZE=2]As[/SIZE][SIZE=2] Form1)[/SIZE][/COLOR]
[SIZE=2][COLOR=white]mParentForm = parentForm[/COLOR][/SIZE]
[SIZE=2][COLOR=white]mClient = client[/COLOR][/SIZE]
[COLOR=white][SIZE=2]readThread = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] System.Threading.Thread([/SIZE][SIZE=2]AddressOf[/SIZE][SIZE=2] doRead)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]readThread.IsBackground = [/SIZE][SIZE=2]True[/SIZE][/COLOR]
[SIZE=2][COLOR=white]readThread.Start()[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Property[/SIZE][SIZE=2] Username() [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Get[/COLOR][/SIZE]
[COLOR=white][SIZE=2]Return[/SIZE][SIZE=2] mUsername[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Get[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Set[/SIZE][SIZE=2]([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] value [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2])[/SIZE][/COLOR]
[SIZE=2][COLOR=white]mUsername = value[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Set[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Property[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Private[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] doRead()[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Const[/SIZE][SIZE=2] BYTES_TO_READ [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Integer[/SIZE][SIZE=2] = 255[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] readBuffer(BYTES_TO_READ) [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Byte[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] bytesRead [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Integer[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] sBuilder [/SIZE][SIZE=2]As[/SIZE][SIZE=2]New[/SIZE][SIZE=2] System.Text.StringBuilder[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Do[/COLOR][/SIZE]
[SIZE=2][COLOR=white]bytesRead = mClient.GetStream.Read(readBuffer, 0, BYTES_TO_READ)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] (bytesRead > 0) [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] message [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2] = System.Text.Encoding.UTF8.GetString(readBuffer, 0, bytesRead)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] (message.IndexOf(MESSAGE_DELIMITER) > -1) [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] subMessages() [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2] = message.Split(MESSAGE_DELIMITER)[/SIZE][/COLOR]
[SIZE=2][COLOR=white]'The first element in the subMessages string array must be the last part of the current message. 'So we append it to the StringBuilder and raise the dataReceived event [/COLOR][/SIZE]
[SIZE=2][COLOR=white]sBuilder.Append(subMessages(0))[/COLOR][/SIZE]
[COLOR=white][SIZE=2]RaiseEvent[/SIZE][SIZE=2] dataReceived([/SIZE][SIZE=2]Me[/SIZE][SIZE=2], sBuilder.ToString)[/SIZE][/COLOR]
[COLOR=white][SIZE=2]sBuilder = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] System.Text.StringBuilder[/SIZE][/COLOR]
[SIZE=2][COLOR=white]'If there are only 2 elements in the array, we know that the second one is an incomplete message, 'though if there are more then two then every element inbetween the first and the last are complete messages: [/COLOR][/SIZE]
[COLOR=white][SIZE=2]If[/SIZE][SIZE=2] subMessages.Length = 2 [/SIZE][SIZE=2]Then[/SIZE][/COLOR]
[SIZE=2][COLOR=white]sBuilder.Append(subMessages(1))[/COLOR][/SIZE]
[SIZE=2][COLOR=white]Else[/COLOR][/SIZE]
[COLOR=white][SIZE=2]For[/SIZE][SIZE=2] i [/SIZE][SIZE=2]As[/SIZE][SIZE=2]Integer[/SIZE][SIZE=2] = 1 [/SIZE][SIZE=2]To[/SIZE][SIZE=2] subMessages.GetUpperBound(0) - 1[/SIZE][/COLOR]
[COLOR=white][SIZE=2]RaiseEvent[/SIZE][SIZE=2] dataReceived([/SIZE][SIZE=2]Me[/SIZE][SIZE=2], subMessages(i))[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Next[/COLOR][/SIZE]
[SIZE=2][COLOR=white]sBuilder.Append(subMessages(subMessages.GetUpperBound(0)))[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Else[/COLOR][/SIZE]
[SIZE=2][COLOR=white]'MESSAGE_DELIMITER was not found in the message, so we just append everything to the stringbuilder. [/COLOR][/SIZE]
[SIZE=2][COLOR=white]sBuilder.Append(message)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]If[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Loop[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Public[/SIZE][SIZE=2]Sub[/SIZE][SIZE=2] SendMessage([/SIZE][SIZE=2]ByVal[/SIZE][SIZE=2] msg [/SIZE][SIZE=2]As[/SIZE][SIZE=2]String[/SIZE][SIZE=2])[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Dim[/SIZE][SIZE=2] sw [/SIZE][SIZE=2]As[/SIZE][SIZE=2] IO.StreamWriter[/SIZE][/COLOR]
[SIZE=2][COLOR=white]Try[/COLOR][/SIZE]
[COLOR=white][SIZE=2]SyncLock[/SIZE][SIZE=2] mClient.GetStream[/SIZE][/COLOR]
[COLOR=white][SIZE=2]sw = [/SIZE][SIZE=2]New[/SIZE][SIZE=2] IO.StreamWriter(mClient.GetStream) [/SIZE][SIZE=2]'Create a new streamwriter that will be writing directly to the networkstream. [/SIZE][/COLOR]
[SIZE=2][COLOR=white]sw.Write(msg)[/COLOR][/SIZE]
[SIZE=2][COLOR=white]sw.Flush()[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]SyncLock[/SIZE][/COLOR]
[COLOR=white][SIZE=2]Catch[/SIZE][SIZE=2] ex [/SIZE][SIZE=2]As[/SIZE][SIZE=2] Exception[/SIZE][/COLOR]
[SIZE=2][COLOR=white]MessageBox.Show(ex.ToString)[/COLOR][/SIZE]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Try[/SIZE][SIZE=2]'As opposed to writing to a file, we DONT call close on the streamwriter, since we dont want to close the stream. [/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Sub[/SIZE][/COLOR]
[COLOR=white][SIZE=2]End[/SIZE][SIZE=2]Class[/SIZE][/COLOR]
[COLOR=white]
 
Last edited:
I'd suggest looking at Windows Communication Foundation for the client - server communication. It's the replacement for webservices and .NET remoting and is very powerful and flexible.
 
Back
Top Bottom