我正在开发TCP / IP服务,以读取GPS(TK 103)发送的数据。这是我在网上找到的一个示例后编写的代码:
static void Main(string[] args)
{
TcpListener server = null;
HttpClient httpClient = new HttpClient();
try
{
Int32 port = 20222;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(port);
server.Start();
string data = "";
while (true)
{
Console.WriteLine("Server is listening on " + server.LocalEndpoint);
Console.WriteLine("Waiting for a connection...");
var client = server.AcceptTcpClient();
Console.WriteLine("Connected...");
NetworkStream stream = client.GetStream();
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
int bytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize);
data = Encoding.ASCII.GetString(bytesToRead, 0, bytesRead);
Console.WriteLine(data);
}
}
}
从理论上讲,这应该返回以下内容: ##,imei:xxxxx,A;
但这是返回的内容: xx \ r \ u0001 \ u0003Xh?\ 0U \ u0016 \ b \ 0 \ u001e ?? \ r \ n
我尝试了不同的编码,但是没有用。