Results 1 to 13 of 13

Thread: VS 2012 Express: Dim bytesFrom(10024) As Byte

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    VS 2012 Express: Dim bytesFrom(10024) As Byte

    I am attempting to teach myself some new techniques with VB.NET. which will be a TCP/IP Server/Client setup...and no it will not be a chat application. It will allow a client to fill out a form, transmit the form data to the server for processing. In most of the examples I'm studying I see this line repeated:

    Code:
    Dim bytesFrom(10024) As Byte
    So can anyone explain to me in simple terms what this is actually doing. I know what Dim command does but the number in the parenthesis is what I need more info on. The string I'm sending will consist of 36 fields, mostly 1, 0, True, etc... and a few with integers, a few with short strings. I will concatentate them all together with the | delimiter and process it on the server end. I'm concerned that this number may need to be changed if my app grows to include more information in the data stream.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    The number is the zero based size of the array. So

    Dim buffer(15) As Byte

    creates an array of 16 bytes (0-15).
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    I wish you luck with a TCP server/client if you don't know what that line is doing because I would say that it's going to be a challenge. Firstly, I don't think the number would be 10024. It might be 1024 but it should be 1023. That line is creating a Byte array to use as a buffer, i.e. somewhere to read the data into that is received from the other end of the connection. 1024 bytes is a binary kilobyte, so that's a nice round figure to use as a block size for receiving data. If you wanted to use 10 kilobytes as the buffer size then the number should be 10239. Because creating an array in VB.NET requires you to specify the upper bound, if you want an array of 1024 elements then you specify an upper bound of 1023. If you want an array with length 10240 then you specify 10239.

    If you don't need a buffer that size then specify a different size. That said, unless the data will be exactly the same size every time, the buffer is not necessarily supposed to hold all the data. The data can be bigger than the buffer size or it can be smaller. Properly written code can handle both.

    Also, I think that it's a bad idea to use those pipes to delimit the data. I would suggest that the best way would be to define a type with a property for each value, create an instance of that type and populate its properties and then serialise it straight to the NetworkStream. You can read the data at the other end and deserialise it straight back to an object and get the data from its properties.

  4. #4

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Firstly, I don't think the number would be 10024. It might be 1024 but it should be 1023.
    This was exactly my thinking. But I swear, I looked at several different tutorials on this subject and this was the exact syntax being used. I thought the number of 10,024 was rather strange. That is why I asked the question. I will look into the serialization of the data stream, but the | delimiter was real easy to do and I have it working. This is not something I want to make into a complex operation so I may stick with it in the end. But thanks for the insight and I will look at that option.

  5. #5

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Ok, so I tried messing with the array size only to find that the code will not work unless I use something over 8,184 (multiples of 1023). When I used 9,207 or above it will work. Here is this very simple setup which I am trying to learn from and build on.

    SERVER SIDE:
    Code:
    Imports System.Net.Sockets
    Imports System.Text
    Module Module1
        Sub Main()
            Dim serverSocket As New TcpListener(8888)
            Dim requestCount As Integer
            Dim clientSocket As TcpClient
            serverSocket.Start()
            msg("Server Started")
            clientSocket = serverSocket.AcceptTcpClient()
            msg("Accept connection from client")
            requestCount = 0
    
            While (True)
                Try
                    requestCount = requestCount + 1
                    Dim networkStream As NetworkStream = _
                            clientSocket.GetStream()
                    Dim bytesFrom(9207) As Byte
                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    Dim dataFromClient As String = _
                            System.Text.Encoding.ASCII.GetString(bytesFrom)
                    dataFromClient = _
                dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                    msg("Data from client -  " + dataFromClient)
                    Dim serverResponse As String = _
                        "Server response " + Convert.ToString(requestCount)
                    Dim sendBytes As [Byte]() = _
                        Encoding.ASCII.GetBytes(serverResponse)
                    networkStream.Write(sendBytes, 0, sendBytes.Length)
                    networkStream.Flush()
                    msg(serverResponse)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
    
            clientSocket.Close()
            serverSocket.Stop()
            msg("exit")
            Console.ReadLine()
        End Sub
    
        Sub msg(ByVal mesg As String)
            mesg.Trim()
            Console.WriteLine(" >> " + mesg)
            'Dim fieldz As String() = mesg.Split("|")
            'Dim z As String
            'For Each z In fieldz
            '    Console.WriteLine(z)
            'Next
        End Sub
    End Module

    CLIENT SIDE:
    Code:
    Imports System.Net.Sockets
    Imports System.Text
    
    Public Class Form1
    
        Dim clientSocket As New System.Net.Sockets.TcpClient()
        Dim serverStream As NetworkStream
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim serverStream As NetworkStream = clientSocket.GetStream()
            Dim outStream As Byte() = _
            System.Text.Encoding.ASCII.GetBytes("UserName|APD|48|48|0|0|0|True|1|1|1|None|0|0|1|0|Double|1|1|12|12$")
            serverStream.Write(outStream, 0, outStream.Length)
            serverStream.Flush()
    
            Dim inStream(9207) As Byte
            serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
            Dim returndata As String = _
            System.Text.Encoding.ASCII.GetString(inStream)
            msg("Data from Server : " + returndata)
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load
            msg("Client Started")
            clientSocket.Connect("10.0.0.110", 8888)
            Label1.Text = "Client Socket Program - Server Connected ..."
        End Sub
    
        Sub msg(ByVal mesg As String)
            TextBox1.Text = TextBox1.Text + Environment.NewLine + " >> " + mesg
        End Sub
    End Class
    The size and contents of the data stream I'm sending is not in it's final form yet. But this does work nicely between two Win7 machines I have on my LAN. And it's giving me a good starting point to begin understanding the process.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Quote Originally Posted by Vladamir View Post
    This was exactly my thinking. But I swear, I looked at several different tutorials on this subject and this was the exact syntax being used. I thought the number of 10,024 was rather strange. That is why I asked the question. I will look into the serialization of the data stream, but the | delimiter was real easy to do and I have it working. This is not something I want to make into a complex operation so I may stick with it in the end. But thanks for the insight and I will look at that option.
    That's weird. Maybe there is one but I can't think of any reason to pick that number. Maybe someone made a typo and used 10024 instead of 1024 and everyone else just copied.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Quote Originally Posted by Vladamir View Post
    Ok, so I tried messing with the array size only to find that the code will not work unless I use something over 8,184 (multiples of 1023). When I used 9,207 or above it will work. Here is this very simple setup which I am trying to learn from and build on.
    The code will work no matter what buffer size you use, as long as you write the code properly. As the name suggests, the buffer is just a buffer. It's a place to catch the data as it comes in from the stream but it is not the final resting place of the data. In many cases you can't know what size the data will be that you're going to receive and you can't just make the buffer infinitely large to cater for every possibility. The idea is that is that you read the data into the buffer as it's received and then you transfer it somewhere else. If the entire data is larger than the buffer then you will need to make several reads in order to get all the data. Each read goes into the buffer first and then gets appended to the final data elsewhere, so the contents of the buffer gets overwritten every time but you're not losing the data from the previous read because you have copied it elsewhere. Where that other location is depends on the situation. Maybe the data is pure binary and you use a List(Of Byte) that you can Add the buffer contents to each time. Maybe the data is text and you use a StringBuilder, so you can use Encoding.GetString on the buffer contents and Append it to the StringBuilder. Maybe it's something else entirely.

  8. #8

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    The code will work no matter what buffer size you use, as long as you write the code properly.
    That would be understandable but when I set both the client and the server with anything less than 9,207 the following error message shows up on the server when I start the client. That is I can start the server side, but as soon as I start the client side this error window pops up:

    System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    Parameter name: size at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at TCPServer_01_.Module1.Main in ....
    and then when I click the button on the client system I get this error message:

    An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.dll
    Additional information: Specified argument was out of the range of valid values.
    The program '[3840] TCPClient.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).
    If I use 9,207 or larger, and again I make sure this number matches on both sets of code(Client and Server) then all is well.

    I also need to study further how to let the server know when the client exits as when I close the client before the server, which is what will be happening, the server side reports another error window.

    And in this project, the stream will be limited to a fixed number of answers. Cannot say it will always be the same number of bytes, but it will be in a very narrowed down range as most of the 36 answers requested in the User Form will be 0 or 1, with only a few strings being of a very limited length and some integer which will all fall in the range between 11 and 194. This project is not only to learn something new, it's hopefully to replace the FileSystemWatcher method I'm using now on this app. The trouble with FSW is that it will only watch Windows folders and the servers at my client's office are Novell. And FSW will not fire while watching folders on a Novell server.
    Last edited by Vladamir; Nov 25th, 2012 at 12:57 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    What size was the Byte array and what values did you pass to the 'offset' and 'size' parameters? 'offset' should be zero and and 'size' should be the length of the array. If you're getting an ArgumentOutOfRangeException then presumably you specified values that would take the data beyond the bounds of the array. You can't read more data than you have space for in the buffer.

  10. #10

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Very good questions, but let me see if I can answer them.

    The error message does not show up in when the datastream is sent. It appears on the server side when the client is merely started. I would think it's just sending a connection signal. Anyway, if I set the number in that part of the code to anything less than 9207, the server will crash. The client is ok but the server gives me that error message I reported earlier. If I set the number to 9207 or higher it all works and I can send the string shown in the Client code. The server receives it and parses it just like I want it to. But I don't want to proceed with the next step until I understand this part of it.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    You didn't actually answer my question. Look at the second quote in post #8. It says that NetworkStream.Read is being called in your Main method. When you make that call and the exception is thrown:
    What size was the Byte array and what values did you pass to the 'offset' and 'size' parameters?

  12. #12

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Sorry, but my understanding of this is still infantile. The error message comes up on the server side the exact second the client is started. The clients sends nothing that I know of at that point other than the signal to make the connection. Not until you click the button does the client send the text I'm wanting to transfer. As far as the byte array and the values passed to it, if it's not shown in the code above I don't know how to answer that.

  13. #13
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: VS 2012 Express: Dim bytesFrom(10024) As Byte

    Quote Originally Posted by Vladamir View Post
    which will be a TCP/IP Server/Client setup...and no it will not be a chat application. It will allow a client to fill out a form, transmit the form data to the server for processing.
    I'm not sure this is necessarily the best application to learn TCP level communication on, as it's not a realistic scenario. This would be better suited to using HTTP level communication rather than directly using TCP.

    For something more realistic, how about trying something like a game, where the client is communicating the player actions back to the server, and the server updates the client with other clients' updates/updates from the game itself. Or something else that requires a stream of information, rather than a request/response document oriented communication.

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