Results 1 to 5 of 5

Thread: [RESOLVED] Determining count for readbytes function of Binaryreaders

  1. #1

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Resolved [RESOLVED] Determining count for readbytes function of Binaryreaders

    Hi,

    I was trying to make a client/server application. Server listens at a specific port for an incoming connection and if there is a connection it accepts is and asks for a password. If password is correct then they simply wait for some "string" from eachother. everything is fine if I send and get data in string format. there is no problem at all!

    (By the way I am using simple usual TcpListener/TcpClient, Networkstream, Binaryreader/writer, etc...

    I decided to add file transfer option. well for that i needed to transfer data in byte arrays. Because of this I decided to make all data traffic in byte arrays. So I converted strings to arrays and arrays to string in between. In case of file transfer, file chunks are being sent with a cap like "[cap]"<data chunk>. This was my idea. On the other side there is a sub looking what is coming and what does it mean by looking its cap. In case of file transport specific cap, cap will be removed and data is being added to previous ones...

    My problem was, that my binaryreader: reader reads incoming data without any parameter:
    Code:
    1. message=reader.readstring

    But in case of readbytes i have to put a parameter called count.
    Well I don't know how much bytes are going to come until I get them. In case of TcpClient I can get it with: Client.GetStream.length.

    But how can I get the same result with TcpListener? (At the server)

    Can I transfer chunks of file bytes in string format and convert them back to bytearrays without any loss or problem?

    Thanks in advance...
    Dim Me As Coder

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determining count for readbytes function of Binaryreaders

    You need to design a protocol. Every communication between client and server in either direction should start with a header. That header should contain data indicating the type and size of data in the message. Your header might consist of 9 bytes, with the first byte containing a number representing the type of the message and the last 8 bytes containing a number representing the size. Your receiver will then will look for a message header, read it, interpret it and then read the message based on that. If the header contains the data type for file and the number 1024, the receiver knows that it needs to read 1024 bytes and those bytes represent a file. You may want to add more fields to the header if you need more information, e.g. file name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: Determining count for readbytes function of Binaryreaders

    Hey, thanks for your answer... Did I understand correct? I am going to look for 9 bytes with read.readbytes(9) and interpret it with a code and then as you said read.readbytes(dSize). (dSize means datasize retreived from header)
    Is this correct so far?
    Dim Me As Coder

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Determining count for readbytes function of Binaryreaders

    If you header did indeed consist of a single byte to indicate message type and then 8 bytes to indicate data length, you'd call ReadByte to get the message type and then ReadInt64 to get the data length.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: Determining count for readbytes function of Binaryreaders

    I got it thanks...
    Last edited by Genom; Feb 9th, 2011 at 05:21 AM.
    Dim Me As Coder

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