Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GPS103跟踪器在C语言中的监听应用#_C#_Sockets_Gps_Tcplistener - Fatal编程技术网

C# GPS103跟踪器在C语言中的监听应用#

C# GPS103跟踪器在C语言中的监听应用#,c#,sockets,gps,tcplistener,C#,Sockets,Gps,Tcplistener,我正在为我在C#中的GPS跟踪器开发一个基于控制台的监听应用程序,我的GPS跟踪器配置为在服务器上使用特定端口发送数据包,我的应用程序运行在该端口。现在事情是根据GPS103的协议文件进行的 它首先发送像###,1234567899121这样的字符串,这是我在应用程序中已经收到的一个,但是在收到这个字符串后,我必须从应用程序向我的GPS跟踪器发送“加载”,GPS跟踪器将再次通过发送登录成功消息进行响应 问题:我必须在收到第一个字符串后发送命令,但我从未收到GPS跟踪器的回复 namespace

我正在为我在C#中的GPS跟踪器开发一个基于控制台的监听应用程序,我的GPS跟踪器配置为在服务器上使用特定端口发送数据包,我的应用程序运行在该端口。现在事情是根据GPS103的协议文件进行的 它首先发送像
###,1234567899121这样的字符串,这是我在应用程序中已经收到的一个
,但是在收到这个字符串后,我必须从应用程序向我的GPS跟踪器发送“加载”,GPS跟踪器将再次通过发送登录成功消息进行响应

问题:我必须在收到第一个字符串后发送命令,但我从未收到GPS跟踪器的回复

namespace PakTrackingListenerApp_tk103_
{
class Program
{
    static void Main(string[] args)
    {
        TcpListener listener = new TcpListener(1000);
        System.Net.ServicePointManager.Expect100Continue = false;
        listener.Start();

        while (true)
        {
            Console.WriteLine("Waiting for a connection");
            TcpClient client = listener.AcceptTcpClient();
            StreamReader sr = new StreamReader(client.GetStream());
            StreamWriter sw = new StreamWriter(client.GetStream());

            try
            {
                string request = sr.ReadLine();
                Console.WriteLine("GPS Command" + request);
                string[] tokens = request.Split(',');

                string response = "LOAD";
                if (tokens[0] == "##")
                {
                    response = "LOAD";
                    Console.WriteLine("Token" + tokens[0]);
                }

                sw.WriteLine(response);
                sw.Flush();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception :" +e.Message);
            }
            client.Close();
        }
    }
}
}

您正在终止与
client.Close()的连接未阅读回复。在它返回空之前不要关闭

1) 设备发送
##,1234567899121,A

2) 回复
LOAD

3) 它发回
1234567899121

4) 回复
**,imei:1234567899121,B

5) 它发回跟踪数据
imei:1234567899121,跟踪器,161003171049,F,091045.000,A,1017.6730,N,07845.7982,E,0.00,0

6) 在

现在,它不断重复步骤3到6

请参阅这些链接以了解正确的用法

代码示例:

  ' Enter the listening loop. 
        While True
            Console.Write(listeningPort & " Waiting for a connection... " & vbLf)

            ' Perform a blocking call to accept requests. 
            ' You could also user server.AcceptSocket() here. 
            Dim client As TcpClient = server.AcceptTcpClient()
            Console.WriteLine(listeningPort & " Connected!")

            data = Nothing

            ' Get a stream object for reading and writing 
            Dim stream As NetworkStream = client.GetStream()
            stream.ReadTimeout = 180000

            Dim i As Int32

            ' Loop to receive all the data sent by the client.
            i = stream.Read(bytes, 0, bytes.Length)
            While (i <> 0)
                ' Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                Console.WriteLine(listeningPort & " Received: {0}", data)

                If Trim(data) <> "" Then
                    If Trim(data).StartsWith("##,") Then
                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("LOAD")
                        stream.Write(byte1, 0, byte1.Length)
                    ElseIf Trim(data).EndsWith(";") And Trim(data).Contains("imei:") = False Then
                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("**,imei:" & data.Replace(";", ",B;"))
                        stream.Write(byte1, 0, byte1.Length)
                    Else

           'TODO: PROCESS THE DATA HERE

                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("ON")
                        stream.Write(byte1, 0, byte1.Length)
                    End If
                End If

                Try
                    i = stream.Read(bytes, 0, bytes.Length)
                Catch ex As IOException     'Added For the Timeout, End Connection and Start Again
                    If ex.Message.Contains("did not properly respond after a period of time") Then
                        'WriteLog("Port: " & listeningPort & " IOException: " & ex.Message)
                        client.Close()
                        server.Server.Close()
                        server.Stop()
                    Else
                        Try
                            client.Close()
                            server.Server.Close()
                            server.Stop()
                        Catch e As Exception
                            'WriteLog("Port: " & listeningPort & " Error in Closing Port. : " & e.Message)
                        End Try
                    End If
                    GoTo finalblock
                End Try
            End While
            ' Shutdown and end connection
            client.Close()
        End While
”进入监听循环。
虽然是真的
Console.Write(ListengPort&“等待连接…”&vbLf)
'执行阻止调用以接受请求。
'您也可以在此处使用server.AcceptSocket()。
作为TcpClient=server.AcceptTcpClient()的Dim客户端
Console.WriteLine(ListingPort和“已连接!”)
数据=无
'获取用于读取和写入的流对象
作为NetworkStream=client.GetStream()的Dim流
stream.ReadTimeout=180000
Dim i As Int32
'循环以接收客户端发送的所有数据。
i=stream.Read(字节,0,字节.长度)
而(i 0)
'将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i)
WriteLine(ListingPort&“Received:{0}”,数据)
如果修剪(数据)”,则
如果Trim(data).StartsWith(“##,”),则
Dim byte1 As Byte()=System.Text.Encoding.ASCII.GetBytes(“加载”)
stream.Write(字节1,0,字节1.Length)
ElseIf Trim(data).EndsWith(;)和Trim(data).Contains(.imei:)=False然后
Dim byte1作为Byte()=System.Text.Encoding.ASCII.GetBytes(“**,imei:”&data.Replace(“;”,“,B;”)
stream.Write(字节1,0,字节1.Length)
其他的
'TODO:在此处处理数据
Dim byte1 As Byte()=System.Text.Encoding.ASCII.GetBytes(“ON”)
stream.Write(字节1,0,字节1.Length)
如果结束
如果结束
尝试
i=stream.Read(字节,0,字节.长度)
为超时添加了Catch ex As IOException,结束连接并重新启动
如果ex.Message.Contains(“在一段时间后未正确响应”),则
'WriteLog(“端口:&ListingPort&“IOException:&ex.Message”)
client.Close()
server.server.Close()
server.Stop()
其他的
尝试
client.Close()
server.server.Close()
server.Stop()
捕获e作为例外
'WriteLog(“端口:”&ListingPort&“关闭端口时出错:”&e.Message)
结束尝试
如果结束
转到最终锁定
结束尝试
结束时
'关闭和结束连接
client.Close()
结束时

这些设备的方框或手册中没有显示协议格式。很难找到协议本身。

您正在终止与
client.Close()的连接未阅读回复。在它返回空之前不要关闭

1) 设备发送
##,1234567899121,A

2) 回复
LOAD

3) 它发回
1234567899121

4) 回复
**,imei:1234567899121,B

5) 它发回跟踪数据
imei:1234567899121,跟踪器,161003171049,F,091045.000,A,1017.6730,N,07845.7982,E,0.00,0

6) 在

现在,它不断重复步骤3到6

请参阅这些链接以了解正确的用法

代码示例:

  ' Enter the listening loop. 
        While True
            Console.Write(listeningPort & " Waiting for a connection... " & vbLf)

            ' Perform a blocking call to accept requests. 
            ' You could also user server.AcceptSocket() here. 
            Dim client As TcpClient = server.AcceptTcpClient()
            Console.WriteLine(listeningPort & " Connected!")

            data = Nothing

            ' Get a stream object for reading and writing 
            Dim stream As NetworkStream = client.GetStream()
            stream.ReadTimeout = 180000

            Dim i As Int32

            ' Loop to receive all the data sent by the client.
            i = stream.Read(bytes, 0, bytes.Length)
            While (i <> 0)
                ' Translate data bytes to a ASCII string.
                data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                Console.WriteLine(listeningPort & " Received: {0}", data)

                If Trim(data) <> "" Then
                    If Trim(data).StartsWith("##,") Then
                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("LOAD")
                        stream.Write(byte1, 0, byte1.Length)
                    ElseIf Trim(data).EndsWith(";") And Trim(data).Contains("imei:") = False Then
                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("**,imei:" & data.Replace(";", ",B;"))
                        stream.Write(byte1, 0, byte1.Length)
                    Else

           'TODO: PROCESS THE DATA HERE

                        Dim byte1 As Byte() = System.Text.Encoding.ASCII.GetBytes("ON")
                        stream.Write(byte1, 0, byte1.Length)
                    End If
                End If

                Try
                    i = stream.Read(bytes, 0, bytes.Length)
                Catch ex As IOException     'Added For the Timeout, End Connection and Start Again
                    If ex.Message.Contains("did not properly respond after a period of time") Then
                        'WriteLog("Port: " & listeningPort & " IOException: " & ex.Message)
                        client.Close()
                        server.Server.Close()
                        server.Stop()
                    Else
                        Try
                            client.Close()
                            server.Server.Close()
                            server.Stop()
                        Catch e As Exception
                            'WriteLog("Port: " & listeningPort & " Error in Closing Port. : " & e.Message)
                        End Try
                    End If
                    GoTo finalblock
                End Try
            End While
            ' Shutdown and end connection
            client.Close()
        End While
”进入监听循环。
虽然是真的
Console.Write(ListengPort&“等待连接…”&vbLf)
'执行阻止调用以接受请求。
'您也可以在此处使用server.AcceptSocket()。
作为TcpClient=server.AcceptTcpClient()的Dim客户端
Console.WriteLine(ListingPort和“已连接!”)
数据=无
'获取用于读取和写入的流对象
作为NetworkStream=client.GetStream()的Dim流
stream.ReadTimeout=180000
Dim i As Int32
'循环以接收客户端发送的所有数据。
i=stream.Read(字节,0,字节.长度)
而(i 0)
'将数据字节转换为ASCII字符串。
数据=System.Text.Encoding.ASCII.GetString(字节,0,i)
WriteLine(ListingPort&“Received:{0}”,数据)
如果修剪(数据)”,则
如果修剪(数据).StartsWith(“##,”)