Glue4Net简单部署基于win服务的Socket程序

Glue4Net简单部署基于win服务的Socket程序

在写一些服务应用的时候经常把要它部署到windows service中,主要是让系统自动加载相关程序用于后台运行.正常写windows server需要单独写个程序包装和相应的安装类才能完成这些工作.但通过Glue4Net就可以非常简单地把代码或DLL部署到windows server中运行.下面地介绍通过Glue4Net简单地部署一个Socket程序.

安装Glue4net

首要工作是要把Glue4net安装下windows service中,下载http://www.ikende.com/files/file/Glue4net.rar 解压后运行install.bat 这样Glue4net就安装到windows server中.这个时候你要在服务中启动Glue4net,之后windows启动就会自动加载.

实现一个Socket Tcp服务功能

Glue4net可以挂DLL和代码文件,如果功能比较简单那直接写个.cs或.vb文件即可,下面就通过c#写一个简单的Socket Tcp服务.

复制代码
  1 using System;

  2 using System.Collections.Generic;

  3 using System.Linq;

  4 using System.Net;

  5 using System.Net.Sockets;

  6 using System.Text;

  7 

  8 namespace Glue4Net.SocketServer

  9 {

 10     public class Server:Glue4Net.IAppModule

 11     {

 12         private Socket mListenSocket;

 13 

 14         private IList<Socket> mClients = new List<Socket>();

 15 

 16         private bool IsAccepting = false;

 17 

 18         private void BeginReceive(Socket e)

 19         {

 20             SocketAsyncEventArgs saea = new SocketAsyncEventArgs();

 21             saea.Completed += OnReceiveCompleted;

 22             saea.SetBuffer(new byte[1024], 0, 1024);

 23             saea.UserToken = e;

 24             if (!e.ReceiveAsync(saea))

 25             {

 26                 OnReceiveCompleted(this, saea);

 27             }

 28         }

 29 

 30         private void SendData(Socket e, byte[] data, int offset, int count)

 31         {

 32             e.Send(data, offset, count, SocketFlags.None);

 33         }

 34 

 35         private void OnReceiveCompleted(object sender, SocketAsyncEventArgs e)

 36         {

 37             if (e.SocketError == SocketError.Success && e.BytesTransferred > 0)

 38             {

 39                 SendData((Socket)e.UserToken, e.Buffer, 0, e.BytesTransferred);

 40                 BeginReceive((Socket)e.UserToken);

 41             }

 42             else

 43             {

 44                 mClients.Remove((Socket)e.UserToken);

 45                 Log.Error("{0} disposed!", e.RemoteEndPoint);

 46             }

 47             

 48         }

 49 

 50         private void OnAccept(object state)

 51         {

 52             while (IsAccepting)

 53             {

 54                 try

 55                 {

 56                     Socket socket = mListenSocket.Accept();

 57                     Log.Info("{0} connected!", socket.RemoteEndPoint);

 58                     mClients.Add(socket);

 59                     BeginReceive(socket);

 60                 }

 61                 catch (Exception e_)

 62                 {

 63                     Log.Info("Socket Server Accept error {0}",e_.Message);

 64                 }

 65             }

 66         }

 67 

 68         public void Load()

 69         {

 70             try

 71             {

 72                 mListenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

 73                 mListenSocket.Bind(new IPEndPoint(IPAddress.Any, 10024));

 74                 mListenSocket.Listen(10);

 75                 IsAccepting = true;

 76                 System.Threading.ThreadPool.QueueUserWorkItem(OnAccept);

 77                 Log.Info("Socket Server Listen @{0}", 10024);

 78                 Log.Info("Load Socket Server Success!");

 79             }

 80             catch (Exception e_)

 81             {

 82                 Log.Error("Load Socket Server Error {0}", e_.Message);

 83             }

 84         }

 85 

 86         public IEventLog Log

 87         {

 88             get;

 89             set;

 90         }

 91 

 92         public string Name

 93         {

 94             get { return "Socket Server"; }

 95         }

 96 

 97         private void DisposedSocket(Socket e)

 98         {

 99             try

100             {

101                 

102                 if (e != null)

103                 {

104                     mListenSocket.Close();

105                     mListenSocket.Shutdown(SocketShutdown.Both);

106                 }

107             }

108             catch

109             {

110             }

111         }

112 

113         public void UnLoad()

114         {

115             try

116             {

117                 IsAccepting = false;

118                 DisposedSocket(mListenSocket);

119                 foreach (Socket client in mClients)

120                 {

121                     DisposedSocket(client);

122                 }

123                 Log.Info("Unload Socket Server Success!");

124             }

125             catch (Exception e_)

126             {

127                 Log.Error("Unload Socket Server Error {0}", e_.Message);

128             }

129         }

130     }

131 }
复制代码

继承Glue4Net.IAppModule是容器加载运行的规则,需要在Load方法中实现启动运行的工作,然后在UnLoad方法里实现服务关闭的工作.功能完成后把代码保存到任何目录下,这里是保存到H:\SocketServer;以上是一个非常简单的Socket TCP服务,把接收的信息回应给发送端.

部署Socket程序

为了保证Glue4Net能加载,必须保让代码目录下存在Glue4Net.Dll这个文件,因为它是模块加载的入口点.把程序加载到Glue4Net是非常简单的事情,只需要在domains.config中添加一个配置即可.

1 <add name="socketserver"

2         compiler="true"

3         path="H:\SocketServer\"

4             updateWatch="true"

5                watchFilter="*.cs|*.config"/>

添加一个socketserver的应用名,然后目录指向之前存放代码的目录,确认配置信息无误后保存.当文件保后无须对Glue4Net进行重启服务即可加载完成.针对以上编写的代码部署完成后,你只需要telent 127.0.0.1 10024 即可连接到相应的服务中.

总结

通过Glue4Net非常简单就能把程序添加到windows service中运行,而这个部署是基于热加载.当添加或修改某些应用的时候,是完全不会影响其他应用的正常工作.

 
分类:  类库分享

你可能感兴趣的:(socket)