469,903 Members | 1,806 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Check Packet Size of TCP Data Before Reading Next Byte in Stream

Hi :D,
I am sending data to server via TCP IP Connection. I am using a continuous loop at the server end - that accepts new clients and while streams can be read, it reads data stream.

The data is sent from the client with 2 leading bytes of data that represent the size of the packet of data sent and type of data.

My question is: how do I retrieve the size of the data packet and then check that this amount of data has been retrieved?

Thanks :)

Q

PS:

Client:
Expand|Select|Wrap|Line Numbers
  1.         Dim Enc As Encoding = Encoding.GetEncoding("utf-32")
  2.  
  3.         Dim UserNameText As String = UserName.Text
  4.  
  5.         Dim UserNameTextLength = Enc.GetByteCount(UserNameText)
  6.  
  7.         Dim UserNameByte As Byte() = New Byte(UserNameTextLength + 1) {}
  8.         Enc.GetBytes(UserNameText, 0, UserNameText.Length, UserNameByte, 2)
  9.  
  10.         UserNameByte(0) = 99 'Packet Size
  11.  
  12.         UserNameByte(1) = 88 'Packet Type
  13.  
  14.         Dim Client As New TcpClient("192.168.1.65", 65535)
  15.  
  16.         Dim Stream As NetworkStream = Client.GetStream
  17.  
  18.         Dim Chars As Char() = Enc.GetChars(UserNameByte)
  19.  
  20.         MessageBox.Show(Chars)
  21.  
  22.         Stream.Write(UserNameByte, 0, UserNameByte.Length)
  23.  
Server: I would like to retrieve the packet size and then retrieve that size of data.
Expand|Select|Wrap|Line Numbers
  1.         While Listener.Pending = False
  2.             Client = Listener.AcceptTcpClient()
  3.             Dim Stream As NetworkStream = Client.GetStream()
  4.  
  5.             While Stream.CanRead
  6.  
  7.                 Dim UserNameByte(Client.ReceiveBufferSize) As Byte
  8.                 Stream.Read(UserNameByte, 0, CInt(Client.ReceiveBufferSize))
  9.  
  10.                 Dim UserNameStringByte(Client.ReceiveBufferSize - 2) As Byte
  11.  
  12.                 Array.Copy(UserNameByte, 2, UserNameStringByte, 0, UserNameByte.Length - 2)
  13.  
  14.                 Dim Enc As Encoding = Encoding.GetEncoding("utf-32")
  15.                 Dim Chars As Char() = Enc.GetChars(UserNameStringByte)
  16.  
  17.                 MsgBox(Chars)
  18.  
  19.             End While
  20.  
  21.         End While
  22.  
May 25 '09 #1
1 3782
Plater
7,872 Expert 4TB
Assuming your server is at the start of waiting for a message.
Read in 2 bytes. Convert that into the correct number. Check to see if that many bytes is available to read. Based on that result, either read or continue to wait?
May 27 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

12 posts views Thread by David Sworder | last post: by
8 posts views Thread by junk5 | last post: by
7 posts views Thread by owolablo | last post: by
9 posts views Thread by rattan | last post: by
4 posts views Thread by Zytan | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.