Skip to main content

 none
Update Code from VB6 to C# RRS feed

  • Question

  • 
    

    Would anyone like to convert this VB6 code to C#. It is quite a simple program. It just receives a small bit of data from a client to display while listening for other clients. I've Been trying to do is but just can't seem to figure out C# TCP listener. I can figure out other things but TCP is just not my thing. Thanks if you can help.

    Shelley.

    This is the Server Code

    Dim SocketNumber As Integer
    Dim detector(1001 To 1005, 0 To 7) As String
    Dim MyString As String
    Dim ArrCount As Integer
    Dim IP As String
    Dim Sig10 As String
    Dim DataUpload() As String
    Dim intX As Integer
    Dim DataPackets As Integer
    Dim packets(1001 To 1005) As Integer
    Dim c As Integer
    Dim I(1001 To 1005) As Integer
    
    
    
    Private Sub Command1_Click()
    End
    End Sub
    
    Sub Form_Load()
        Winsock1001(0).LocalPort = "1001"
        Winsock1001(0).Listen
        Winsock1002(0).LocalPort = "1002"
        Winsock1002(0).Listen
        Winsock1003(0).LocalPort = "1003"
        Winsock1003(0).Listen
        Winsock1004(0).LocalPort = "1004"
        Winsock1004(0).Listen
        Winsock1005(0).LocalPort = "1005"
        Winsock1005(0).Listen
        
                    Shape1(0).FillStyle = 0
                    Shape1(1).FillStyle = 0
                    Shape1(2).FillStyle = 0
                    
                    Shape1(3).FillStyle = 0
                    Shape1(4).FillStyle = 0
                    Shape1(5).FillStyle = 0
                    
                    Shape1(6).FillStyle = 0
                    Shape1(7).FillStyle = 0
    End Sub
    
    '*******************************************
    '*********    Client 1001    ***************
    '*******************************************
    
    
    
    Private Sub Winsock1001_Close(Index As Integer)
        Winsock1001(Index).Close
        lstUsers.AddItem Winsock1001(Index).Index & " Connection " & IP & " - just disconnected."
        
    End Sub
    
    Private Sub Winsock1001_ConnectionRequest(Index As Integer, ByVal requestID As Long)
            
        SocketNumber = SocketNumber + 1
        Load Winsock1001(SocketNumber)
        Winsock1001(SocketNumber).Accept requestID
        lstUsers.AddItem SocketNumber & " IP- " & Winsock1001(SocketNumber).RemoteHostIP & ":Port 1001" & " - Just connected : "
        IP = Winsock1001(SocketNumber).RemoteHostIP
        
    End Sub
    
    Private Sub Winsock1001_DataArrival(Index As Integer, ByVal bytesTotal As Long)
        Dim StringData As String
            Winsock1001(Index).GetData StringData, vbString
            DataUpload = Split(StringData, ",")
                For intX = 0 To UBound(DataUpload)
                Next
            DataPackets = DataPackets + 1
            packets(1001) = packets(1001) + 1
            IP = DataUpload(0)
            MyString = DataUpload(1)
            Const StringLen = 1 ' set whatever you want
            ReDim detector1001((Len(MyString) - 1) \ StringLen)
                For I(1001) = 1 To Len(MyString) Step StringLen
                    detector1001(ArrCount) = Mid$(MyString, I(1001), StringLen)
                    detector(1001, I(1001) - 1) = detector1001(ArrCount)
                    'a = I(1001) - 1
                    'detector(I(1001)) = StringArray(I(1001))
                     ArrCount = ArrCount + 1
                Next
            ArrCount = 0
           ' If DataPackets > 1000 Then DataPackets = 0 'Text1.Text = IP & " , " & MyString
            Text6.Text = DataPackets & " Data packs  " & detector(1001, 0) + " " + detector(1001, 1) + " " + detector(1001, 2) + " " + detector(1001, 3) + " " + detector(1001, 4) + " " + detector(1001, 5) + " " + detector(1001, 6) + " " + detector(1001, 7)
            'Text6.Text = clr
            Text1.Text = " 1001 Recieved " & packets(1001) & " Packets"
    
        
    pass:
        For A = 0 To 7
            detector(1001, A) = ""
            
        Next
    End Sub

    This is the Client Code

    Public signalstate1 As String
    Public signalstate2 As String
    Public a As Integer
    Dim rv(0 To 7) As Integer
    Dim rvs(0 To 7) As String
    
    Private Sub Command2_Click()
    End
    End Sub
    
    Private Sub Command5_Click()
            Winsock1.Close
    
    
    End Sub
    
    Public Sub Form_Load()
            form1.Show
            signalstate1 = "00000000"
            signalstate2 = "00000000"
            Winsock1.RemoteHost = "127.0.0.1"
            Winsock1.RemotePort = 1001
            Winsock1.Connect
    
    
    End Sub
    
    
    
    Private Sub Timer1_Timer()
    
    a = a + 1
    Text1.Text = a
        
        If Winsock1.State = 7 Then GoTo cont
            Winsock1.Close
            Winsock1.RemoteHost = "192.168.1.178"
            Winsock1.RemotePort = 1001
            Winsock1.Connect
    
        
        'End If
        
    cont:
        
        If Winsock1.State <> 7 Then GoTo runloop
        
        If signalstate2 = signalstate1 Then GoTo runloop
        
        If signalstate2 <> signalstate1 Then
            
            signalstate1 = signalstate2
            Text1.Text = "Server Connected"
            Winsock1.SendData Winsock1.RemoteHost & "," & signalstate1
            GoTo endsub
        Else
            
            GoTo runloop
            
        End If
    
    
    
    
    runloop:
    
    Text1.Text = " No Server Connection"
        'This part reads the detectors and looks for changes:
        'If a change is made then it will goto connect:
    endsub:
    
    End Sub
    
    Private Sub Timer2_Timer()
            
            rv(0) = 0 '((1 - 0) * Rnd())
            rv(1) = ((1 - 0) * Rnd())
            rv(2) = ((1 - 0) * Rnd())
            rv(3) = ((1 - 0) * Rnd())
            rv(4) = ((1 - 0) * Rnd())
            rv(5) = ((1 - 0) * Rnd())
            rv(6) = ((1 - 0) * Rnd())
            rv(7) = ((1 - 0) * Rnd())
            rvs(0) = (CStr(rv(0)))
            rvs(1) = (CStr(rv(1)))
            rvs(2) = (CStr(rv(2)))
            rvs(3) = (CStr(rv(3)))
            rvs(4) = (CStr(rv(4)))
            rvs(5) = (CStr(rv(5)))
            rvs(6) = (CStr(rv(6)))
            rvs(7) = (CStr(rv(7)))
            signalstate2 = rvs(0) + rvs(1) + rvs(2) + rvs(3) + rvs(4) + rvs(5) + rvs(6) + rvs(7)
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim getstring As String
    Winsock1.GetData getstring
    Text1.Text = getstring
    End Sub
    
    Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
    'End
    End Sub



    • Edited by Sbrown851 Sunday, September 25, 2016 1:28 AM
    Sunday, September 25, 2016 1:22 AM

All replies

  • Well the likelihood is that if you can't figure out a VB.Net listener by upgrading VB6 code to VB.Net manually then you will not figure out a C# .Net listener.

    Plus a VB.Net listener could be converted to C# using an online converter.

    And C# people are unlikely to provide a VB6 conversion to C# as they typically despise Visual Basic as C# is approved as a standard by Ecma (ECMA-334) and ISO (ISO/IEC 23270:2006) while VB6 and VB.Net are Microsoft specific red headed step children which Microsoft always seems to disregard when developing new capabilities and maybe providing at the last step, after major amount of people raise cain, the ability for VB.Net to be used with new capabilities.

    Is there some reason you can't search for C# code for a TCP listener? Or do you need a tutorial on how to write code for a TCP listener in C#? Or possibly do you not understand the VB6 code and therefore have no idea how it works such that if you understood C# code you couldn't create the C# code to perform as the VB6 code performs? Or you don't understand the VB6 code and you don't understand C# code so you have no idea how to do any of it?

    Also see Asynchronous Client Socket Example and Asynchronous Server Socket Example.


    La vida loca

    Sunday, September 25, 2016 4:30 AM
  • I didn't ask for a lecture.
    Monday, September 26, 2016 3:29 AM