I need to pass an argument in a function to connect to a server which has to contain the xml(which I am passing through a string variable). But the server assumes that first 4 bytes of the data stream which I am sending is the length of the xml (in bytes). I have found out the length of the number of byte in the string by
int strlen= System.Text.ASCIIEncoding.Unicode.GetByteCount(ngconnect);
Now the next thing is I need to pass the data to the server which has to contain the data as
First 4 bytes as the length of the string Then the actual data stream.
The code which I have written is
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(strlen+ngconnect);
serverStream.Write(outStream, 0, outStream.Length);
Which I dont think is correct. So please do tell me how should I append the length of the string in the argument such that it is exactly of 4 bytes.
The int
in c# is a 32bit number so its already 4bytes, you just need to convert it to bytes
byte[] strlenBytes = BitConverter.GetBytes(strlen);
and then transmit the info to the server in parts:
serverStream.Write(strlenBytes , 0, strlenBytes .Length);
byte[] outStream = System.Text.Encoding.ASCII.GetBytes(ngconnect);
serverStream.Write(outStream, 0, outStream.Length);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加