本文整理匯總了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();
}
}
}