⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 socket.cs

📁 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的
💻 CS
📖 第 1 页 / 共 3 页
字号:
								++posn2;							}						}					}					else					{						for(posn = 0; posn < readArray.Length; ++posn)						{							if(readArray[posn] == InvalidHandle)							{								checkRead[posn] = null;							}						}					}				}				if(checkWrite != null)				{					if(!(checkWrite.IsFixedSize))					{						posn2 = 0;						for(posn = 0; posn < writeArray.Length; ++posn)						{							if(writeArray[posn] == InvalidHandle)							{								checkWrite.RemoveAt(posn2);							}							else							{								++posn2;							}						}					}					else					{						for(posn = 0; posn < writeArray.Length; ++posn)						{							if(writeArray[posn] == InvalidHandle)							{								checkWrite[posn] = null;							}						}					}				}				if(checkError != null)				{					if(!(checkError.IsFixedSize))					{						posn2 = 0;						for(posn = 0; posn < errorArray.Length; ++posn)						{							if(errorArray[posn] == InvalidHandle)							{								checkError.RemoveAt(posn2);							}							else							{								++posn2;							}						}					}					else					{						for(posn = 0; posn < errorArray.Length; ++posn)						{							if(errorArray[posn] == InvalidHandle)							{								checkError[posn] = null;							}						}					}				}			}	// Send data on this socket.	public int Send(byte[] buffer, int offset, int size,					SocketFlags socketFlags)			{				int result;				// Validate the arguments.				ValidateBuffer(buffer, offset, size);				// Perform the send operation.				lock(this)				{					if(handle == InvalidHandle)					{						throw new ObjectDisposedException							(S._("Exception_Disposed"));					}					result = SocketMethods.Send						(handle, buffer, offset, size, (int)socketFlags);					if(result < 0)					{						throw new SocketException(SocketMethods.GetErrno());					}					else					{						return result;					}				}			}	public int Send(byte[] buffer, int size, SocketFlags socketFlags)			{				return Send(buffer, 0, size, socketFlags);			}	public int Send(byte[] buffer, SocketFlags socketFlags)			{				if(buffer == null)				{					throw new ArgumentNullException("buffer");				}				return Send(buffer, 0, buffer.Length, socketFlags);			}	public int Send(byte[] buffer)			{				if(buffer == null)				{					throw new ArgumentNullException("buffer");				}				return Send(buffer, 0, buffer.Length, SocketFlags.None);			}	// Send data on this socket to a specific location.	public int SendTo(byte[] buffer, int offset, int size,					  SocketFlags socketFlags, EndPoint remoteEP)			{				int result;				byte[] addr;				// Validate the arguments.				ValidateBuffer(buffer, offset, size);				if(remoteEP == null)				{					throw new ArgumentNullException("remoteEP");				}				else if(remoteEP.AddressFamily != family)				{					throw new SocketException(Errno.EINVAL);				}				// Convert the end point into a sockaddr buffer.				addr = remoteEP.Serialize().Array;				// Perform the send operation.				lock(this)				{					if(handle == InvalidHandle)					{						throw new ObjectDisposedException							(S._("Exception_Disposed"));					}					result = SocketMethods.SendTo						(handle, buffer, offset, size, (int)socketFlags, addr);					if(result < 0)					{						throw new SocketException(SocketMethods.GetErrno());					}					else					{						return result;					}				}			}	public int SendTo(byte[] buffer, int size, SocketFlags socketFlags,					  EndPoint remoteEP)			{				return SendTo(buffer, 0, size, socketFlags, remoteEP);			}	public int SendTo(byte[] buffer, SocketFlags socketFlags,					  EndPoint remoteEP)			{				if(buffer == null)				{					throw new ArgumentNullException("buffer");				}				return SendTo(buffer, 0, buffer.Length,							   socketFlags, remoteEP);			}	public int SendTo(byte[] buffer, EndPoint remoteEP)			{				if(buffer == null)				{					throw new ArgumentNullException("buffer");				}				return SendTo(buffer, 0, buffer.Length,							  SocketFlags.None, remoteEP);			}	// Set a raw numeric socket option.	private void SetSocketOptionRaw(SocketOptionLevel optionLevel,									SocketOptionName optionName,									int optionValue)			{				lock(this)				{					if(handle == InvalidHandle)					{						throw new ObjectDisposedException							(S._("Exception_Disposed"));					}					if(!SocketMethods.SetSocketOption							(handle, (int)optionLevel, (int)optionName,							 optionValue))					{						throw new SocketException(SocketMethods.GetErrno());					}				}			}	// Set an option on this socket.	public void SetSocketOption(SocketOptionLevel optionLevel,								SocketOptionName optionName,								int optionValue)			{				// Validate the option information to ensure that the				// caller is not trying to set something that is insecure.				if(optionLevel == SocketOptionLevel.Socket)				{					if(optionName == SocketOptionName.KeepAlive ||					   optionName == SocketOptionName.ReceiveBuffer ||					   optionName == SocketOptionName.SendBuffer ||					   optionName == SocketOptionName.ReuseAddress)					{						SetSocketOptionRaw(optionLevel, optionName,										   optionValue);						return;					}					if(optionName == SocketOptionName.DontLinger)					{						// Convert "DontLinger" into a "Linger" call.						LingerOption linger =							new LingerOption(optionValue != 0, 0);						SetSocketOption(optionLevel,										SocketOptionName.Linger,										linger);						return;					}					if(optionName == SocketOptionName.ExclusiveAddressUse)					{						// Flip the option and turn it into "ReuseAddress".						SetSocketOptionRaw(optionLevel,										   SocketOptionName.ReuseAddress,										   (optionValue == 0) ? 1 : 0);						return;					}					if(optionName == SocketOptionName.ReceiveTimeout ||					   optionName == SocketOptionName.SendTimeout)					{						// These options are fixed by the Internet RFC's,						// are should never be changed by applications.						// But there is existing C# code that thinks they						// can be set to different values.  Quietly ignore.						return;					}				}				else if(optionLevel == SocketOptionLevel.Tcp)				{					if(optionName == SocketOptionName.NoDelay ||					   optionName == SocketOptionName.Expedited)					{						SetSocketOptionRaw(optionLevel, optionName,										   optionValue);						return;					}				}				else if(optionLevel == SocketOptionLevel.Udp)				{					if(optionName == SocketOptionName.NoChecksum ||					   optionName == SocketOptionName.ChecksumCoverage)					{						SetSocketOptionRaw(optionLevel, optionName,										   optionValue);						return;					}				}				throw new SecurityException(S._("Arg_SocketOption"));			}	public void SetSocketOption(SocketOptionLevel optionLevel,								SocketOptionName optionName,								byte[] optionValue)			{				// We don't support any options with byte[] values.				throw new SecurityException(S._("Arg_SocketOption"));			}	public void SetSocketOption(SocketOptionLevel optionLevel,								SocketOptionName optionName,								Object optionValue)			{				// Validate the option information to ensure that the				// caller is not trying to set something that is insecure.				if(optionValue == null)				{					throw new ArgumentNullException("optionValue");				}				if(optionLevel == SocketOptionLevel.Socket)				{					if(optionName == SocketOptionName.Linger)					{						// Modify the linger option.						if(!(optionValue is LingerOption))						{							throw new ArgumentException								(S._("Arg_SocketOptionValue"));						}						LingerOption linger = (LingerOption)optionValue;						if(linger.LingerTime < 0 ||						   linger.LingerTime > UInt16.MaxValue)						{							throw new ArgumentException								(S._("Arg_SocketOptionValue"));						}						lock(this)						{							if(handle == InvalidHandle)							{								throw new ObjectDisposedException									(S._("Exception_Disposed"));							}							if(!SocketMethods.SetLingerOption									(handle, linger.Enabled,									 linger.LingerTime))							{								throw new SocketException									(SocketMethods.GetErrno());							}						}						return;					}				}				else if(optionLevel == SocketOptionLevel.IP)				{					if(optionName == SocketOptionName.AddMembership ||					   optionName == SocketOptionName.DropMembership)					{						// Modify the multicast membership set.						if(!(optionValue is MulticastOption))						{							throw new ArgumentException								(S._("Arg_SocketOptionValue"));						}						MulticastOption multicast =							(MulticastOption)optionValue;						byte[] group =							(new IPEndPoint(multicast.Group, 0))								.Serialize().Array;						byte[] mcint =							(new IPEndPoint(multicast.LocalAddress, 0))								.Serialize().Array;						lock(this)						{							if(handle == InvalidHandle)							{								throw new ObjectDisposedException									(S._("Exception_Disposed"));							}							if(!SocketMethods.SetMulticastOption									(handle, (int)family, (int)optionName,									 group, mcint))							{								throw new SocketException									(SocketMethods.GetErrno());							}						}						return;					}				}				throw new ArgumentException(S._("Arg_SocketOption"));			}	// Perform a shutdown on one or either socket direction.	public void Shutdown(SocketShutdown how)			{				lock(this)				{					if(handle == InvalidHandle)					{						throw new ObjectDisposedException							(S._("Exception_Disposed"));					}					if(!SocketMethods.Shutdown(handle, (int)how))					{						throw new SocketException(SocketMethods.GetErrno());					}				}			}			// Determine if IPv4 or IPv6 is supported.	public static bool SupportsIPv4			{				get				{					return SocketMethods.AddressFamilySupported						((int)AddressFamily.InterNetwork);				}			}	public static bool SupportsIPv6			{				get				{					return SocketMethods.AddressFamilySupported						((int)AddressFamily.InterNetworkV6);				}			}	// Get the address family for this socket.	public AddressFamily AddressFamily			{				get				{					return family;				}			}	// Get the number of bytes that are available for reading.	public int Available			{				get				{					lock(readLock)					{						if(handle == InvalidHandle)						{							throw new ObjectDisposedException								(S._("Exception_Disposed"));						}						int result = SocketMethods.GetAvailable(handle);						if(result < 0)						{							throw new SocketException								(SocketMethods.GetErrno());						}						else						{							return result;						}					}				}			}	// Get or set the blocking state on this socket.	public bool Blocking			{				get				{					return blocking;				}				set				{					lock(this)					{						if(handle == InvalidHandle)						{							throw new ObjectDisposedException								(S._("Exception_Disposed"));						}						if(blocking != value)						{							blocking = value;							SocketMethods.SetBlocking(handle, value);						}					}				}			}	// Determine if this socket is connected.	public bool Connected			{				get				{					return connected;				}			}	// Get the operating system handle for this socket.	public IntPtr Handle			{				get				{					return handle;				}			}	// Get the operating system handle for a socket object,	// bailing out if the object is not a valid socket.	private static IntPtr GetHandle(Object obj)			{				Socket socket = (obj as Socket);				if(socket == null)				{					throw new ArgumentException(S._("Arg_NotSocket"));				}				lock(socket)				{					if(socket.handle == InvalidHandle)					{						throw new ArgumentException(S._("Arg_NotSocket"));					}					return socket.handle;				}			}	// Get the local end-point in use by this socket.	public EndPoint LocalEndPoint			{				get				{					byte[] addrReturn;					EndPoint ep;					lock(this)					{						if(handle == InvalidHandle)						{							throw new ObjectDisposedException								(S._("Exception_Disposed"));						}						if(localEP != null)						{							return localEP;						}						// Get a sockaddr buffer of the right size.						ep = remoteEP;						if(ep == null)						{							// We don't have a remote end-point to guess							// the size from, so check for known families.							if(family == AddressFamily.InterNetwork)							{								ep = new IPEndPoint(IPAddress.Any, 0);							}							else if(family == AddressFamily.InterNetworkV6)							{								ep = new IPEndPoint(IPAddress.IPv6Any, 0);							}							else							{								throw new SocketException(Errno.EINVAL);							}						}						addrReturn = ep.Serialize().Array;						Array.Clear(addrReturn, 0, addrReturn.Length);						// Get the name of the socket.						if(!SocketMethods.GetSockName(handle, addrReturn))						{							throw new SocketException								(SocketMethods.GetErrno());						}						// Create a new end-point object using addrReturn.						localEP = ep.Create(new SocketAddress(addrReturn));						return localEP;					}				}			}	// Get the protocol type in use by this socket.	public ProtocolType ProtocolType			{				get				{					return protocol;				}			}	// Get the remote end-point in use by this socket.	public EndPoint RemoteEndPoint			{				get				{					lock(this)					{						if(handle == InvalidHandle)						{							throw new ObjectDisposedException								(S._("Exception_Disposed"));						}						if(remoteEP == null)						{							throw new SocketException(Errno.ENOTCONN);						}						return remoteEP;					}				}			}	// Get the type of this socket.	public SocketType SocketType			{				get				{					return socketType;				}			}	// Helper function for validating buffer arguments.	private static void ValidateBuffer				(byte[] buffer, int offset, int size)			{				if(buffer == null)				{					throw new ArgumentNullException("buffer");				}				else if(offset < 0 || offset > buffer.Length)				{					throw new ArgumentOutOfRangeException						("offset", S._("ArgRange_Array"));				}				else if(size < 0)				{					throw new ArgumentOutOfRangeException						("size", S._("ArgRange_Array"));				}				else if((buffer.Length - offset) < size)				{					throw new ArgumentException(S._("Arg_InvalidArrayRange"));				}			}}; // class Socket}; // namespace System.Net.Sockets

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -