以下是网络相关的操作类,希望对大家有用:
using System;
using System.Text;
using System.Net.Sockets;
using System.Net.Mail;
using System.Net;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
/// <summary> /// 网络操作相关的类 /// </summary> public class NetHelper { #region 检查设置的IP地址是否正确,返回正确的IP地址 /// <summary> /// 检查设置的IP地址是否正确,并返回正确的IP地址,无效IP地址返回"-1"。 /// </summary> /// <param name="ip">设置的IP地址</param> //public static string GetValidIP(string ip) //{ // if (PageValidate.IsIP(ip)) // { // return ip; // } // else // { // return "-1"; // } //} #endregion #region 检查设置的端口号是否正确,返回正确的端口号 /// <summary> /// 检查设置的端口号是否正确,并返回正确的端口号,无效端口号返回-1。 /// </summary> /// <param name="port">设置的端口号</param> 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对象 /// <summary> /// 将字符串形式的IP地址转换成IPAddress对象 /// </summary> /// <param name="ip">字符串形式的IP地址</param> public static IPAddress StringToIPAddress(string ip) { return IPAddress.Parse(ip); } #endregion #region 获取本机的计算机名 /// <summary> /// 获取本机的计算机名 /// </summary> public static string LocalHostName { get { return Dns.GetHostName(); } } #endregion #region 获取本机的局域网IP /// <summary> /// 获取本机的局域网IP /// </summary> public static string LANIP { get { //获取本机的IP列表,IP列表中的第一项是局域网IP,第二项是广域网IP IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; //如果本机IP列表为空,则返回空字符串 if (addressList.Length < 1) { return ""; } //返回本机的局域网IP return addressList[0].ToString(); } } #endregion #region 获取本机在Internet网络的广域网IP /// <summary> /// 获取本机在Internet网络的广域网IP /// </summary> public static string WANIP { get { //获取本机的IP列表,IP列表中的第一项是局域网IP,第二项是广域网IP IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; //如果本机IP列表小于2,则返回空字符串 if (addressList.Length < 2) { return ""; } //返回本机的广域网IP return addressList[1].ToString(); } } #endregion #region 获取远程客户机的IP地址 /// <summary> /// 获取远程客户机的IP地址 /// </summary> /// <param name="clientSocket">客户端的socket对象</param> public static string GetClientIP(Socket clientSocket) { IPEndPoint client = (IPEndPoint)clientSocket.RemoteEndPoint; return client.Address.ToString(); } #endregion #region 创建一个IPEndPoint对象 /// <summary> /// 创建一个IPEndPoint对象 /// </summary> /// <param name="ip">IP地址</param> /// <param name="port">端口号</param> public static IPEndPoint CreateIPEndPoint(string ip, int port) { IPAddress ipAddress = StringToIPAddress(ip); return new IPEndPoint(ipAddress, port); } #endregion #region 创建一个TcpListener对象 /// <summary> /// 创建一个自动分配IP和端口的TcpListener对象 /// </summary> public static TcpListener CreateTcpListener() { //创建一个自动分配的网络节点 IPAddress ipAddress = IPAddress.Any; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 0); return new TcpListener(localEndPoint); } /// <summary> /// 创建一个TcpListener对象 /// </summary> /// <param name="ip">IP地址</param> /// <param name="port">端口</param> 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对象 /// <summary> /// 创建一个基于TCP协议的Socket对象 /// </summary> public static Socket CreateTcpSocket() { return new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } #endregion #region 创建一个基于UDP协议的Socket对象 /// <summary> /// 创建一个基于UDP协议的Socket对象 /// </summary> public static Socket CreateUdpSocket() { return new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } #endregion #region 获取本地终结点 #region 获取TcpListener对象的本地终结点 /// <summary> /// 获取TcpListener对象的本地终结点 /// </summary> /// <param name="tcpListener">TcpListener对象</param> public static IPEndPoint GetLocalPoint(TcpListener tcpListener) { return (IPEndPoint)tcpListener.LocalEndpoint; } /// <summary> /// 获取TcpListener对象的本地终结点的IP地址 /// </summary> /// <param name="tcpListener">TcpListener对象</param> public static string GetLocalPoint_IP(TcpListener tcpListener) { IPEndPoint localEndPoint = (IPEndPoint)tcpListener.LocalEndpoint; return localEndPoint.Address.ToString(); } /// <summary> /// 获取TcpListener对象的本地终结点的端口号 /// </summary> /// <param name="tcpListener">TcpListener对象</param> public static int GetLocalPoint_Port(TcpListener tcpListener) { IPEndPoint localEndPoint = (IPEndPoint)tcpListener.LocalEndpoint; return localEndPoint.Port; } #endregion #region 获取Socket对象的本地终结点 /// <summary> /// 获取Socket对象的本地终结点 /// </summary> /// <param name="socket">Socket对象</param> public static IPEndPoint GetLocalPoint(Socket socket) { return (IPEndPoint)socket.LocalEndPoint; } /// <summary> /// 获取Socket对象的本地终结点的IP地址 /// </summary> /// <param name="socket">Socket对象</param> public static string GetLocalPoint_IP(Socket socket) { IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint; return localEndPoint.Address.ToString(); } /// <summary> /// 获取Socket对象的本地终结点的端口号 /// </summary> /// <param name="socket">Socket对象</param> public static int GetLocalPoint_Port(Socket socket) { IPEndPoint localEndPoint = (IPEndPoint)socket.LocalEndPoint; return localEndPoint.Port; } #endregion #endregion #region 绑定终结点 /// <summary> /// 绑定终结点 /// </summary> /// <param name="socket">Socket对象</param> /// <param name="endPoint">要绑定的终结点</param> public static void BindEndPoint(Socket socket, IPEndPoint endPoint) { if (!socket.IsBound) { socket.Bind(endPoint); } } /// <summary> /// 绑定终结点 /// </summary> /// <param name="socket">Socket对象</param> /// <param name="ip">服务器IP地址</param> /// <param name="port">服务器端口</param> public static void BindEndPoint(Socket socket, string ip, int port) { //创建终结点 IPEndPoint endPoint = CreateIPEndPoint(ip, port); //绑定终结点 if (!socket.IsBound) { socket.Bind(endPoint); } } #endregion #region 指定Socket对象执行监听 /// <summary> /// 指定Socket对象执行监听,默认允许的最大挂起连接数为100 /// </summary> /// <param name="socket">执行监听的Socket对象</param> /// <param name="port">监听的端口号</param> public static void StartListen(Socket socket, int port) { //创建本地终结点 IPEndPoint localPoint = CreateIPEndPoint(NetHelper.LocalHostName, port); //绑定到本地终结点 BindEndPoint(socket, localPoint); //开始监听 socket.Listen(100); } /// <summary> /// 指定Socket对象执行监听 /// </summary> /// <param name="socket">执行监听的Socket对象</param> /// <param name="port">监听的端口号</param> /// <param name="maxConnection">允许的最大挂起连接数</param> public static void StartListen(Socket socket, int port, int maxConnection) { //创建本地终结点 IPEndPoint localPoint = CreateIPEndPoint(NetHelper.LocalHostName, port); //绑定到本地终结点 BindEndPoint(socket, localPoint); //开始监听 socket.Listen(maxConnection); } /// <summary> /// 指定Socket对象执行监听 /// </summary> /// <param name="socket">执行监听的Socket对象</param> /// <param name="ip">监听的IP地址</param> /// <param name="port">监听的端口号</param> /// <param name="maxConnection">允许的最大挂起连接数</param> public static void StartListen(Socket socket, string ip, int port, int maxConnection) { //绑定到本地终结点 BindEndPoint(socket, ip, port); //开始监听 socket.Listen(maxConnection); } #endregion #region 连接到基于TCP协议的服务器 /// <summary> /// 连接到基于TCP协议的服务器,连接成功返回true,否则返回false /// </summary> /// <param name="socket">Socket对象</param> /// <param name="ip">服务器IP地址</param> /// <param name="port">服务器端口号</param> 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); //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message); } } #endregion #region 以同步方式发送消息 /// <summary> /// 以同步方式向指定的Socket对象发送消息 /// </summary> /// <param name="socket">socket对象</param> /// <param name="msg">发送的消息</param> public static void SendMsg(Socket socket, byte[] msg) { //发送消息 socket.Send(msg, msg.Length, SocketFlags.None); } /// <summary> /// 使用UTF8编码格式以同步方式向指定的Socket对象发送消息 /// </summary> /// <param name="socket">socket对象</param> /// <param name="msg">发送的消息</param> public static void SendMsg(Socket socket, string msg) { //将字符串消息转换成字符数组 byte[] buffer = ConvertHelper.StringToBytes(msg, Encoding.Default); //发送消息 socket.Send(buffer, buffer.Length, SocketFlags.None); } #endregion #region 以同步方式接收消息 /// <summary> /// 以同步方式接收消息 /// </summary> /// <param name="socket">socket对象</param> /// <param name="buffer">接收消息的缓冲区</param> public static void ReceiveMsg(Socket socket, byte[] buffer) { socket.Receive(buffer); } /// <summary> /// 以同步方式接收消息,并转换为UTF8编码格式的字符串,使用5000字节的默认缓冲区接收。 /// </summary> /// <param name="socket">socket对象</param> 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对象 /// <summary> /// 关闭基于Tcp协议的Socket对象 /// </summary> /// <param name="socket">要关闭的Socket对象</param> public static void Close(Socket socket) { try { //禁止Socket对象接收和发送数据 socket.Shutdown(SocketShutdown.Both); } catch (SocketException ex) { throw ex; } finally { //关闭Socket对象 socket.Close(); } } #endregion #region 发送电子邮件 /// <summary> /// 发送电子邮件,所有SMTP配置信息均在config配置文件中system.net节设置. /// </summary> /// <param name="receiveEmail">接收电子邮件的地址</param> /// <param name="msgSubject">电子邮件的标题</param> /// <param name="msgBody">电子邮件的正文</param> /// <param name="IsEnableSSL">是否开启SSL</param> public static bool SendEmail(string receiveEmail, string msgSubject, string msgBody, bool IsEnableSSL) { //创建电子邮件对象 MailMessage email = new MailMessage(); //设置接收人的电子邮件地址 email.To.Add(receiveEmail); //设置邮件的标题 email.Subject = msgSubject; //设置邮件的正文 email.Body = msgBody; //设置邮件为HTML格式 email.IsBodyHtml = true; //创建SMTP客户端,将自动从配置文件中获取SMTP服务器信息 SmtpClient smtp = new SmtpClient(); //开启SSL smtp.EnableSsl = IsEnableSSL; try { //发送电子邮件 smtp.Send(email); return true; } catch (Exception ex) { throw ex; } } #endregion } |