C#日志SharpDevelop环境(2)

2019年4月19日 0 条评论 192 次阅读 0 人点赞

HP-Socket实例框架

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using HPSocketCS;

namespace MyService
{
	public class MyService : ServiceBase
	{
		public const string MyServiceName = "YXSoftService";
		public const string MyServiceDescription = "内网管理服务器服务";
		private  HPSocketCS.TcpServer server = new HPSocketCS.TcpServer();
		private	 HPSocketCS.Extra<string> extra = new HPSocketCS.Extra<string>();
		private Thread thread;
		private string ServiceIP;
		private List<string> list = new List<string>();
		public MyService()
		{
			InitializeComponent();
		}
		
		private void InitializeComponent()
		{
			this.ServiceName = MyServiceName;
		
		}
		
		/// <summary>
		/// Start this service.
		/// </summary>
		protected override void OnStart(string[] args)
		{
			AddMsg("服务启动");
			// TODO: Add start code here (if required) to start your service.
			//server.OnPrepareListen += new ServerEvent.OnPrepareListenEventHandler(OnPrepareListen);
			server.OnAccept += new ServerEvent.OnAcceptEventHandler(OnAccept);
			//server.OnSend += new ServerEvent.OnSendEventHandler(OnSend);
			// 两个数据到达事件的一种
			//server.OnPointerDataReceive += new ServerEvent.OnPointerDataReceiveEventHandler(OnPointerDataReceive);
			// 两个数据到达事件的一种
			server.OnReceive += new ServerEvent.OnReceiveEventHandler(OnReceive);

			server.OnClose += new ServerEvent.OnCloseEventHandler(OnClose);
			server.OnShutdown += new ServerEvent.OnShutdownEventHandler(OnShutdown);
                
			server.IpAddress = "127.0.0.1";
			server.Port = 1989;
			// 启动服务
			if (server.Start()) {
				AddMsg("服务器启动成功");
			} else {
				AddMsg("服务器启动失败");
			}
			thread = new Thread(new ThreadStart(MyProcess));
			thread.Start();
		}
		
		private void MyProcess()
		{
			
			while (true) {
				//AddMsg("服务线程");
				Thread.Sleep(1000);
			}
			
		
		}
		/// <summary>
		/// Stop this service.
		/// </summary>
		protected override void OnStop()
		{
			// TODO: Add tear-down code here (if required) to stop your service.
			server.Stop();
			thread.Abort();
			AddMsg("服务停止");
			
		}
		private void AddMsg(string msg)
		{
			System.IO.File.AppendAllText("D:\\log.txt", msg + " " + DateTime.Now.ToString() + "\n");
		}

		private void SaveIP(string ip)
		{
			bool isExit = false;
			for (int i = 0; i < list.Count; i++) {
				if (list[i] == ip) {
					isExit = true;
					break;
				}
			}
			if (!isExit) {
				list.Add(ip);
			}
	
		}

		private void MoveIP(string ip)
		{
			for (int i = 0; i < list.Count; i++) {
				if (list[i]==ip){
					list.Remove(ip);
				}
			}
			
		}
		
		HandleResult OnAccept(IServer sender, IntPtr connId, IntPtr pClient)
		{
			// 客户进入了

			// 获取客户端ip和端口
			string ip = string.Empty;
			ushort port = 0;
			if (server.GetRemoteAddress(connId, ref ip, ref port)) {
				extra.Set(connId, ip);
			} 
			return HandleResult.Ok;
		}

	

		HandleResult OnReceive(IServer sender, IntPtr connId, byte[] bytes)
		{
			// 数据到达了
			try {
				// 获取附加数据
				string ip = extra.Get(connId);
				if (ip == null) {
					return HandleResult.Error;
				}
				if (bytes.Length > 1) {
					//AddMsg(string.Format("IP:{0},收到数据:{1}", ip, System.Text.Encoding.Default.GetString(bytes)));
					
					
					
				} else {
					server.Send(connId, bytes, bytes.Length);
				}
				return HandleResult.Ok;
			} catch (Exception) {

				return HandleResult.Ignore;
			}
		}

		HandleResult OnClose(IServer sender, IntPtr connId, SocketOperation enOperation, int errorCode)
		{
			//客户离开
			string ip = extra.Get(connId);
			extra.Remove(connId);
			return HandleResult.Ok;
		}

		HandleResult OnShutdown(IServer sender)
		{
			// 服务关闭了
			AddMsg("服务器停止");
			return HandleResult.Ok;
		}
        
	}
}

月下

我想要的,从始至终,不过一个你罢了。

文章评论(0)

你必须 登录 才能发表评论