Browse Source

新增剪贴板工具类、Ping工具类

yuzhengyang 8 years ago
parent
commit
f38662dd72

+ 40 - 0
Fork.Net/Y.Utils/ClipboardUtils/ClipboardTool.cs

@@ -0,0 +1,40 @@
+using System.Drawing;
+using System.Windows.Forms;
+
+namespace Y.Utils.ClipboardUtils
+{
+    public class ClipboardTool
+    {
+        /// <summary>
+        /// 设置剪贴板的文本内容
+        /// </summary>
+        /// <param name="s">文本内容</param>
+        public static void SetText(string s)
+        {
+            Clipboard.SetDataObject(s ?? "");
+        }
+        /// <summary>
+        /// 获取剪贴板中的文本内容
+        /// </summary>
+        /// <returns>返回剪贴板文本</returns>
+        public static string GetText()
+        {
+            IDataObject iData = Clipboard.GetDataObject();
+            return (string)iData.GetData(DataFormats.Text);
+        }
+        /// <summary>
+        /// 获取剪贴板位图格式数据
+        /// </summary>
+        /// <returns>位图</returns>
+        public static Bitmap GetBitmap()
+        {
+            IDataObject iData = Clipboard.GetDataObject();
+            if (iData.GetDataPresent(DataFormats.Bitmap))
+            {
+                Bitmap bt = (Bitmap)iData.GetData(DataFormats.Bitmap);
+                return bt;
+            }
+            return null;
+        }
+    }
+}

+ 38 - 0
Fork.Net/Y.Utils/NetworkUtils/PingTool.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net.NetworkInformation;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Y.Utils.NetworkUtils
+{
+    public class PingTool
+    {
+        public static bool CanPing(string ip)
+        {
+            return CanPing(ip, 120);
+        }
+        public static bool CanPing(string ip, int timeout)
+        {
+            try
+            {
+                Ping pingSender = new Ping();
+                PingReply reply = pingSender.Send(ip, timeout);//第一个参数为ip地址,第二个参数为ping的时间
+                if (reply.Status == IPStatus.Success)
+                {
+                    //ping的通
+                    return true;
+                }
+                else
+                {
+                    //ping不通
+                    return false;
+                }
+            }
+            catch { }
+            //异常
+            return false;
+        }
+    }
+}

+ 189 - 0
Fork.Net/Y.Utils/SocketUtils/SocketTool.cs

@@ -0,0 +1,189 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
+using System.Net.Sockets;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Y.Utils.SocketUtils
+{
+    public class SocketTool : IDisposable
+    {
+        const int ReceiveBufferSize = 1024;
+
+        private string _IP;
+        private int _Port;
+        private bool IsReceive = false;
+        private List<byte> ReceiveByte = new List<byte>();
+        private readonly object ReceiveLock = new object();
+
+        public string IP { get; }
+        public int Port { get; }
+        private IPAddress IPAddress { get; set; }
+        private IPEndPoint IPEndPoint { get; set; }
+        public Socket Socket { get; set; }
+
+        public delegate void GetByteDelegate(byte[] b);
+        public GetByteDelegate ReceiveByteContent;
+
+        private SocketTool() { }
+
+        public SocketTool(string ip, int port)
+        {
+            _IP = ip;
+            _Port = port;
+            IPAddress = IPAddress.Parse(ip);
+            IPEndPoint = new IPEndPoint(IPAddress, port);
+        }
+
+        public bool Connect()
+        {
+            try
+            {
+                if (Socket != null)
+                {
+                    return true;
+                }
+                else
+                {
+                    Socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+                    Socket.Connect(IPEndPoint);
+                    return true;
+                }
+            }
+            catch { }
+            return false;
+        }
+
+        public bool Send(string s)
+        {
+            try
+            {
+                byte[] sb = Encoding.ASCII.GetBytes(s);
+                return Send(sb);
+            }
+            catch { }
+            return false;
+        }
+        public bool Send(byte[] b)
+        {
+            try
+            {
+                int rs = Socket.Send(b);
+                if (rs > 0) return true;
+            }
+            catch { }
+            return false;
+        }
+
+        public void Receive()
+        {
+            Task.Factory.StartNew(() =>
+            {
+                if (!IsReceive)
+                {
+                    lock (ReceiveLock)
+                    {
+                        if (!IsReceive)
+                        {
+                            IsReceive = true;
+                            while (IsReceive)
+                            {
+                                ReceiveContent();
+                            }
+                        }
+                    }
+                }
+            });
+        }
+        private void ReceiveContent()
+        {
+            //string recStr = "";
+            //byte[] recBytes = new byte[4096];
+            //int bytes = Socket.Receive(recBytes, recBytes.Length, 0);
+            //recStr += Encoding.ASCII.GetString(recBytes, 0, bytes);
+            try
+            {
+                byte[] recByte = new byte[ReceiveBufferSize];
+                int recLength = Socket.Receive(recByte, recByte.Length, 0);
+                for (int k = 0; k < recLength; k++)
+                {
+                    ReceiveByte.Add(recByte[k]);
+                }
+
+                if (ReceiveByte.Count > 6 && ReceiveByte[0] == 255 && ReceiveByte[1] == 254)
+                {
+                    int msgBodyLength = BitConverter.ToInt32(new byte[] { ReceiveByte[2], ReceiveByte[3], ReceiveByte[4], ReceiveByte[5] }, 0);
+                    if (ReceiveByte.Count >= 6 + msgBodyLength)
+                    {
+                        byte[] body = ReceiveByte.GetRange(6, msgBodyLength).ToArray();
+                        string bodyToGBK = Encoding.GetEncoding("GBK").GetString(body);
+                        ReceiveByteContent(body);
+                        Send(ReceiveByte.GetRange(0, 6).ToArray());
+                        ReceiveByte.RemoveRange(0, 6 + msgBodyLength);
+                    }
+                }
+                else
+                {
+                    ReceiveByte.Clear();
+                    Socket.Send(new byte[] { 0 });
+                }
+
+                //string recStr = Encoding.GetEncoding("GBK").GetString(recByte, 0, recLength);
+                //if (recLength > 0)
+                //{
+                //    Console.Write("rec:");
+                //    for (int j = 0; j < recLength; j++)
+                //    {
+                //        Console.Write(recByte[j] + " ");
+                //    }
+                //    Console.WriteLine(" ,");
+                //}
+                //if (recStr.Length > 0)
+                //    Console.WriteLine("接收到信息:" + recStr); 
+            }
+            catch (Exception e)
+            { }
+        }
+
+        #region IDisposable Support
+        private bool disposedValue = false; // 要检测冗余调用
+
+        protected virtual void Dispose(bool disposing)
+        {
+            if (!disposedValue)
+            {
+                if (disposing)
+                {
+                    // TODO: 释放托管状态(托管对象)。
+                    IsReceive = false;
+                    Socket?.Shutdown(SocketShutdown.Both);
+                    Socket?.Close();
+                    Socket?.Dispose();
+                }
+
+                // TODO: 释放未托管的资源(未托管的对象)并在以下内容中替代终结器。
+                // TODO: 将大型字段设置为 null。
+
+                disposedValue = true;
+            }
+        }
+
+        // TODO: 仅当以上 Dispose(bool disposing) 拥有用于释放未托管资源的代码时才替代终结器。
+        // ~SocketTool() {
+        //   // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
+        //   Dispose(false);
+        // }
+
+        // 添加此代码以正确实现可处置模式。
+        public void Dispose()
+        {
+            // 请勿更改此代码。将清理代码放入以上 Dispose(bool disposing) 中。
+            Dispose(true);
+            // TODO: 如果在以上内容中替代了终结器,则取消注释以下行。
+            // GC.SuppressFinalize(this);
+        }
+        #endregion
+    }
+}

+ 3 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -58,6 +58,7 @@
     <Compile Include="AttributeUtils\CustomAttributeHelper.cs" />
     <Compile Include="AttributeUtils\ControlAttribute.cs" />
     <Compile Include="AttributeUtils\ControlAttributeEvent.cs" />
+    <Compile Include="ClipboardUtils\ClipboardTool.cs" />
     <Compile Include="EncryptUtils\AesTool.cs" />
     <Compile Include="EncryptUtils\DesTool.cs" />
     <Compile Include="EnumUtils\FlagsEnumTool.cs" />
@@ -80,6 +81,7 @@
     <Compile Include="ImageUtils\ExifHelper.cs" />
     <Compile Include="ImageUtils\ImageHelper.cs" />
     <Compile Include="BaseUtils\RMB.cs" />
+    <Compile Include="NetworkUtils\PingTool.cs" />
     <Compile Include="OfficeUtils\WordHelper.cs" />
     <Compile Include="OfficeUtils\WordHelperTester.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
@@ -93,6 +95,7 @@
     <Compile Include="JsonUtils\JsonTool.cs" />
     <Compile Include="ReflectionUtils\DomainTool.cs" />
     <Compile Include="ReflectionUtils\SimpleReflection.cs" />
+    <Compile Include="SocketUtils\SocketTool.cs" />
     <Compile Include="StringUtils\SimilarString.cs" />
     <Compile Include="StringUtils\StringTool.cs" />
     <Compile Include="TestTest\OperatorTest.cs" />