建立网络服务器

SupperSocket官网:http://www.supersocket.net/

SupperSocket 源码和库的下载地址:https://supersocket.codeplex.com/releases/view/161987

 

准备:下载SupperSocket的Lib库文件:

新建C#工程,修改目标框架如下图:

在项目中添加引用(.net4.0版本):

 

在创建服务器前,我们需要创建3个必要的文件,Server、Session、Command这三个文件:

Server负责管理客户端和服务器之间的连接Session,Command负责命令处理;

 

1. 创建AppServer

新建AppServer文件:

在该类中重写服务器核心代码:

2. 创建AppSession

新建AppSession文件:

 

3. 创建Command文件:

新建AppCommand文件:

 

4. Main函数处理

namespace NetworkServer_ElectricalPowerSystem
{
    class Program
    {
        static void Main(string[] args)
        {
            ElectricServer_jqm EServer = new ElectricServer_jqm();

            //设置坚挺端口号
            if (!EServer.Setup(2012)) //Setup with listening port
            {
                Console.WriteLine("Failed to setup!");
                Console.ReadKey();
                return;
            }

            //启动服务器
            if (!EServer.Start())
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }

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

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

            //Stop the appServer
            EServer.Stop();

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

 

5. 使用window7 自带的远程连接(telnet)测试:


 ==================================

简单的构建好的Server项目:

https://files.cnblogs.com/files/jqm304775992/ElectricalPowerSystemNetworkServer_JQM.rar

posted @ 2015-10-12 11:17  那些消散不去的是寂寞  阅读(411)  评论(0编辑  收藏  举报