Beispiel #1
0
        public void ReadCallback(IAsyncResult ar)
        {
            string strProcedureName =
                string.Format(
                    "{0}.{1}",
                    className,
                    MethodBase.GetCurrentMethod().Name);
            string content = string.Empty;

            // Retrieve the state object and the handler socket
            // from the asynchronous state object.
            StateObject state   = (StateObject)ar.AsyncState;
            Socket      handler = state.workSocket;

            // Read data from the client socket.
            int bytesRead = handler.EndReceive(ar);

            if (bytesRead > 0)
            {
                // There might be more data, so store the data received so far.
                state.sb.Append(
                    Encoding.ASCII.GetString(
                        state.buffer,
                        0,
                        bytesRead));

                //content = state.sb.ToString();
                content = Encoding.ASCII.GetString(
                    state.buffer,
                    0,
                    bytesRead);

                Console.WriteLine(
                    "Read {0} bytes from socket [{1}].\nData:{2}",
                    content.Length,
                    handler.RemoteEndPoint.ToString(),
                    content);
                WriteLog.Instance.Write(
                    string.Format(
                        "Read {0} bytes from socket [{1}].Data:{2}",
                        content.Length,
                        handler.RemoteEndPoint.ToString(),
                        content),
                    strProcedureName);

                RecordData record =
                    new RecordData(
                        handler.RemoteEndPoint.ToString(),
                        content);
                if (record.DataValid())
                {
                    #region 处理收到的数据
                    Thread dataRecording = new Thread(new ThreadStart(record.Record));
                    dataRecording.Start();
                    #endregion

                    // Echo the data back to the client.
                    Send(handler, "OK");
                }
                else
                {
                    Send(handler, "FALSE");
                }

                handler.BeginReceive(
                    state.buffer,
                    0,
                    StateObject.BufferSize,
                    0,
                    new AsyncCallback(ReadCallback),
                    state);
            }
        }
Beispiel #2
0
        public void StartRead()
        {
            string strProcedureName =
                string.Format(
                    "{0}.{1}",
                    className,
                    MethodBase.GetCurrentMethod().Name);

            WriteLog.Instance.WriteBeginSplitter(strProcedureName);
            try
            {
                int    readSize = 0;
                byte[] buffer   = new byte[11];

                stream.ReadTimeout = 100;
                readSize           = stream.Read(buffer, 0, 11);
                if (readSize == 0)
                {
                    WriteLog.Instance.Write("未收到数据", strProcedureName);
                    SendResponse("FALSE");
                    return;
                }

                #region 整理收到的通讯数据,去除不可见字符
                List <byte> temp = new List <byte>();
                for (int i = 0; i < buffer.Length; i++)
                {
                    if (buffer[i] >= 32 && buffer[i] <= 126)
                    {
                        temp.Add(buffer[i]);
                    }
                    else
                    {
                        break;
                    }
                }
                #endregion

                string content = Encoding.ASCII.GetString(temp.ToArray());
                WriteLog.Instance.Write(
                    string.Format(
                        "从 [{0}] 读取到 [{1}] 字节,内容: [{2}]",
                        connection.Client.RemoteEndPoint.ToString(),
                        content.Length,
                        content),
                    strProcedureName);

                RecordData record =
                    new RecordData(
                        connection.Client.RemoteEndPoint.ToString(),
                        content);
                if (record.DataValid())
                {
                    #region 处理收到的数据
                    Thread dataRecording = new Thread(new ThreadStart(record.Record));
                    dataRecording.Start();
                    #endregion

                    SendResponse("OK");
                }
                else
                {
                    SendResponse("FALSE");
                }
            }
            catch (Exception error)
            {
                SendResponse("FALSE");
                WriteLog.Instance.Write(error.Message, strProcedureName);
            }
            finally
            {
                WriteLog.Instance.WriteEndSplitter(strProcedureName);
            }
        }