本文整理汇总了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);
示例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);
示例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");
}
示例4: TcpClient
//Uses a host name and port number to establish a socket connection.
TcpClient tcpClient = new TcpClient ();
tcpClient.Connect ("www.contoso.com", 11002);
示例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();
}
}
}