• 大小: 87KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-16
  • 语言: C#
  • 标签:

资源简介

可进行简单的客户端信息的接收,解析、转发,打印日志功能

资源截图

代码片段和文件信息

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

namespace SocketTestForms
{
    class Class1
    {
        private bool LoginReceive(SocketState state)
        {
            try
            {
                state.Socket.BeginReceive(state.Buffer 0 SocketState.BufferSize SocketFlags.None new AsyncCallback(LogReceiveCallback) state);
            }
            catch (Exception ex)//出现Socket异常就关闭连接 
            {
                CloseSocket(state ex);//这个函数用来关闭客户端连接 
                return false;
            }
            return true;
        }
        private void LogReceiveCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;
            Socket handler = state.Socket;
            int bytesRead = 0;
            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (Exception ex)
            {
                CloseSocket(state ex);//这个函数用来关闭客户端连接 
                return;
            }

            if (bytesRead > 0)
            {
                //处理接受的数据
                byte[] buffer = new byte[bytesRead];
                Buffer.BlockCopy(state.Buffer 0 buffer 0 bytesRead);
                //Analyzer(state buffer);//这个函数用来处理接收到的信息。

                BeginReceive(state);

            }
            else
            {
                CloseSocket(state new Exception(“0 byte data received!“));
            }
        }
        private void Analyzer(SocketState state byte[] buffer)
        {

            WriteLog(System.Text.Encoding.ASCII.GetString(buffer) + “recive success!“);
            if (buffer[0] != ‘*‘ || buffer[buffer.Length - 1] != ‘#‘)
            {
                return;
            }
            var bufferToString = System.Text.Encoding.ASCII.GetString(buffer);//将ASCII码字节数组转化为字符串
            string[] strcmdSentence = byteToString4.Split(‘#‘);//将字符串按照逗号分开

            for (int k = 0; k < strcmdSentence.Length; k++)
            {
                Array.Copy(strcmdStence 1 bufferSentence 0 buffer.Length - 1);
                string[] strcmdWord = bufferSentence[k].Split(““);
            }
            byte[] buffer1 = new byte[buffer.Length - 2];
            Array.Copy(buffer 1 buffer1 0 buffer.Length - 2);
            var byteToString4 = System.Text.Encoding.ASCII.GetString(buffer1);//将ASCII码字节数组转化为字符串
            string[] strdata = byteToString4.Split(‘‘);//将字符串按照逗号分开

            if (strdata[0] == “REQ“)
            {
                for (int i = 0; i < _socketStates.Count; i++)
                {
                    SocketState desState = (SocketState)_socketStates[i];
                    if (desState.SocketSrc == strdata[2])
                    {
                        Send(state System.Text.Encoding.Default.GetBytes(“*REF1,“ + strdata[2] + ““ + strdata[3] + ““ + strdata[4] + “#“

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-06-22 10:31  SocketTestForms\
     目录           0  2017-09-19 17:49  SocketTestForms\SocketTestForms\
     文件         935  2017-06-22 10:31  SocketTestForms\SocketTestForms.sln
     文件       94208  2017-10-10 12:42  SocketTestForms\SocketTestForms.v11.suo
     文件         187  2017-06-22 10:31  SocketTestForms\SocketTestForms\App.config
     目录           0  2017-09-19 16:36  SocketTestForms\SocketTestForms\bin\
     目录           0  2017-09-22 10:05  SocketTestForms\SocketTestForms\bin\Debug\
     文件         959  2017-09-08 10:10  SocketTestForms\SocketTestForms\bin\Debug\rizhi08.log
     文件       14848  2017-10-10 10:45  SocketTestForms\SocketTestForms\bin\Debug\SocketTestForms.exe
     文件         187  2017-06-22 10:31  SocketTestForms\SocketTestForms\bin\Debug\SocketTestForms.exe.config
     文件       44544  2017-10-10 10:45  SocketTestForms\SocketTestForms\bin\Debug\SocketTestForms.pdb
     文件       22984  2017-10-10 09:39  SocketTestForms\SocketTestForms\bin\Debug\SocketTestForms.vshost.exe
     文件         187  2017-06-22 10:31  SocketTestForms\SocketTestForms\bin\Debug\SocketTestForms.vshost.exe.config
     文件         490  2012-06-06 02:06  SocketTestForms\SocketTestForms\bin\Debug\SocketTestForms.vshost.exe.manifest
     目录           0  2017-09-19 16:36  SocketTestForms\SocketTestForms\bin\Release\
     文件        7825  2017-09-20 18:55  SocketTestForms\SocketTestForms\Class1.cs
     文件         772  2017-10-10 10:42  SocketTestForms\SocketTestForms\Form1.cs
     文件        1509  2017-07-08 18:24  SocketTestForms\SocketTestForms\Form1.Designer.cs
     文件        5817  2017-07-08 18:24  SocketTestForms\SocketTestForms\Form1.resx
     目录           0  2017-06-22 10:31  SocketTestForms\SocketTestForms\obj\
     目录           0  2017-10-10 10:45  SocketTestForms\SocketTestForms\obj\Debug\
     文件        1278  2017-06-22 11:00  SocketTestForms\SocketTestForms\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6927  2017-09-20 09:21  SocketTestForms\SocketTestForms\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件         852  2017-10-10 09:39  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.csproj.FileListAbsolute.txt
     文件         975  2017-09-22 10:05  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.csproj.GenerateResource.Cache
     文件        2143  2017-09-22 10:58  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.csprojResolveAssemblyReference.cache
     文件       14848  2017-10-10 10:45  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.exe
     文件         180  2017-10-09 09:15  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.Form1.resources
     文件       44544  2017-10-10 10:45  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.pdb
     文件         180  2017-10-09 09:15  SocketTestForms\SocketTestForms\obj\Debug\SocketTestForms.Properties.Resources.resources
     文件           0  2017-06-22 10:31  SocketTestForms\SocketTestForms\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
............此处省略15个文件信息

评论

共有 条评论

相关资源