当前位置: 首页>>代码示例>>C#>>正文


C# TcpClient.Connect方法代码示例

本文整理汇总了C#中System.Net.Sockets.TcpClient.Connect方法的典型用法代码示例。如果您正苦于以下问题:C# TcpClient.Connect方法的具体用法?C# TcpClient.Connect怎么用?C# TcpClient.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.TcpClient的用法示例。


在下文中一共展示了TcpClient.Connect方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TcpClient

//Uses a remote endpoint to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint (ipAddress, 11004);

tcpClient.Connect (ipEndPoint);
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:6,代码来源:TcpClient.Connect

示例2: TcpClient

//Uses the IP address and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
IPAddress ipAddress = Dns.GetHostEntry ("www.contoso.com").AddressList[0];

tcpClient.Connect (ipAddress, 11003);
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:5,代码来源:TcpClient.Connect

示例3: DoConnect

static void DoConnect(string host, int port)
{
    // Connect to the specified host.
    TcpClient t = new TcpClient(AddressFamily.InterNetwork);
      
    IPAddress[] IPAddresses = Dns.GetHostAddresses(host);

    Console.WriteLine("Establishing connection to {0}", host);
    t.Connect(IPAddresses, port);

    Console.WriteLine("Connection established");
}
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:12,代码来源:TcpClient.Connect

示例4: TcpClient

//Uses a host name and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);
开发者ID:.NET开发者,项目名称:System.Net.Sockets,代码行数:3,代码来源:TcpClient.Connect

示例5: Main

//引入命名空间
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MainClass 
{
    [STAThread]
    static void Main(string[] args)
    {
        TcpClient MyClient = new TcpClient();
        MyClient.Connect("localhost", 10000);
        NetworkStream MyNetStream = MyClient.GetStream();
            
        if(MyNetStream.CanWrite && MyNetStream.CanRead)
        {
            Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there");
            MyNetStream.Write(sendBytes, 0, sendBytes.Length);
      
            byte[] bytes = new byte[MyClient.ReceiveBufferSize];
            MyNetStream.Read(bytes, 0, (int) MyClient.ReceiveBufferSize);
    
            string returndata = Encoding.ASCII.GetString(bytes);
            Console.WriteLine("This is what the host returned to you: " + returndata);
        }else if (!MyNetStream.CanRead) {
            Console.WriteLine("You can not write data to this stream");
            MyClient.Close();
        }else if (!MyNetStream.CanWrite)
        {             
            Console.WriteLine("You can not read data from this stream");
            MyClient.Close();
        }   
    }   
}
开发者ID:C#程序员,项目名称:System.Net.Sockets,代码行数:35,代码来源:TcpClient.Connect


注:本文中的System.Net.Sockets.TcpClient.Connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。