Click to See Complete Forum and Search --> : Byte vs byte...


tobeyu
January 14th, 2003, 10:45 AM
what is the difference with these?? I know byte is an integral data type where Byte is a structure but when they both declare an unsigned 8-bit integer, what is the difference and why would you use one vs the other?

My problem is, I am playing around with a socket application and looking at the TcpClient example in MSDN, it declares a Byte[] to hold the data to send and a byte[] to hold the data to receive. This is confusing the heck out of me and I can't figure out why the difference.

From MSDN:

TcpClient tcpClient = new TcpClient();
// Uses the GetStream public method to return the NetworkStream.
try{
NetworkStream networkStream = tcpClient.GetStream();
if(networkStream.CanWrite){
Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
networkStream.Write(sendBytes, 0, sendBytes.Length);
}
else{
Console.WriteLine("You cannot write data to this stream.");
tcpClient.Close();
return;
}
if(networkStream.CanRead){

// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[tcpClient.ReceiveBufferSize];
// Read can return anything from 0 to numBytesToRead.
// This method blocks until at least one byte is read.
networkStream.Read(bytes, 0, (int) tcpClient.ReceiveBufferSize);

// Returns the data received from the host to the console.
string returndata = Encoding.ASCII.GetString(bytes);
Console.WriteLine("This is what the host returned to you: " + returndata);
}
else{
Console.WriteLine("You cannot read data from this stream.");
tcpClient.Close();
return;
}
}
catch (Exception e ) {
Console.WriteLine(e.ToString());
}


Thanks in advance,
Tobey

hspc
January 16th, 2003, 01:19 AM
byte: keyword denotes atype that stores values from 0 to 255
Byte: a structure that have method for comparison and ...etc. and it is safe for multithreading.

tobeyu
January 16th, 2003, 07:16 AM
Thanks for the reply, but I am afraid I understand that much. My confusion is why would you use one instead of the other specifically as in the example above?

Thanks again,
Tobey

WillemM
January 18th, 2003, 12:50 AM
In case of network programming, on which you probably use threads to prevent your program from freezing when the network connection drops. It's better to use Byte[] since this one is threadsafe. If you don't use threads you can better use byte[] since this one doesn't have the extra memory-load of the thread safety and functions.

pareshgh
January 18th, 2003, 02:46 AM
the reason you have to send byte is sockets work on rpc libraries on network layers. RPC libraries were written very early days and it can't be changed dynamycally since it at very bottom level . now they expect the communication to be sent with a stream of bytes. that's why the sockets written in .NET library are expecting stream of bytes, thus your solution byte array,

another reason they can't just put string is char is 2 byte in .NET because of multi-codes or language support. ...



Paresh:o