Solved

System.Net.Sockets Read Unknown size of bytes

Posted on 2008-06-25
2
853 Views
Last Modified: 2012-05-05
In an application I read data from the System.Net.Sockets.TCPClient buffer, but because I don't know the exact bytes to read I am having problems. The read function fires a timeout. Is there a way to read unknown bytes?
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.ReadTimeout = 20000
networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

Open in new window

0
Comment
Question by:emresamisuzer
2 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 100 total points
ID: 21870850
Read(), will return the number of bytes counted, so if you have a return value less than 20000, you have reached the end of the stream.
Also you can check the .DataAvailable property before trying to read.
0
 
LVL 11

Accepted Solution

by:
melmers earned 400 total points
ID: 21872095
When you have an unknown number of bytes you can read them byte per byte from the stream
and add the byte to an array of bytes. when no bytes are on the line then you have reached the end.

The other way is to read the data async with .BeginRecive and .EndRecive when you call .EndRecive
you get the number of readed bytes as a long value.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question