SocketWrench .NET Edition

Example Form Code

With the form design completed, the next step is to add the code which will connect to the server, retrieve the file and display it. This can be done in the Click event handler for the Button1 control. By double-clicking on the button, a code window will open in the IDE and your cursor will be positioned at an empty event handler named Button1_Click:

[Visual Basic]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub

The first thing that we need to do is create an instance of the SocketWrench class which will be used in the event handler. To do this, we use the Dim statement:

[Visual Basic]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Socket As New SocketTools.SocketWrench

    If Socket.Initialize() = False Then
        MsgBox("Unable to initialize SocketWrench class", MsgBoxStyle.Exclamation)
        Exit Sub
    End If
End Sub

This ensures that the new instance of the class could be initialized correctly, loading the required networking libraries and preparing it to be used by the application. After the class instance has been initialized, the next step is to establish a connection with the remote host. To do this, we need to let SocketWrench know what server to connect to. This can be done by setting the HostName and RemotePort properties. We also want to specify a timeout period, so we'll set the Timeout property as well:

[Visual Basic]
Socket.HostName = TextBox1.Text
Socket.RemotePort = CInt(TextBox2.Text)
Socket.Timeout = CInt(TextBox3.Text)

The CInt function is used to convert the port number and timeout period to integer values. Note that in a complete, real-world application you would want to make sure that the values being entered by the user are valid. However, for purposes of this example we'll assume that reasonable values have been entered.

After the properties have been set, the next step is to establish the connection with the server. This is done using the Connect method:

[Visual Basic]
If Socket.Connect() = False Then
    MsgBox(Socket.LastErrorString, MsgBoxStyle.Exclamation)
    Exit Sub
End If

If the connection is successful, then the method will return True. If it fails, then it will return False and a message box is displayed to the user indicating that there was an error. The actual text of the error message comes from the LastErrorString property, which returns a string that describes the last error which has occurred.

Next, we use the WriteLine method to send the GET command to the web server. This command tells the server that we want it to retrieve the contents of the specified file and send it back to us. The data is then read using the ReadStream method, which reads all of the available data on the socket and returns it in a string buffer. That string is then displayed in TextBox5, our multi-line textbox control. When we're done, we call the Disconnect method to terminate the session.

Here's the complete code for the Button1_Click event handler:

[Visual Basic]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Socket As New SocketTools.SocketWrench
    Dim strCommand As String
    Dim strBuffer As String

    If Socket.Initialize() = False Then
        MsgBox("Unable to initialize SocketWrench class", MsgBoxStyle.Exclamation)
        Exit Sub
    End If

    Socket.HostName = TextBox1.Text
    Socket.RemotePort = CInt(TextBox2.Text)
    Socket.Timeout = CInt(TextBox3.Text)

    ' Establish a connection with the server and display an error
    ' message if the connection attempt fails
    If Socket.Connect() = False Then
        MsgBox(Socket.LastErrorString, MsgBoxStyle.Exclamation)
        Exit Sub
    End If

    ' Compose the command to be sent to the server to request the
    ' file and send the command to the server using the WriteLine
    ' method
    strCommand = "GET " + TextBox4.Text
    Socket.WriteLine(strCommand)

    ' Read the data returned by the server and display it in the
    ' multi-line textbox control
    If Socket.ReadStream(strBuffer, True) Then
        TextBox5.Text = strBuffer
    End If

    ' Disconnect from the server
    Socket.Disconnect()
End Sub