Results 1 to 11 of 11

Thread: VB.net and Winsock

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    4

    VB.net and Winsock

    Hi,

    Basically my project is to create a client in vb.net to connect to an ip address and 10 ports on that ip address and wait for the data to come through.

    I've already made the software in VB.net that uses the new system.net.sockets but it has problems when reading the data (I'm reading every second and doing a split on the data into a textbox and from there i'm reading the textbox and moving the data from the textbox into an array) but it reads some of the data and then reads the next bit of data a second later instead of waiting for all of the data like the old Winsock control used to do.

    I'm wondering if anyone has managed to get the Winsock control working in VB.Net (with multi-connections) as the 'DataArrival' function would stop the problem that i'm having?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: VB.net and Winsock

    Welcome to the forums.

    This might help...

    http://www.freevbcode.com/ShowCode.asp?ID=7557

  3. #3
    Super Moderator
    Join Date
    Dec 2003
    Posts
    4,787

    Re: VB.net and Winsock

    Why are you using Winsock in .net?

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    4

    Re: VB.net and Winsock

    Hi guys thanks for the reply,

    The reason why i want to use winsock in vb.net is because im using .net sockets now and i'm having some issues, i'm currently listening to 10 ports on an ip address and i want to wait for data to come through and then do something with that data.

    The problem i'm having is that i'm running the script on a 1 second timer and sometimes i get the first part of the data from the com port and then a second later i get the rest...i can't allow this to happen. Do you have any ideas?....my code is below:

    Code:
     Dim clientSocket(10) As System.Net.Sockets.TcpClient 'set variable for tcp client connection
        Dim serverStream As NetworkStream 'set variable for network stream of data
        Dim RtnCmd As String 'return command recieved from data
        Dim strLocalCopyReturnData(10) As String 'convert ascii characters into strings and return
        Dim strReturnData As String 'data returned from network stream
        Dim RtnAscii As String 'ascii code returned
        Dim intNumberofPorts As Integer = 2 ' Number of sockets for test
    
    #Region "Timers"
    
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timRefresh_Read_Ports.Tick
    
            '******************************************************************************************************
            '
            '
            '   Subroutine :    Timer1_Tick
            '
            '   Description :   Check for a connection on the ports, waits for data to be available then
            '                   moves the data into a textbox. Then breakdowns the data to display and writes
            '                   it into the SQL database
            '                   
            '   Author :        M Mills
            '
            '   Date :          11 May 2010
            '
            '   Version :       1.00
            '
            '
            '****************************************************************************************************** 
    
            Dim myCommand As SqlCommand
            Dim ra As Integer
            Dim db_strlocalcopydata As String = Nothing
            Dim db_datetime As DateTime = Nothing
            Dim db_milliseconds As Integer = Nothing
            Dim db_crane As String = Nothing
            Dim db_direction As String = Nothing
            Dim db_string As String
            Dim db_cmd As String
            Dim db_x As String
            Dim db_y As String
            Dim db_f As String
            Dim strSQL_Data As String
            Dim StrTime As String = Nothing
    
            ToolStripStatusLabel4.Text = "Last Update : " & Now.ToString
    
            Dim myarray(10) As String
            Dim temparray(10) As String
    
            For INTX = 1 To intNumberofPorts
                Dim sqlarray() As String = Nothing
    
                If clientSocket(INTX).Connected Then
    
                    TimeSinceLastScan(INTX) = TimeSinceLastScan(INTX) + 1
    
                    While clientSocket(INTX).GetStream.DataAvailable
                        Dim serverStream As NetworkStream = clientSocket(INTX).GetStream()
                        Dim buffSize As Integer
                        serverStream.Flush()
    
                        Dim inStream(10024) As Byte
                        buffSize = clientSocket(INTX).ReceiveBufferSize
                        serverStream.Read(inStream, 0, buffSize)
                        Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
    
                        If clientSocket(INTX).GetStream.CanRead Then
    
                            TimeSinceLastScan(INTX) = 0
    
                            Select Case INTX
                                Case 1
                                        TextBox1.Text = returndata
                                    myarray = Split(TextBox1.Text, vbCrLf, -1, CompareMethod.Text)
                                Case 2
                                        TextBox2.Text = returndata
                                    myarray = Split(TextBox2.Text, vbCrLf, -1, CompareMethod.Text)
                             End Select
    
                            Dim counter2 As Integer
    
                            For counter2 = 0 To myarray.Count - 1
    
                                For counter = 1 To Len(myarray(counter2))
    
                                    RtnAscii = Asc(Mid(myarray(counter2), counter, 1))
                                    Select Case RtnAscii
                                        Case "255"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & "<LF>" & "<CR>" & " Crane " : RtnAscii = ""
                                        Case "254"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & "<LF>" & "<CR>" & " Host   " : RtnAscii = ""
                                        Case "253"
                                            RtnAscii = ""
                                        Case "3"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <ETX>" : RtnAscii = ""
                                        Case "5"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <ENC>" : RtnAscii = ""
                                        Case "6"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <ACK>" : RtnAscii = ""
                                        Case "10"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <LF>" : RtnAscii = ""
                                        Case "13"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <CR>" : RtnAscii = ""
                                        Case "4"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <EOT>" : RtnAscii = ""
                                        Case "2"
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & " <STX>" : RtnAscii = ""
                                        Case Is >= 32
                                            strLocalCopyReturnData(INTX) = strLocalCopyReturnData(INTX) & Mid(myarray(0), counter, 1) 'returndata
                                    End Select
    
                                Next counter
    
                                    sqlarray(counter2) = StrTime & " " & strLocalCopyReturnData(INTX)
                                    ListView1.Items(INTX - 1).SubItems.Item(4).Text = strLocalCopyReturnData(INTX)
    
                                    strLocalCopyReturnData(INTX) = ""
    
                            Next counter2
    
                            If sqlarray IsNot Nothing Then
                                For i = 0 To sqlarray.Count - 1
    
                                    If sqlarray(i) IsNot Nothing And (Mid(sqlarray(i), 26, 5) <> "<EOT>" Or Mid(sqlarray(i), 26, 5) <> "<ENC>") Then
    
                                        Select Case Mid(sqlarray(i), 31, 1)
                                            Case "G"
                                                db_cmd = Mid(sqlarray(i), 31, 1)
                                                db_x = Mid(sqlarray(i), 32, 3)
                                                db_y = Mid(sqlarray(i), 35, 3)
                                                db_f = Mid(sqlarray(i), 38, 3)
                                                      Case Else
                                                db_cmd = Nothing
                                                db_x = Nothing
                                                db_y = Nothing
                                                db_f = Nothing
                                        End Select
    
                                        If i = sqlarray.Count - 1 Then
                                            ListView1.Items(INTX - 1).SubItems.Item(1).Text = DateTime.Now.ToString("dd/MM/yyyy H:mm:ss.fff")
                                            ListView1.Items(INTX - 1).SubItems.Item(2).Text = db_crane
                                            ListView1.Items(INTX - 1).SubItems.Item(3).Text = db_direction
                                            ListView1.Items(INTX - 1).SubItems.Item(5).Text = db_cmd
                                            ListView1.Items(INTX - 1).SubItems.Item(6).Text = db_x
                                            ListView1.Items(INTX - 1).SubItems.Item(7).Text = db_y
                                            ListView1.Items(INTX - 1).SubItems.Item(8).Text = db_f
                                            ListView1.Refresh()
                                        End If
    
                                        db_strlocalcopydata = sqlarray(i)
    
                                        If db_strlocalcopydata <> Nothing Then
    
                                           {I then write the string to a SQL database}
    
                                        End If
                                    End If
                                Next i
                            End If
                        End If
    
                    End While
             Next
    
            myConnection.Close()
    
        End Sub
    
    #End Region

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: VB.net and Winsock

    It's a bit hard to follow your code, what with the ambiguous variable names and all, but if I understand you correctly...you say that you receive the first part of the data after at one time, and the other part the next time you read from the socket, and you want to read ALL at once?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    4

    Re: VB.net and Winsock

    Yea thats correct. I want to read everything thats come in and then move it to the textbox and from the textbox I can do whatever I want with it then. Is doing it on a 1 second timer the best way to do it or can I do it another way?

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: VB.net and Winsock

    Since the read operations are blocking you could very well spawn a thread for each socket that'll read data directly when it arrives at the socket. What you're doing now is polling to see if data has arrived, and might not be the best solution.

    Anyway, what you're doing right now is just saying "here's a buffer with a size of X, fill it as much as possible with all data that has arrived". All data might not have arrived on the underlying socket and it'll then just give you what it has accumulated so far.
    You should read the data in a more controlled manner. I assume you have a way to determine when you have reached the end of a message?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2010
    Posts
    4

    Re: VB.net and Winsock

    ok thanks, yea most of the data that arrives ends with <CR> <LF>, how would I go about doing it in a more controlled manner?...this is my first .net sockets project so i'm new to all this. How would I spawn a thread for each socket and wait for all the data to arrive?

  9. #9
    New Member
    Join Date
    Jun 2010
    Posts
    2

    Re: VB.net and Winsock

    Hi, curiously I have de same problem, the server&#180;s request arrive in two parts but in my case I cannot determinate the end of the request.

    How can I solve it?
    Last edited by newbiene; Jun 15th, 2010 at 06:09 AM.

  10. #10
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    Re: VB.net and Winsock

    Can you use events instead of polling for data?
    I wrote a book Visual Studio 2008 Programming
    Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn

  11. #11
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: VB.net and Winsock

    Quote Originally Posted by Millsy View Post
    ok thanks, yea most of the data that arrives ends with <CR> <LF>, how would I go about doing it in a more controlled manner?...this is my first .net sockets project so i'm new to all this. How would I spawn a thread for each socket and wait for all the data to arrive?
    You'd have two options as far as I can see; Read from the stream one byte at a time, and check for <CR> followed by <LF>, at which point you've received one full message. Alternatively, you can read big chunks of data from the stream at once, and perform the check for <CR><LF> in the data afterwards. It's all just several ways to skin a cat, and I'd go for the first option.
    Quote Originally Posted by newbiene View Post
    Hi, curiously I have de same problem, the serverĀ“s request arrive in two parts but in my case I cannot determinate the end of the request.
    How can I solve it?
    If the application protocol is really designed in a way such that there is no end of a request or a response, then you have to treat it as such. There is no way to know when a request or response, nor should you ever have to know.

    Quote Originally Posted by plender View Post
    Can you use events instead of polling for data?
    Since the receive calls are all blocking, you don't need to poll for data. The method will call the blocking thread until it has received data, or an error occured.
    Being as such, raising event when data has been received is no harder than raising an event directly after the call to the receive method.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width