读取套接字直到C#行结束?

时间:2010-04-16 15:36:55

标签: c# sockets

我正在尝试编写一个侦听给定端口上的TCP套接字的服务,直到收到行尾,然后根据收到的“行”执行命令。

我已经关注了c#的基本套接字编程教程,并提出了以下代码来监听套接字:

public void StartListening()
        {
            _log.Debug("Creating Maing TCP Listen Socket");
            _mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, _port);
            _log.Debug("Binding to local IP Address");
            _mainSocket.Bind(ipLocal);

            _log.DebugFormat("Listening to port {0}",_port);
            _mainSocket.Listen(10);

            _log.Debug("Creating Asynchronous callback for client connections");
            _mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);

        }

        public void OnClientConnect(IAsyncResult asyn)
        {
            try
            {
                _log.Debug("OnClientConnect Creating worker socket");
                Socket workerSocket = _mainSocket.EndAccept(asyn);
                _log.Debug("Adding worker socket to list");
                _workerSockets.Add(workerSocket);
                _log.Debug("Waiting For Data");
                WaitForData(workerSocket);

                _log.DebugFormat("Clients Connected [{0}]", _workerSockets.Count);

                _mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
            }
            catch (ObjectDisposedException)
            {
                _log.Error("OnClientConnection: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                _log.Error("Socket Exception", se);
            }
        }

        public class SocketPacket
        {
            private System.Net.Sockets.Socket _currentSocket;

            public System.Net.Sockets.Socket CurrentSocket
            {
                get { return _currentSocket; }
                set { _currentSocket = value; }
            }
            private byte[] _dataBuffer = new byte[1];

            public byte[] DataBuffer
            {
                get { return _dataBuffer; }
                set { _dataBuffer = value; }
            }


        }

        private void WaitForData(Socket workerSocket)
        {
            _log.Debug("Entering WaitForData");
            try
            {
                lock (this)
                {
                    if (_workerCallback == null)
                    {
                        _log.Debug("Initializing worker callback to OnDataRecieved");
                       _workerCallback = new AsyncCallback(OnDataRecieved);
                    }
                }
                SocketPacket socketPacket = new SocketPacket();
                socketPacket.CurrentSocket = workerSocket;
                workerSocket.BeginReceive(socketPacket.DataBuffer, 0, socketPacket.DataBuffer.Length, SocketFlags.None, _workerCallback, socketPacket);
            }
            catch (SocketException se)
            {
                _log.Error("Socket Exception", se);
            }
        }

        public void OnDataRecieved(IAsyncResult asyn)
        {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;
            try
            {

                int iRx = socketData.CurrentSocket.EndReceive(asyn);
                char[] chars = new char[iRx + 1];
                _log.DebugFormat("Created Char array to hold incomming data. [{0}]",iRx+1);

                System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
                int charLength = decoder.GetChars(socketData.DataBuffer, 0, iRx, chars, 0);
                _log.DebugFormat("Read [{0}] characters",charLength);

                String data = new String(chars);
                _log.DebugFormat("Read in String \"{0}\"",data);

                WaitForData(socketData.CurrentSocket);
            }
            catch (ObjectDisposedException)
            {
                _log.Error("OnDataReceived: Socket has been closed. Removing Socket");
                _workerSockets.Remove(socketData.CurrentSocket);

            }
            catch (SocketException se)
            {
                _log.Error("SocketException:",se);
                _workerSockets.Remove(socketData.CurrentSocket);

            }
        }

我认为这将是我想要做的事情的良好基础,但是我已经将传入的字符逐个添加到文本框中并且没有对它做任何事情。哪个对我想做的事情不起作用。

我的主要问题是OnDataReceived方法与Wait for data方法的解耦。这意味着我在构建字符串时遇到问题(我会使用字符串构建器,但我可以接受多个连接,这样就无法正常工作。

理想情况下,我希望在听到套接字时看,直到我看到行尾字符,然后调用一个方法,并将结果字符串作为参数。

这是最好的方法。

2 个答案:

答案 0 :(得分:1)

尝试使用asynch套接字。下面的代码将监听一个套接字,如果接收到通过telnet的新行char,它将回显给incomming套接字。您似乎只需要将该输入重定向到文本框。

    private string _hostName;
    private const int _LISTENINGPORT = 23;
    private Socket _incomingSocket;
    byte[] _recievedData;
    //todo: do we need 1024 byte? the asynch methods read the bytes as they come
    //so when 1 byte typed == 1 byte read. Unless its new line then it is two.
    private const int _DATASIZE = 1024;

    public ConnectionServer()
    {
        IPAddress localAddr = IPAddress.Parse("127.0.0.1");
        _hostName = Dns.GetHostName();
        _recievedData = new byte[_DATASIZE];
        _incomingSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPEndPoint endPoint = new IPEndPoint(localAddr, _LISTENINGPORT);
        _incomingSocket.Bind(endPoint);
        _incomingSocket.Listen(10);
    }
    ~ConnectionServer()
    {
    }
    public void StartListening()
    {
        _incomingSocket.BeginAccept(new AsyncCallback(OnAccept), _incomingSocket);
    }
    private void OnAccept(IAsyncResult result)
    {
        UserConnection connectionInfo = new UserConnection();
        Socket acceptedSocket = (Socket)result.AsyncState;
        connectionInfo.userSocket = acceptedSocket.EndAccept(result);
        connectionInfo.messageBuffer = new byte[_DATASIZE];
        //Begin acynch communication with target socket
        connectionInfo.userSocket.BeginReceive(connectionInfo.messageBuffer, 0, _DATASIZE, SocketFlags.None,
            new AsyncCallback(OnReceiveMessage), connectionInfo);
        //reset the listnening socket to start accepting 
        _incomingSocket.BeginAccept(new AsyncCallback(OnAccept), result.AsyncState);
    }
   private void OnReceiveMessage(IAsyncResult result)
        {
            UserConnection connectionInfo = (UserConnection)result.AsyncState;
            int bytesRead = connectionInfo.userSocket.EndReceive(result);
            if (connectionInfo.messageBuffer[0] != 13 && connectionInfo.messageBuffer[1] != 10)
            //ascii for newline and line feed
            //todo dress this up
            {
                if (string.IsNullOrEmpty(connectionInfo.message))
                {
                    connectionInfo.message = ASCIIEncoding.ASCII.GetString(connectionInfo.messageBuffer);
                }
                else
                {
                    connectionInfo.message += ASCIIEncoding.ASCII.GetString(connectionInfo.messageBuffer);
                }
            }
            else
            {
                connectionInfo.userSocket.Send(ASCIIEncoding.ASCII.GetBytes(connectionInfo.message), SocketFlags.None);
                connectionInfo.userSocket.Send(connectionInfo.messageBuffer, SocketFlags.None);
                connectionInfo.message = string.Empty;
                connectionInfo.messageBuffer = new byte[_DATASIZE];
            }


{
    public class UserConnection
    {
        public Socket userSocket { get; set; }
        public Byte[] messageBuffer { get; set; }
        public string message { get; set; }
    }
}

答案 1 :(得分:1)

您似乎有几个问题:

您有一个名为WaitForData的异步方法。这非常令人困惑,因为名称中带有单词Wait的方法通常会阻止当前正在执行的线程,直到发生某些事情(或者,可选地,超时到期)。这恰恰相反。您打算将其作为同步还是异步操作?

也没有必要实例化Decoder对象,也不需要char数组(似乎)任何东西;只需致电System.Text.Encoding.UTF8.GetString(socketData.DataBuffer, 0, iRx)

你似乎也没有对行做任何事情......这就是为什么它对行没有任何作用。

使用StringBuilder的方法就是我要做的。我会在StringBuilder课程中添加SocketData并将其称为Builder。在捕获字符串数据时,请执行以下操作:

string[] data = System.Text.Encoding.UTF8.GetString(
                    socketData.DataBuffer, 0, iRx).Split(Environment.NewLine);

socketData.Builder.Append(data[0]);

for(int i = 1; i < data.Length; i++)
{
    // the socketData.Builder variable now contains a single line, so do 
    // something with it here, like raise an event
    OnLineReceived(builder.ToString());

    socketData.Builder = new StringBuilder(data[i]);
}

这里需要注意的是UTF8是一个多字节编码,这意味着您可能会抓取一块切断中间字符的数据。通常,最好在通信的另一端进行这种预处理,然后以适当的长度前缀格式发送数据。