I can read a string of bytes via UDP but cannot convert them to a string of integers.
try {
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
// Blocks until a message returns on this socket from a remote host.
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
// Uses the IPEndPoint object to determine which host responded
Console.WriteLine("Received: \n\n " +
returnData.ToString());
Console.WriteLine("\nSource:\n\n " +
"IP: " + RemoteIpEndPoint.Address.ToString() +
"\n Port: " +
RemoteIpEndPoint.Port.ToString());
//split string by delimiter
string[] arrString = returnData.Split(new char[] { ',' });
int[] data = new int[arrString.Length];
for (int i = 0; i < arrString.Length; i++)
{
data[i] = int.Parse(arrString[i]);
}
System.Threading.Thread.Sleep(500000000);
// udpClient.Close();
}
catch (Exception e ) {
Console.WriteLine("\nError: \n");
Console.WriteLine(e.ToString());
System.Threading.Thread.Sleep(500000000);
};
How to: Convert a byte Array to an int (C# Programming Guide)
byte[] bytes = { 0, 0, 0, 25 };
// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
Array.Reverse(bytes);
int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
IPAddress
类具有一些帮助程序方法,可以在网络和主机顺序之间来回转换字节,即: HostToNetworkOrder和NetworkToHostOrder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.