superSocket 简单案例,使用ExecuteCommand的案例

服务端和客户端都是最简单的控制台程序。
(服务端的引用superSocket不多说)
服务端4个文件,
PlayerServer.cs
PlayerSession.cs
ECHO.cs
Program.cs。

四个文件的内容分别如下:

PlayerServer.cs

using SuperSocket.SocketBase;
using SuperSocket.SocketBase.Config;

namespace superSocketServer
{
    class PlayerServer : AppServer<PlayerSession>
    {
        protected override bool Setup(IRootConfig rootConfig, IServerConfig config)
        {
            return base.Setup(rootConfig, config);
        }

        protected override void OnStartup()
        {
            base.OnStartup();
        }

        protected override void OnStopped()
        {
            base.OnStopped();
        }
    }
}

PlayerSession.cs

using SuperSocket.SocketBase;

namespace superSocketServer
{
    public class PlayerSession : AppSession<PlayerSession>
    {
        public int GameHallId { get; internal set; }

        public int RoomId { get; internal set; }
    }
}

ECHO.cs

using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;

namespace superSocketServer
{
    public class ECHO : CommandBase<PlayerSession, StringRequestInfo>
    {
        public override void ExecuteCommand(PlayerSession session, StringRequestInfo requestInfo)
        {
            session.Send("~~~" + requestInfo.Body + "\t" + session.GameHallId);         
        }
    }
}

Program.cs

using System;

namespace superSocketServer
{
    class Program
    {
        static PlayerServer appServer { get; set; }
        static void Main(string[] args)
        {
            appServer = new PlayerServer();

            //Setup the appServer
            if (!appServer.Setup(2012)) //Setup with listening port
            {
                Console.WriteLine("Failed to setup!");
                Console.ReadKey();
                return;
            }
            Console.WriteLine();

            //Try to start the appServer
            if (!appServer.Start())
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("The server started successfully, press key 'q' to stop it!");

            appServer.NewSessionConnected += AppServer_NewSessionConnected;


            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            }

            //Stop the appServer
            appServer.Stop();

            Console.WriteLine("The server was stopped!");
            Console.ReadKey();
        }

        private static void AppServer_NewSessionConnected(PlayerSession session)
        {
            Console.WriteLine("AppServer_NewSessionConnected!!!!");
        }
    }
}

客户端,只有一个Program.cs文件。


using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace superSocketClient
{
    class Program
    {
        static Socket socketClient { get; set; }
        static void Main(string[] args)
        {
            //创建实例
            socketClient = new Socket(SocketType.Stream, ProtocolType.Tcp);
            IPAddress ip = IPAddress.Parse("192.168.0.121");
            IPEndPoint point = new IPEndPoint(ip, 2012);
            //进行连接
            socketClient.Connect(point);


            //不停的接收服务器端发送的消息
            Thread thread = new Thread(Recive);
            thread.IsBackground = true;
            thread.Start();


            ////不停的给服务器发送数据
            Thread thread2 = new Thread(Send);
            thread2.IsBackground = true;
            thread2.Start();

            Console.ReadKey();
        }

        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="o"></param>
        static void Recive()
        {
          //  为什么用telnet客户端可以,但这个就不行。
            while (true)
            {
                //获取发送过来的消息
                byte[] buffer = new byte[1024 * 1024 * 2];
                var effective = socketClient.Receive(buffer);
                if (effective == 0)
                {
                    break;
                }
                var str = Encoding.UTF8.GetString(buffer, 0, effective);
                Console.WriteLine("来自服务器 --- " + str);
                Thread.Sleep(1000);
            }
        }


        static void Send()
        {          
            int i = 0;
            int sum = 0;
            while (true)
            {
                i++;
                sum += i;

                var buffter = Encoding.UTF8.GetBytes($"ECHO {sum} {sum + 1}" + "\r\n");
                var temp = socketClient.Send(buffter);                
                Thread.Sleep(1000);
            }

        }
    }
}

以上即可成功运行,亲测。

自己体会体会就有感觉了。
自定义的xxxServer要结合自定义的xxxSession使用。

猜你喜欢

转载自blog.csdn.net/festone000/article/details/80268873