.NET中TCPClient的源代码

TcpClient是.NET Framework提供的一种更高层次的TCP服务类,它用于建立到远程主机的客户端连接。该类提供了多种构造函数,允许指定本地端点、连接远程主机的IP和端口等。TcpClient包含了一个Socket对象,并提供了连接、断开、获取数据流等方法。通过GetStream方法,可以获取用于读写数据到远程主机的NetworkStream。
摘要由CSDN通过智能技术生成

//------------------------------------------------------------------------------
// <copyright file="TCPClient.cs" company="Microsoft">
//    
//      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.
//    
// </copyright>
//------------------------------------------------------------------------------

namespace System.Net.Sockets {

    /// <include file='doc/TCPClient.uex' path='docs/doc[@for="TcpClient"]/*' />
    /// <devdoc>
    /// <para>The <see cref='System.Net.Sockets.TcpClient'/> class provide TCP services at a higher level
    ///    of abstraction than the <see cref='System.Net.Sockets.Socket'/> class. <see cref='System.Net.Sockets.TcpClient'/>
    ///    is used to create a Client connection to a remote host.</para>
    /// </devdoc>
    public class TcpClient : IDisposable {

        Socket m_ClientSocket;
        bool m_Active;
        NetworkStream m_DataStream;
        bool m_DataStreamCreated;

        // specify local IP and port
        /// <include file='doc/TCPClient.uex' path='docs/doc[@for="TcpClient.TcpClient"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Initializes a new instance of the <see cref='System.Net.Sockets.TcpClient'/>
        ///       class with the specified end point.
        ///    </para>
        /// </devdoc>
        public TcpClient(IPEndPoint localEP) {
            if (localEP==null) {
                throw new ArgumentNullException("localEP");
            }
            initialize();
            Client.Bind(localEP);
        }

        // TcpClient(IPaddress localaddr); // port is arbitrary
        // TcpClient(int outgoingPort); // local IP is arbitrary

        // address+port is arbitrary
        /// <include file='doc/TCPClient.uex' path='docs/doc[@for="TcpClient.TcpClient1"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Initializes a new instance of the <see cref='System.Net.Sockets.TcpClient'/> class.
        ///    </para>
        /// </devdoc>
        public TcpClient() {
            initialize();
        }

        // bind and connect
        /// <include file='doc/TCPClient.uex' path='docs/doc[@for="TcpClient.TcpClient2"]/*' />
        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Net.Sockets.TcpClient'/> class and connects to the
        ///    specified port on the

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值