[...]
byteReadStream = new byte[tcpc.Available]; //allocate
space
for data
tcpc.GetStream().Read(byteReadStream, 0, tcpc.Available);
//read data into byte array
Console.WriteLine(Encoding.Default.GetString(byteReadStream)
+ "\n"); Write data to console buffer
This is wrong, for at least a couple of reasons.
The most significant one is that TCP does not guarantee that all of the
data sent will be read in a single call to Read(). You _must_ iterate the
read in some way (loop, async i/o, whatever) until you know for sure that
you've read all of the data. This could be as simple as reading
everything until the end of the stream (connection is closed), or more
complicated as in defining a fixed-length for the data, sending a length
before the string, or using some sort of delimiter/terminator (e.g. null
character).
The other issue is that the Available property may change between the time
one allocates the buffer and the time one calls Read(). If it increases,
then the buffer length passed to Read() is larger than the buffer itself
actually is, which will produce an error. At the very least, one should
be passing "byteReadStream.Length" as the buffer length, not some other
value. Preferable would be to allocate the buffer as some fixed size,
rather than anticipating the size of the read. What size is appropriate
depends on your application protocol, but for larger transfers, a 4K or 8K
buffer would be appropriate.
IMHO it would be better for the OP to simply read the docs for
TcpListener, etc. and take advantage of the code samples there. If he
still has questions after looking at the documentation, then we can answer
those in a more specific, directed way.
Pete