
Originally Posted by
jmcilhinney
The whole point of my code is that you simply create the objects, tell them to connect and then tell them to communicate. Any multi-threading is handled internally, so if you want multiple clients then you simply create multiple clients. If you want to pass them different addresses then that's what you do. If you know how to pass one address to one MessageClient object then you know how to pass multiple addresses to multiple objects: it's simply doing the one thing multiple times.
Probably I need to learn a lot about object programming
. I try to create a service that will communicate with various hardware modules and write data in database. I'm electronic hobbyst, not the proffessional programmers, so maybe you can help me with following code .
Code:
Imports Wunnell.Net
Public Class Form1
Private ReadOnly host As String = "localhost"
Private ReadOnly port As Integer = 8081
Private WithEvents client As New MessageClient(host, port)
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Me.client.Dispose()
End Sub
Private Sub connectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connectButton.Click
'Me.connectButton.Enabled = False
'Connect to the server.
'Me.client.Connect()
End Sub
Private Sub sendButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sendButton.Click
Dim message = Me.messageTextBox.Text
Me.client.Send(message)
Me.UpdateLog(String.Format("Message (=>{0}:{1}): {2}", Me.host, Me.port, message))
Me.messageTextBox.SelectAll()
Me.messageTextBox.Select()
End Sub
Private Sub PromptToReconnect(ByVal message As String, ByVal caption As String, ByVal icon As MessageBoxIcon)
Me.sendButton.Enabled = False
Me.connectButton.Enabled = True
Select Case MessageBox.Show(message, _
caption, _
MessageBoxButtons.YesNoCancel, _
icon)
Case Windows.Forms.DialogResult.Yes
'Try again to connect.
Me.connectButton.PerformClick()
Case Windows.Forms.DialogResult.No
'Do nothing.
Case Windows.Forms.DialogResult.Cancel
Me.Close()
End Select
End Sub
Private Sub UpdateLog(ByVal text As String)
Me.logTextBox.AppendText(text & ControlChars.NewLine)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
client.Connect()
AddHandler client.ConnectionAccepted, AddressOf Accepted
AddHandler client.MessageReceived, AddressOf MessageRecieved
AddHandler client.ConnectionFailed, AddressOf failed
AddHandler client.ConnectionClosed, AddressOf cclosed
End Sub
Private Sub MessageRecieved(ByVal Sender As Object, ByVal e As MessageReceivedEventArgs)
Me.UpdateLog(String.Format("Message ({0}=>): {1}", e.Host, e.Message))
End Sub
Private Sub failed(ByVal Sender As Object, ByVal e As MessageReceivedEventArgs)
Me.PromptToReconnect("The specified server could not be found. Would you like to try again?", _
"Connection Failed", _
MessageBoxIcon.Error)
End Sub
Private Sub cclosed(ByVal Sender As Object, ByVal e As MessageReceivedEventArgs)
Me.PromptToReconnect("The connection was closed by the server. Would you like to reconnect?", _
"Connection Closed", _
MessageBoxIcon.Warning)
End Sub
Private Sub Accepted(ByVal Sender As Object, ByVal e As ConnectionEventArgs)
Dim host = e.Host.ToString()
Me.Text = String.Format("{0}=>{1}", Me.client.LocalPort, host)
Me.UpdateLog("Info: Connection accepted to " & host)
Me.sendButton.Enabled = True
End Sub
End Class
As you can see I put all code on an form and this is the end of my programming knowledge
. Now I need a little help on how to dynamically create objects (clients) which would be connected to different servers.