我正在使用一个连接到运行gpsd服务的树莓pi的gps。我正试图使用tcp连接到该服务,但无法使其正常工作。我也找不到任何关于它的文件。
这是我现在的代码:
private static void Main(string[] args)
{
Console.WriteLine($"Trying to connect to {ServerAddress}...");
var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ServerAddress, 80);
var result = new byte[256];
client.Receive(result);
Test();
}
有人能告诉我它是如何完成的吗?或者给我一个文档或c#示例的链接。
看看这个来源中关于C示例的部分。
核心C客户端是一个套接字接收器。
如果你需要一个功能齐全的客户,我在最初的评论中建议解决方法
你可以在Raspberry Pi上使用标准客户端,并构建一个新的Raspberry上的网络服务,您可以在从c#"的标准方式
C#侦听器
否则,您可以尝试只创建一个基本的C#侦听器:遵循msdnHow-to。
public void createListener()
{
// Create an instance of the TcpListener class.
TcpListener tcpListener = null;
IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];
try
{
// Set the listener on the local IP address
// and specify the port.
tcpListener = new TcpListener(ipAddress, 13);
tcpListener.Start();
output = "Waiting for a connection...";
}
catch (Exception e)
{
output = "Error: " + e.ToString();
MessageBox.Show(output);
}
while (true)
{
// Always use a Sleep call in a while(true) loop
// to avoid locking up your CPU.
Thread.Sleep(10);
// Create a TCP socket.
// If you ran this server on the desktop, you could use
// Socket socket = tcpListener.AcceptSocket()
// for greater flexibility.
TcpClient tcpClient = tcpListener.AcceptTcpClient();
// Read the data stream from the client.
byte[] bytes = new byte[256];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
SocketHelper helper = new SocketHelper();
helper.processMsg(tcpClient, stream, bytes);
}
}
整个详细的实施将超出这里的范围。
旁注
请记住,循环时需要睡眠。
可能更简单的解决方案
然而,乍一看,利用本机客户端绑定似乎是更容易的方法。
例如,gps_read()
被描述为
阻止从后台进程读取数据。
这里的想法是从C#调用C++包装器,并按照另一个答案