473,499 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

23 New Member
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 4127
Plater
7,872 Recognized Expert Expert
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

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

Similar topics

7
2229
by: Andrew | last post by:
Hello, Is it possible for a file position indicator of an input stream to be greater than the size of a file? And if so could this cause fgetc to somehow write to the file? For example is it...
4
2478
by: QQ | last post by:
Hello I am a newbie on network programming. I am trying to receive a packet if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct sockaddr*)&register_addr, &addr_len))==-1){ fprintf(stderr,...
12
16879
by: David Sworder | last post by:
Hi, I'm writing an application in which a client (C#/WinForms) and server (C#/service) interact with one another. The client establishes a "session" with the server but for scalability reasons...
3
9493
by: Nick | last post by:
I have found a class that compresses and uncompresses data but need some help with how to use part of it below is the deflate method which compresses the string that I pass in, this works OK. At...
8
7226
by: junk5 | last post by:
Hi I need to read raw 16 bit data from a file, where the first byte is the most significant byte of the first data value and the second byte is the least significant byte of the first data value...
7
2376
by: owolablo | last post by:
Hi, I'm writing a program that sends 1500 bytes of data at once, from the client to the server. However, the packet is broken down into 3 different segments of 500 each before getting to the...
9
2542
by: rattan | last post by:
I want a specific packet format for packet exchange between a client server across the network. For example frame format as a python class could be: class Frame: def __init__(self, buffer=None,...
1
2504
by: Diego F. | last post by:
Hello. I'm having problems reading data from a socket. In my test, I see the maximum size to get data from the socket is 8192 bytes. In my tests, if I send less that 8192, the readen data have...
4
7041
by: Zytan | last post by:
This may be the dumbest question of all time, but... When I set the packet size, does it mean ALL packets are that size, no matter what? Let's say the packet size is 8KB, and I send a 5 byte...
0
7130
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7007
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5468
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
664
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.