Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub buttonConnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles buttonConnect.Click
' reset the socket
Winsock.Close()
' set the new properties
Winsock.RemoteHost = "localhost" 'I put localhost here just now instead of my real IP
Winsock.RemotePort = "1234"
' initiate the connection
txtMain.Text = txtMain.Text & vbCrLf & "Connecting to " & comboDSLAM.Text & "..."
Winsock.Connect()
End Sub
Private Sub cmdSend_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles buttonSend.Click
' make sure we're connected
If Winsock.CtlState = MSWinsockLib.StateConstants.sckConnected Then
' send the data
Winsock.SendData(txtCommand.Text & vbCrLf)
System.Windows.Forms.Application.DoEvents()
' log the data in the main window
LogData(txtCommand.Text)
' clear the text ready for the next command
txtCommand.Text = ""
Else
txtMain.Text = txtMain.Text & vbCrLf & "You're not connected to anything"
End If
End Sub
Private Sub Winsock_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock.ConnectEvent
' alert the user that we're connected
txtMain.Text = txtMain.Text & vbCrLf & "Connection established to " & Winsock.RemoteHostIP
End Sub
Private Sub Winsock_CloseEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock.CloseEvent
' alert the user when disconnected
txtMain.Text = txtMain.Text & vbCrLf & "The connection to " & comboDSLAM.Text & " (" & Winsock.RemoteHostIP & ") has been terminated"
End Sub
Private Sub Winsock_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles Winsock.DataArrival
Dim strData As String
' get the data
Winsock.GetData(strData)
' display it in the main textbox
LogData(strData)
End Sub
Private Sub Winsock_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles Winsock.Error
' alert the user to the error
txtMain.Text = txtMain.Text & vbCrLf & "Error: " & eventArgs.description
End Sub
Private Sub LogData(ByVal strData As String)
' add string to the end of the textbox
txtMain.Text = txtMain.Text & vbCrLf & strData
' scroll the textbox down
txtMain.SelectionStart = Len(txtMain.Text)
End Sub
Private Sub Form_Unload(ByVal Cancel As Integer)
Winsock.Close()
End Sub
End Class