Source code for TcpClient in .NET

xiaoxiao2021-03-06  49

/ / -------------------------------------------------------------------------------------------- ------------------------------ // // / / Copyright (c) 2002 Microsoft Corporation. All rights reserved.// // The use and distribution terms for this software are contained in the file // named license.txt, which can be found in the root of this distribution.// By Using this Software in Any Fashion, you are agreeing to be bound by the // Terms of this license./// You Must Not Remove this notice, or any other, from this software.// // / / -------------------------------------------------------------------------------------------- ------------------------------

Namespace system.net.sockets {

/// /// /// The Class Provide TCP Services at a higher level /// of Abstract Than The Class. /// is used to create aclient connection to a remote host. /// public class tclient: idisposable {

Socket M_ClientSocket; Bool M_Active; NetworkStream M_DataStream; Bool M_DataStreamCreated

// Specify local ip and port /// /// // / /// initializes a new instance of the /// Class with the specified end point. /// /// public TcpClient (IPEndPoint localEP) {if (localEP == null) {throw new ArgumentNullException ( "localEP");} initialize (); Client.Bind (localEP);} // TcpClient (IPaddress localaddr); // port IS arbitrary // tclient (int outgoingport); // local ip is arbitrary

// address port is arbitrary /// /// // / /// initializes a new instance of the Class. /// /// public tclient () {Initialize ();

// bind and connection /// /// /// < Para> Initializes a new instance of the class and connects to the /// specified port on the specified host. /// public TcpClient (string hostname, int port) {if (hostname == null) {throw new ArgumentNullException ( "hostname");} if {throw new ArgumentOutOfRangeException ( "port") (ValidationHelper.ValidateTcpPort (port)!);} initialize ( Connect (Hostname, Port);} //// @ u u u ac ac c = =;;}

/// /// /// ////// Used by the class to provide ////// /// /// protected socket client {get {return m_clientsocket;} set {m_clientsocket = value;}}

/// /// /// ////// Used by the class to indeicate triad by the class to indeicate triad by the class to indeicate. File = 'DOC / TCPCLIENT.UEX' PATH = 'DOCS / DOC [@ for = "tcpclient.connect"] / *' //// /// ///connects the client to the the client specified port on the specified host /// /// public void Connect (string hostname, int port) {if (m_CleanedUp) {throw new ObjectDisposedException (this.GetType () FullName.).; } If (hostname == null) {throw new argumentnullexception ("hostname");} if (! Value (! Value) {{@ @@@T) { Throw new ArgumentOutofrangeException ("port");} ipaddress address = dns.resolve (hostname) .addresslist [0]; connect (address, port);}

/// /// ////// Connects the Client to the specified port on the specified host. /// /// public void Connect (IPAddress address, int port) {if (m_CleanedUp) {throw new ObjectDisposedException (this.GetType ( ) .FullName);} if (address == null) {throw new ArgumentNullException ( "address");} if (ValidationHelper.ValidateTcpPort (port)) {throw new ArgumentOutOfRangeException ( "port");!} IPEndPoint remoteEP = new IPEndPoint (ADRESS, PORT); Connect (Remoteep);} /// //// /// /// Connect The Client to the specified end point. /// /// public void Connect (IPEndPoint remoteEP) {if (m_CleanedUp) {throw new ObjectDisposedException (this.GetType () FullName.);} if (remoteEP == null) {throw new ArgumentNullException ( "remoteEP");} Client.Connect (remoteEP ); M_active = true;}

/// /// /// /// returns the stream used to read and write data to the /// remote host. /// /// public NetworkStream GetStream () {if (m_CleanedUp) {throw new ObjectDisposedException (this.GetType ( ) .FullName);} if (Client.Connected) {throw new InvalidOperationException (SR.GetString (SR.net_notconnected));!} if (m_DataStream == null) {m_DataStream = new NetworkStream (Client, true); m_DataStreamCreated = true } Return m_datastream;} /// /// /// /// disposes the TCP connection. /// /// // UEUE PUB Lic void close () {GlobalLog.print ("TcpClient :: close ()"); (iDisposable) .dispose ();}

Private bool m_cleanedup = false;

/// protected virtual void dispose (bool disposing) {i (m_cleanedup) {Return }

IF (Disposing) {// no managed Objects to cleanup}

if (! m_DataStreamCreated) {// // if the NetworkStream was not created, the Socket might // still be there and needs to be closed. In the case in which // we are bound to a local IPEndPoint this will remove the // binding and free up the IPEndPoint for later uses // Socket chkClientSocket = Client;. if (chkClientSocket = null!) {chkClientSocket.InternalShutdown (SocketShutdown.Both); chkClientSocket.Close (); Client = null;}} // // wether the NetworkStream was created or not, we need to // lose its internal reference so that this TcpClient can be // collected and the NetworkStream can still be used. // note that the NetworkStream was created as owning the Socket, / / sowe Lose the Internal Reference to The Socket Above AS Well. // m_datastream = null; m_cleanedup = true;} /// /// void idisposable.dispose () {dispose (true); gc.suppressfinalize (this);}

/// ~ tclient () {Dispose (false);}

/// /// /// /// gets or sets the size of the receive buffer in bytes /// /// public int ReceiveBufferSize {get {return numericOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);.} set {Client.SetSocketOption (Socketoptionname.socket, socketoptionname.receivebuffer, value);}} /// / // /// /// Gets OR /// sets the size of the send buffer in bytes. /// /// public int sendbuffitsize {get {return NumericOption (socketoptionledLevel.socket, Socketoptionname.sendbuffer;

Set {Client.setSocketoption (socketoptionne.Socket, socketoptionname.sendbuffer, value);}}

/// /// /// /// gets or sets the receive time out value of the connection in seconds /// /// public int ReceiveTimeout {get {return numericOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);.} set {Client .SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, value);}} /// /// /// /// Gets or sets the send time out value of the connection in seconds. /// /// public int sendtimeout {get { Return NumericOption ET, SocketOptionName.sendtimeout;

Set {Client.setSocketoption (socketoptionne.Socket, socketoptionname.sendtimeout, value);}}

/// /// /// ///// gets or sets the value of the connection's linger option /// /// public LingerOption LingerState {get {return (LingerOption) Client.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger);.} set {Client.setSocketOption (socketoptionname.linger, value);}} /// /// /// Enables or disables delay when desnd or limited buffers are full. /// /// public bool nodelay {get { Return NumericOption (SocketOptionLevel.tcp, socketoptionname.nodelay)! = 0? true: false;} set { Client.setSocketoption (socketoptionlevel.tcp, socketoptionname.nodelay, value? 1: 0);}}

Private void initialize () {client = new socket (addressfamily.internetwork, sockettype.stream, protocoltype.tcp); m_active = false;}

Private Int NumericOption (Socketoptionname Optionname) {Return (int) client.getsocketoption (optionLevel, Optionname);

}; // Class TcpClient

} // namespace system.net.sockets

转载请注明原文地址:https://www.9cbs.com/read-40754.html

New Post(0)