C# TCP 客戶端 2 種 (最小精簡) 寫法:
https://lolikitty.pixnet.net/blog/post/112329777
https://www.delftstack.com/howto/csharp/get-local-ip-address-in-csharp/
書本上操寫的範例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace ConsoleApplication_Internet_1
{
class Program
{
static void Main(string[] args)
{
//create IP address
IPAddress ipAddr = IPAddress.Parse("210.59.154.30");
Console.WriteLine("ipAddr=" + ipAddr);
//create IP endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 80);
Console.WriteLine("ipEndPoint" + ipEndPoint);
// IP Endpoint 序列化為 SocketAddress
SocketAddress socketAddr = ipEndPoint.Serialize();
Console.WriteLine("socketAddr=" + socketAddr);
Console.ReadKey();
}
}
}
繼續 key.....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication_Internet_1
{
class Program
{
static void Main(string[] args)
{
//create IP address
IPAddress ipAddr = IPAddress.Parse("210.59.154.30");
Console.WriteLine("ipAddr=" + ipAddr);
//create IP endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 80);
Console.WriteLine("ipEndPoint" + ipEndPoint);
// IP Endpoint 序列化為 SocketAddress
SocketAddress socketAddr = ipEndPoint.Serialize();
Console.WriteLine("socketAddr = " + socketAddr);
//透過 DNS 找尋IP位址相對應之主機名稱
IPHostEntry remoteHostEntry = Dns.GetHostEntry(ipAddr);
Console.WriteLine("host of ip:" + ipAddr + "is" + remoteHostEntry.HostName);
//由於主機有可能有一個以上的 Alias
//因此程式中以迴圈方式判斷 Alias
string[] aliasList = remoteHostEntry.Aliases;
for(int i=0;i<=aliasList.Length-1;i++)
{
Console.WriteLine("Alias " + i + " : " + aliasList[i]);
}
//由於主機有可能有一個以上的IP address
//因此主程式中以迴圈方式判斷 AddressList
IPAddress[] addrList = remoteHostEntry.AddressList;
for(int i=0;i<addrList.Length-1;i++)
{
Console.WriteLine("Address " + i + " : " + addrList[i]);
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
namespace ConsoleApplication_Internet_1
{
class Program
{
static void Main(string[] args)
{
//create IP address
IPAddress ipAddr = IPAddress.Parse("210.59.154.30");
Console.WriteLine("ipAddr=" + ipAddr);
//create IP endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 80);
Console.WriteLine("ipEndPoint" + ipEndPoint);
// IP Endpoint 序列化為 SocketAddress
SocketAddress socketAddr = ipEndPoint.Serialize();
Console.WriteLine("socketAddr=" + socketAddr);
Console.ReadKey();
}
}
}
執行結果:
繼續 key.....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication_Internet_1
{
class Program
{
static void Main(string[] args)
{
//create IP address
IPAddress ipAddr = IPAddress.Parse("210.59.154.30");
Console.WriteLine("ipAddr=" + ipAddr);
//create IP endpoint
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 80);
Console.WriteLine("ipEndPoint" + ipEndPoint);
// IP Endpoint 序列化為 SocketAddress
SocketAddress socketAddr = ipEndPoint.Serialize();
Console.WriteLine("socketAddr = " + socketAddr);
//透過 DNS 找尋IP位址相對應之主機名稱
IPHostEntry remoteHostEntry = Dns.GetHostEntry(ipAddr);
Console.WriteLine("host of ip:" + ipAddr + "is" + remoteHostEntry.HostName);
//由於主機有可能有一個以上的 Alias
//因此程式中以迴圈方式判斷 Alias
string[] aliasList = remoteHostEntry.Aliases;
for(int i=0;i<=aliasList.Length-1;i++)
{
Console.WriteLine("Alias " + i + " : " + aliasList[i]);
}
//由於主機有可能有一個以上的IP address
//因此主程式中以迴圈方式判斷 AddressList
IPAddress[] addrList = remoteHostEntry.AddressList;
for(int i=0;i<addrList.Length-1;i++)
{
Console.WriteLine("Address " + i + " : " + addrList[i]);
}
Console.ReadKey();
}
}
}
沒有留言:
張貼留言