VB6 to .Net

Jay1b

Contributor
Joined
Aug 3, 2003
Messages
640
Location
Kent, Uk.
Hi

I'm trying to convert a VB6 project to .Net. This worked to some extent, however it didnt convert winsock to sockets. I've tried rewriting the sockets side of things, but i have no experience in this area.

Visual Basic:
   Private Sub btnMyCode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMyCode.Click

        Dim strMessage As String = ""
        Dim strEsc As String = Chr(&H1BS) 'Holds Escape ASCII Character
        sHeader = Chr(&H99S)
        sNumMessage = Chr(&H1S)
        sNumDestination = Chr(&H1S)
        sNumSigns = Chr(&H1S)
        sMessHigh = Chr(0)

        Call writedebug("Trying to Connect..." + sMessLow + "...")

        sMessText1 = strEsc & "[2J" & strEsc & "[1;1H" & sForeColorStr & Me.txtLine.Text


        sMessLow = Chr(Len(Trim(sMessText1)) + Len(Trim(sMessText2)))

        'build message for transmission
        sMessage = sHeader & sNumMessage & sNumDestination & sNumSigns & sMessLow & sMessHigh ' this is the message header stuff
        sMessage = sMessage & sMessText1  'add text for top line ' & sEsc & "[4x"
        sMessage = sMessage & sMessText2 ' add text for second line

        bytChecksum = 0
        ' update checksum using each byte of the message
        For i As Integer = 1 To Len(sMessage)
            bytChecksum = RotateLeft(bytChecksum)
            bytChecksum = bytChecksum Xor Asc(Mid(sMessage, i, 1))
        Next
        ' do one further rotation
        bytChecksum = RotateLeft(bytChecksum)
        ' and append the checksum to the message
        sMessage = sMessage & Chr(bytChecksum)

        'Connect to Socket using WinSock
        ' this boolean (bSocketInIse) is very important - you must not
        ' open a socket to the sign if there is already one open
        If bSocketInUse = False Then
            bSocketInUse = True
            ' we use a timer to close the socket if it fires - this is a safety measure in
            ' case something goes wrong
            Timer1.Interval = 10000
            Timer1.Enabled = True
            'Set Protocol and Sign Information
            'And Connect to socket.

            With Me.Winsock1
                .Protocol = MSWinsockLib.ProtocolConstants.sckTCPProtocol
                ' ip address of sign
                .RemoteHost = "172.28.48.150"
                ' port number sign listens on
                .RemotePort = CInt("3500")
                .Connect()
            End With
        Else
            Call writedebug("Socket In Use Please Wait.")
        End If

    End Sub

   Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
        'set escape string
        sEsc = Chr(&H1BS)

        bSocketInUse = False
        ' initialise to red text on black background
        sForeColorStr = Chr(27) & "[31;40m"

    End Sub

    Private Sub Form_Terminate_Renamed() ' ensure winsock is close when application terminated
        If Me.Winsock1.CtlState > 0 Then Me.Winsock1.Close()
    End Sub

    Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
        'if timer fires display timeout message and disconnect
        Timer1.Enabled = False
        Call writedebug("Display Timed Out")
        Me.Winsock1.Close()
        Call writedebug("Socket Disconnected.")
        bSocketInUse = False
    End Sub

    Private Sub writedebug(ByRef sText As String)
        Me.txtDebug.Text = Me.txtDebug.Text & sText & Environment.NewLine
    End Sub

    Private Sub Winsock1_CloseEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock1.CloseEvent ' Fires this event when sign closes socket
        Call writedebug("Socket Disconnected")
        Call writedebug("")
        bSocketInUse = False
        Me.Timer1.Enabled = False
        Me.Winsock1.Close()
    End Sub

    Private Sub Winsock1_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Winsock1.ConnectEvent ' Fired once winsock connects
        Call writedebug("Connected")
        With Me.Winsock1
            ' send the message to the sign
            .SendData(sMessage)
        End With
        Call writedebug("Data Sent")
    End Sub

    Private Sub Winsock1_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles Winsock1.Error
        ' this is an error event produced bu VB's Winsock
        'display error
        Call writedebug("Socket Wrench Error: " & eventArgs.number)
        'close socket
        Me.Winsock1.Close()
    End Sub

    Function RotateLeft(ByVal Octet As Short) As Byte
        ' used in calculation of checksum
        Octet = Octet * 2
        If Octet > &HFFS Then
            Octet = Octet - &HFFS
        End If
        RotateLeft = CByte(Octet)

    End Function


Can anyone help me convert this into using system.net.sockets please? I've been trying it, for a few days. For some reason all the examples i get form the web just dont seem to work.

Thanks
 
Back
Top