读取从特定端口C#接收的数据

时间:2018-07-31 08:20:06

标签: javascript c# asp.net serial-port port

iam使用'traccar'读取从端口5027接收的数据并将其插入我的数据库中,我想知道是否存在一种使用C#读取从特定端口(在我的情况下为5027)接收的数据的方法?

谢谢您的帮助

2 个答案:

答案 0 :(得分:0)

如果使用HTTPHTTPS协议接收消息时,如果我对您的理解正确,则可以使用HTTPListener作为服务器,也可以使用{{1} }-如果是客户。如果使用其他协议进行通信,则可以使用较低级别的api:HTTPClient / TcpListener

要获得更完整的答案,请解释,您是否希望从外部服务器读取数据,为此您需要连接到其端口5027? 还是要成为服务器,监听端口并处理接收到的数据?

参考文献:

答案 1 :(得分:0)

希望这会对您有所帮助

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace MyTcpListener
    {
        interface ITcpListener : IDisposable
        {
            event ConnectionCreatedHandler OnConnectionCreated;
            event DataReceivingHandler OnDataReceiving;
            event DataReceivedHandler OnDataReceived;
            event ResponseSendingHandler OnResponseSending;
            event ResponseSentHandler OnResponseSent;
            event ErrorHandler OnError;

            object Tag { get; set; }

            void StartListening(string host, int port);
            void Dispose();
        }
    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Net;
    using System.Net.Sockets;

    namespace MyTcpListener
    {
        public delegate void ConnectionCreatedHandler(object sender, TcpConnectionEventArgs e);
        public delegate void DataReceivingHandler(object sender, TcpConnectionEventArgs e);
        public delegate void DataReceivedHandler(object sender, TcpConnectionEventArgs e);
        public delegate void ResponseSendingHandler(object sender, TcpConnectionEventArgs e);
        public delegate void ResponseSentHandler(object sender, TcpConnectionEventArgs e);
        public delegate void DataSendingHandler(object sender, TcpConnectionEventArgs e);
        public delegate void DataSentHandler(object sender, TcpConnectionEventArgs e);
        public delegate void ErrorHandler(object sender, string message);

        class TcpListener : ITcpListener
        {
            public event ConnectionCreatedHandler OnConnectionCreated;
            public event DataReceivingHandler OnDataReceiving;
            public event DataReceivedHandler OnDataReceived;
            public event ResponseSendingHandler OnResponseSending;
            public event ResponseSentHandler OnResponseSent;
            public event ErrorHandler OnError;

            public ManualResetEvent processCompleted = new ManualResetEvent(false);

            public HubTcpListener()
            {

            }
            public object Tag { get; set; }

            public void StartListening(string host, int port) 
            {
                int bufferSize = 1024 * 1024 * 1;
                byte[] buffer = new byte[bufferSize];

                bool keppAlive = true;
                TcpConnectionEventArgs tcpServerConnectionEventArgs = new TcpConnectionEventArgs();
                IPAddress[] ipAddreses = Dns.GetHostAddresses(host);
                IPEndPoint ipEndpoint = new IPEndPoint(ipAddreses[0], port);
                Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                try
                {
                    listenerSocket.Bind(ipEndpoint);
                    listenerSocket.Listen(500);

                    tcpServerConnectionEventArgs.HandlerSocket = listenerSocket;
                    tcpServerConnectionEventArgs.ReceivedTime = DateTime.Now;

                    if (OnConnectionCreated != null) 
                    {
                        OnConnectionCreated(this, tcpServerConnectionEventArgs);
                    }

                    while (keppAlive) 
                    {
                        try
                        {
                            processCompleted.Reset();
                            listenerSocket.BeginAccept(new AsyncCallback(HandleAcceptCallBack), listenerSocket);
                            processCompleted.WaitOne();
                        }
                        catch (Exception ex)
                        {
                            if (OnError != null) 
                            {
                                OnError(this, ex.Message);
                            }
                        }
                    }
                }
                catch (Exception)
                {
                throw;
                }
            }
            private void HandleAcceptCallBack(IAsyncResult asyndResult) 
            {
                processCompleted.Set();
                Socket listener = (Socket)asyndResult.AsyncState;
                Socket handler = listener.EndAccept(asyndResult);
                StateObject stateObject = new StateObject() { clientSocket = handler };

                TcpConnectionEventArgs tcpServerConnectionEventArgs = new TcpConnectionEventArgs();
                tcpServerConnectionEventArgs.ReceivedTime = DateTime.Now;
                tcpServerConnectionEventArgs.HandlerSocket = handler;
                tcpServerConnectionEventArgs.ReceivedData = "";

                if (OnDataReceiving != null)
                {
                    OnDataReceiving(this, tcpServerConnectionEventArgs);
                }
                handler.BeginReceive(stateObject.buffer, 0, StateObject.bufferSize, 0, new AsyncCallback(HandleReadCallBack), stateObject);
            }
            private void HandleReadCallBack(IAsyncResult asyncResult) 
            {

                string contents = string.Empty;
                StateObject state = (StateObject)asyncResult.AsyncState;
                Socket handler = state.clientSocket;
                int bytesLength = handler.EndReceive(asyncResult);

                TcpConnectionEventArgs tcpServerConnectionEventArgs = new TcpConnectionEventArgs();
                tcpServerConnectionEventArgs.ReceivedTime = DateTime.Now;
                tcpServerConnectionEventArgs.HandlerSocket = handler;

                if (bytesLength > 0)
                {
                    state.stringBuilder.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesLength));
                    contents = state.stringBuilder.ToString();
                    tcpServerConnectionEventArgs.ReceivedData = contents; ;
                    if (OnDataReceived != null)
                    {
                        OnDataReceived(this, tcpServerConnectionEventArgs);
                    }
                    Send(handler, contents);
                }
                else 
                {
                    handler.BeginReceive(state.buffer, 0, StateObject.bufferSize, 0, new AsyncCallback(HandleReadCallBack), state);
                }
            }
            private void Send(Socket handler, string data) 
            {
                TcpConnectionEventArgs tcpServerConnectionEventArgs = new TcpConnectionEventArgs();
                tcpServerConnectionEventArgs.ReceivedTime = DateTime.Now;
                tcpServerConnectionEventArgs.HandlerSocket = handler;
                tcpServerConnectionEventArgs.ReceivedData = data;

                if (OnResponseSending != null)
                {
                    OnResponseSending(this, tcpServerConnectionEventArgs);
                    data = tcpServerConnectionEventArgs.ReceivedData;
                }

                byte[] bytes = Encoding.ASCII.GetBytes(data);
                handler.BeginSend(bytes, 0, bytes.Length, 0, new AsyncCallback(HandleSendCallBack), handler);
            }
            private void HandleSendCallBack(IAsyncResult asyncResult) 
            {
                try
                {
                    Socket handler = (Socket)asyncResult.AsyncState;
                    int bytesSendLength = handler.EndSend(asyncResult);

                    TcpConnectionEventArgs tcpServerConnectionEventArgs = new TcpConnectionEventArgs();
                    tcpServerConnectionEventArgs.ReceivedTime = DateTime.Now;
                    tcpServerConnectionEventArgs.HandlerSocket = handler;

                    if (OnResponseSent != null)
                    {
                        OnResponseSent(this, tcpServerConnectionEventArgs);
                    }
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                }
                catch (Exception)
                {
                    throw;
                }
            }
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    if (this.processCompleted != null)
                    {
                        this.processCompleted.Dispose();
                    }
                }
            }
        }
    }



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.Net.Sockets;

    namespace MyTcpListener
    {
        public class TcpConnectionEventArgs : EventArgs
        {
            public Socket HandlerSocket { get; set; }
            public DateTime ReceivedTime { get; set; }
            public string ReceivedData { get; set; }
            public long TransactionID { get; set; }
            public long ConnectorID { get; set; }
        }
    }

    namespace MyTcpListener
    {
        public class StateObject
        {
            public Socket clientSocket = null;
            public const int bufferSize = (1024 * 1024) * 1; // 1 MB
            public byte[] buffer = new byte[bufferSize];
            public StringBuilder stringBuilder = new StringBuilder();
        }
    }