【发布时间】:2015-09-06 05:07:54
【问题描述】:
[![Header][1]][1]我正在做一个小项目来获取经纬度
能否请您告诉我如何从数据中提取真正的纬度和经度值。
using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Linq;
namespace SimpleUdpReciever
{
class pProgram
{
static void Main(string[] args)
{
int localPort = 18006;
IPEndPoint remoteSender = new IPEndPoint(IPAddress.Any, 0);
bool flag = false;
for (int i = 0; i < args.Length; i++)
{
string cmd = args[i];
string value;
int tempInt;
IPAddress tempAddress;
switch (cmd)
{
case "-lp":
value = GetValue(args, ref i);
if (int.TryParse(value, out tempInt))
localPort = tempInt;
break;
case "-rp":
value = GetValue(args, ref i);
if (int.TryParse(value, out tempInt))
remoteSender.Port = tempInt;
break;
case "-rh":
value = GetValue(args, ref i);
if (IPAddress.TryParse(value, out tempAddress))
remoteSender.Address = tempAddress;
else if (int.TryParse(value, out tempInt) && tempInt == 0)
remoteSender.Address = IPAddress.Any;
break;
case "-?":
default:
PrintHelpText();
flag = true;
break;
}
}
// Exit application after help text is displayed
if (flag)
return;
// Create UDP client
UdpClient client = new UdpClient(localPort);
UdpState state = new UdpState(client, remoteSender);
// Start async receiving
client.BeginReceive(new AsyncCallback(DataReceived), state);
// Wait for any key to terminate application
Console.ReadKey();
client.Close();
}
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
private static void DataReceived(IAsyncResult ar)
{
UdpClient c = (UdpClient)((UdpState)ar.AsyncState).c;
IPEndPoint wantedIpEndPoint = (IPEndPoint)((UdpState)(ar.AsyncState)).e;
IPEndPoint receivedIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
Byte[] receiveBytes = c.EndReceive(ar, ref receivedIpEndPoint);
bool isRightHost = (wantedIpEndPoint.Address.Equals(receivedIpEndPoint.Address)) || wantedIpEndPoint.Address.Equals(IPAddress.Any);
bool isRightPort = (wantedIpEndPoint.Port == receivedIpEndPoint.Port) || wantedIpEndPoint.Port == 0;
if (isRightHost && isRightPort)
{
// Console.WriteLine(ByteArrayToString(receiveBytes));
if (receiveBytes[3] == 187 && receiveBytes[4] == 1)
{
//Printing output
Console.WriteLine("4 bytes starting with 5 yields: {0} {1} {2} {3} {4}",
receiveBytes[5],
receiveBytes[6],
receiveBytes[7],
receiveBytes[8],
BitConverter.ToInt32(receiveBytes, 4));
Console.WriteLine("4 bytes starting with 9 yields: {0} {1} {2} {3} {4}",
receiveBytes[9],
receiveBytes[10],
receiveBytes[11],
receiveBytes[11],
BitConverter.ToInt32(receiveBytes, 9));
}
}
// Restart listening for udp data packages
c.BeginReceive(new AsyncCallback(DataReceived), ar.AsyncState);
}
private static string GetValue(string[] args, ref int i)
{
string value = String.Empty;
if (args.Length >= i + 2)
{
i++;
value = args[i];
}
return value;
}
private static void PrintHelpText()
{
Console.WriteLine("Simple Udp Receiver is an application that prints incoming data to screen.");
Console.WriteLine("Data is converted to ASCII before printing.");
Console.WriteLine("*** Amund Gjersoe 2010 ***\n");
Console.WriteLine("Command switches:");
Console.WriteLine("-? : Displays this text.");
Console.WriteLine("-lp : Set local receiving port. \"-lp 4001\" Default: 11000");
Console.WriteLine("-rp : Set remote sender port. \"-rp 4001\" Default: 0 (Any port)");
Console.WriteLine("-rh : Set remote sender ip. \"-rh 192.168.1.10\" Default: 0 (Any ip)");
Console.WriteLine("\n Example of usage:\nSimpleUdpReciver.exe -lp 11000 -rh 192.168.10.10 -rp 4001");
}
}
}
包结构[![packet][4]][4]
[![1d 187 个数据包的全部数据][5]][5]
【问题讨论】:
-
您是想自己解码还是有理由不使用
System.IO.BinaryReader类? (msdn.microsoft.com/en-us/library/…)。或者您在将 byte[] 转换为流时遇到问题?提示:var stream = new System.IO.MemoryStream(receiveBytes); -
@BitTickler 我实际上遵循了这个例子并改变了我的代码,因为我的数据包数据类型是 int32。你能给我一个简单的例子如何处理这种情况。
-
为什么您希望您的值会产生
-36.2901...?也许你只需要使用ToSingle而不是ToInt32。 -
@31eee384 我更改为 Console.WriteLine("4 bytes 从 5 开始产生:{0} {1} {2} {3} {4}", receiveBytes[5], receiveBytes[6 ],receiveBytes[7],receiveBytes[8],BitConverter.ToSingle(receiveBytes, 4));但输出不正确。它现在得到 1.975823E-16 的 -36.3901 ...
-
我评论的第一部分比较重要,那么:你为什么期待你期待的价值?这些数据包是如何写入的,这些字节是否正确?
标签: c# network-programming udp udpclient