123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- using System;
- using System.Text;
- using System.Net.Sockets;
- using System.Net.Mail;
- using System.Net;
- namespace Ant.Service.Utilities
- {
-
-
-
- public class NetHelper
- {
- #region 检查设置的IP地址是否正确,返回正确的IP地址
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- #endregion
- #region 检查设置的端口号是否正确,返回正确的端口号
-
-
-
-
- public static int GetValidPort(string port)
- {
-
- int validPort = -1;
-
- const int MINPORT = 0;
-
- const int MAXPORT = 65535;
-
- try
- {
-
- if (port == "")
- {
- throw new Exception("端口号不能为空!");
- }
-
- if ((Convert.ToInt32(port) < MINPORT) || (Convert.ToInt32(port) > MAXPORT))
- {
- throw new Exception("端口号范围无效!");
- }
-
- validPort = Convert.ToInt32(port);
- }
- catch (Exception ex)
- {
- string errMessage = ex.Message;
- }
- return validPort;
- }
- #endregion
- #region 将字符串形式的IP地址转换成IPAddress对象
-
-
-
-
- public static IPAddress StringToIPAddress(string ip)
- {
- return IPAddress.Parse(ip);
- }
- #endregion
- #region 获取本机的计算机名
-
-
-
- public static string LocalHostName
- {
- get
- {
- return Dns.GetHostName();
- }
- }
- #endregion
- #region 获取本机的局域网IP
-
-
-
- public static string LANIP
- {
- get
- {
-
- IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
-
- if (addressList.Length < 1)
- {
- return "";
- }
-
- return addressList[0].ToString();
- }
- }
- #endregion
- #region 获取本机在Internet网络的广域网IP
-
-
-
- public static string WANIP
- {
- get
- {
-
- IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
-
- if (addressList.Length < 2)
- {
- return "";
- }
-
- return addressList[1].ToString();
- }
- }
- #endregion
- #region 获取远程客户机的IP地址
-
-
-
-
- public static string GetClientIP(Socket clientSocket)
- {
- IPEndPoint client = (IPEndPoint)clientSocket.RemoteEndPoint;
- return client.Address.ToString();
- }
- #endregion
- #region 创建一个IPEndPoint对象
-
-
-
-
-
- public static IPEndPoint CreateIPEndPoint(string ip, int port)
- {
- IPAddress ipAddress = StringToIPAddress(ip);
- return new IPEndPoint(ipAddress, port);
- }
- #endregion
- #region 创建一个TcpListener对象
-
-
-
- public static TcpListener CreateTcpListener()
- {
-
- IPAddress ipAddress = IPAddress.Any;
- IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 0);
- return new TcpListener(localEndPoint);
- }
-
-
-
-
-
- public static TcpListener CreateTcpListener(string ip, int port)
- {
-
- IPAddress ipAddress = StringToIPAddress(ip);
- IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
- return new TcpListener(localEndPoint);
- }
- #endregion
- #region 创建一个基于TCP协议的Socket对象
-
-
-
- public static Socket CreateTcpSocket()
- {
- return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- }
- #endregion
- #region 创建一个基于UDP协议的Socket对象
-
-
-
- public static Socket CreateUdpSocket()
- {
- return new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
- }
- #endregion
- #region 获取本地终结点
- #region 获取TcpListener对象的本地终结点
-
-
-
-
- public static IPEndPoint GetLocalPoint(TcpListener tcpListener)
- {
- return (IPEndPoint)tcpListener.LocalEndpoint;
- }
-
-
-
-
- public static string GetLocalPoint_IP(TcpListener tcpListener)
- {
- IPEndPoint localEndPoint = (IPEndPoint)tcpListener.LocalEndpoint;
- return localEndPoint.Address.ToString();
- }
-
-
-
-
- public static int GetLocalPoint_Port(TcpListener tcpListener)
- {
- IPEndPoint localEndPoint = (IPEndPoint)tcpListener.LocalEndpoint;
- return localEndPoint.Port;
- }
- #endregion
- #region 获取Socket对象的本地终结点
-
-
-
-
- public static IPEndPoint GetLocalPoint(Socket socket)
- {
- return (IPEndPoint)socket.LocalEndPoint;
- }
-
-
-
-
- public static string GetLocalPoint_IP(Socket socket)
- {
- IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
- return localEndPoint.Address.ToString();
- }
-
-
-
-
- public static int GetLocalPoint_Port(Socket socket)
- {
- IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint;
- return localEndPoint.Port;
- }
- #endregion
- #endregion
- #region 绑定终结点
-
-
-
-
-
- public static void BindEndPoint(Socket socket, IPEndPoint endPoint)
- {
- if (!socket.IsBound)
- {
- socket.Bind(endPoint);
- }
- }
-
-
-
-
-
-
- public static void BindEndPoint(Socket socket, string ip, int port)
- {
-
- IPEndPoint endPoint = CreateIPEndPoint(ip, port);
-
- if (!socket.IsBound)
- {
- socket.Bind(endPoint);
- }
- }
- #endregion
- #region 指定Socket对象执行监听
-
-
-
-
-
- public static void StartListen(Socket socket, int port)
- {
-
- IPEndPoint localPoint = CreateIPEndPoint(NetHelper.LocalHostName, port);
-
- BindEndPoint(socket, localPoint);
-
- socket.Listen(100);
- }
-
-
-
-
-
-
- public static void StartListen(Socket socket, int port, int maxConnection)
- {
-
- IPEndPoint localPoint = CreateIPEndPoint(NetHelper.LocalHostName, port);
-
- BindEndPoint(socket, localPoint);
-
- socket.Listen(maxConnection);
- }
-
-
-
-
-
-
-
- public static void StartListen(Socket socket, string ip, int port, int maxConnection)
- {
-
- BindEndPoint(socket, ip, port);
-
- socket.Listen(maxConnection);
- }
- #endregion
- #region 连接到基于TCP协议的服务器
-
-
-
-
-
-
- public static bool Connect(Socket socket, string ip, int port)
- {
- try
- {
-
- socket.Connect(ip, port);
-
- return socket.Poll(-1, SelectMode.SelectWrite);
- }
- catch (SocketException ex)
- {
- throw new Exception(ex.Message);
-
- }
- }
- #endregion
- #region 以同步方式发送消息
-
-
-
-
-
- public static void SendMsg(Socket socket, byte[] msg)
- {
-
- socket.Send(msg, msg.Length, SocketFlags.None);
- }
-
-
-
-
-
- public static void SendMsg(Socket socket, string msg)
- {
-
- byte[] buffer = ConvertHelper.StringToBytes(msg, Encoding.Default);
-
- socket.Send(buffer, buffer.Length, SocketFlags.None);
- }
- #endregion
- #region 以同步方式接收消息
-
-
-
-
-
- public static void ReceiveMsg(Socket socket, byte[] buffer)
- {
- socket.Receive(buffer);
- }
-
-
-
-
- public static string ReceiveMsg(Socket socket)
- {
-
- byte[] buffer = new byte[5000];
-
- int receiveCount = socket.Receive(buffer);
-
- byte[] tempBuffer = new byte[receiveCount];
-
- Buffer.BlockCopy(buffer, 0, tempBuffer, 0, receiveCount);
-
- return ConvertHelper.BytesToString(tempBuffer, Encoding.Default);
- }
- #endregion
- #region 关闭基于Tcp协议的Socket对象
-
-
-
-
- public static void Close(Socket socket)
- {
- try
- {
-
- socket.Shutdown(SocketShutdown.Both);
- }
- catch (SocketException ex)
- {
- throw ex;
- }
- finally
- {
-
- socket.Close();
- }
- }
- #endregion
- #region 发送电子邮件
-
-
-
-
-
-
-
- public static bool SendEmail(string receiveEmail, string msgSubject, string msgBody, bool IsEnableSSL)
- {
-
- System.Net.Mail.MailMessage email = new System.Net.Mail.MailMessage();
-
- email.To.Add(receiveEmail);
-
- email.Subject = msgSubject;
-
- email.Body = msgBody;
-
- email.IsBodyHtml = true;
-
- System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
-
- smtp.EnableSsl = IsEnableSSL;
- try
- {
-
- smtp.Send(email);
- return true;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- #endregion
- }
- }
|