應用程序可以通過 TCPClient、TCPListener 和 UDPClient 類使用傳輸控制協議 (TCP) 和用戶數據文報協議 (UDP) 服務。這些協議類建立在 System.Net.Sockets.Socket 類的基礎之上,負責數據傳送的細節。(也就是說TCPClient、TCPListener 和 UDPClient 類是用來簡化Socket)
TcpClient 和 TcpListener 使用 NetworkStream 類表示網絡。使用 GetStream 方法返回網絡流,然后調用該流的 Read 和 Write 方法。NetworkStream 不擁有協議類的基礎套接字,因此關閉它並不影響套接字。
UdpClient 類使用字節數組保存 UDP 數據文報。使用 Send 方法向網絡發送數據,使用 Receive 方法接收傳入的數據文報。
TcpListener
TcpListener 類提供一些簡單方法,用於在阻止同步模式下偵聽和接受傳入連接請求。可使用 TcpClient 或 Socket 來連接 TcpListener。可使用 IPEndPoint、本地 IP 地址及端口號或者僅使用端口號,來創建 TcpListener。可以將本地 IP 地址指定為Any,將本地端口號指定為 0(如果希望基礎服務提供程序為您分配這些值)。如果您選擇這樣做,可在連接套接字后使用LocalEndpoint 屬性來標識已指定的信息。
Start 方法用來開始偵聽傳入的連接請求。Start 將對傳入連接進行排隊,直至您調用 Stop 方法或它已經完成 MaxConnections 排隊為止。可使用 AcceptSocket 或 AcceptTcpClient 從傳入連接請求隊列提取連接。這兩種方法將阻止。如果要避免阻止,可首先使用 Pending 方法來確定隊列中是否有可用的連接請求。
調用 Stop 方法來關閉 TcpListener。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace _024_tcplistener {
class Program {
static void Main(string[] args) {
//1,TcpListener對socket進行了一層封裝,這個類里面自己會去創建socket對象
TcpListener listener = new TcpListener(IPAddress.Parse("192.168.1.101"),7788);
//2,開始進行監聽
listener.Start();
//3,等待客戶端連接過來
TcpClient client = listener.AcceptTcpClient();
//4,取得客戶端發送過來的數據
NetworkStream stream = client.GetStream();//得到了一個網絡流 從這個網絡流可以取得客戶端發送過來的數據
byte[] data = new byte[1024];//創建一個數據的容器,用來承接數據
while (true)
{
//0 表示從數組的哪個索引開始存放數據
//1024表示最大讀取的字節數
int length = stream.Read(data, 0, 1024);//讀取數據
string message = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine("收到了消息:" + message);
}
stream.Close();
client.Close();
listener.Stop();//停止監聽
Console.ReadKey();
}
}
}
TcpClient
TcpClient 類提供了一些簡單的方法,用於在同步阻止模式下通過網絡來連接、發送和接收流數據。為使 TcpClient 連接並交換數據,使用 TCP ProtocolType 創建的 TcpListener 或 Socket 必須偵聽是否有傳入的連接請求。可以使用下面兩種方法之一連接到該偵聽器:
(1)創建一個 TcpClient,並調用三個可用的 Connect 方法之一。
(2)使用遠程主機的主機名和端口號創建 TcpClient。此構造函數將自動嘗試一個連接。
給繼承者的說明要發送和接收數據,請使用 GetStream 方法來獲取一個 NetworkStream。調用 NetworkStream 的 Write 和Read 方法與遠程主機之間發送和接收數據。使用 Close 方法釋放與 TcpClient 關聯的所有資源。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _003_tcpclient {
class Program {
static void Main(string[] args) {
//當我們創建tcpclient對象的時候,就會跟server去建立連接
TcpClient client = new TcpClient("192.168.1.101",7788);
NetworkStream stream = client.GetStream();//通過網絡流進行數據的交換
//read用來讀取數據,write用來寫入數據其實就是發送數據
//利用一個死循環,重復向服務器端發送數據
while (true)
{
string message = Console.ReadLine();
byte[] data = Encoding.UTF8.GetBytes(message);
stream.Write(data, 0, data.Length);
}
stream.Close();
client.Close();
Console.ReadKey();
}
}
}
UdpClient
UdpClient 類提供了一些簡單的方法,用於在阻止同步模式下發送和接收無連接 UDP 數據報。因為 UDP 是無連接傳輸協議,所以不需要在發送和接收數據前建立遠程主機連接。但您可以選擇使用下面兩種方法之一來建立默認遠程主機:
使用遠程主機名和端口號作為參數創建 UdpClient 類的實例。
創建 UdpClient 類的實例,然后調用 Connect 方法。
可以使用在 UdpClient 中提供的任何一種發送方法將數據發送到遠程設備。使用 Receive 方法可以從遠程主機接收數據。
UdpClient 方法還允許發送和接收多路廣播數據報。使用 JoinMulticastGroup 方法可以將 UdpClient 預訂給多路廣播組。使用 DropMulticastGroup 方法可以從多路廣播組中取消對 UdpClient 的預訂。
服務器端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _025_udpclient {
class Program {
static void Main(string[] args) {
//創建udpclient 綁定ip跟端口號
UdpClient udpClient = new UdpClient(new IPEndPoint(IPAddress.Parse("192.168.1.101"),7788));
while (true)
{
//接收數據
IPEndPoint point = new IPEndPoint(IPAddress.Any, 0);
byte[] data = udpClient.Receive(ref point);//通過point確定數據來自哪個ip的哪個端口號 返回值是一個字節數組,就是我們的數據
string message = Encoding.UTF8.GetString(data);
Console.WriteLine("收到了消息:" + message);
}
udpClient.Close();
Console.ReadKey();
}
}
}
客戶端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
namespace _004_udpclient {
class Program {
static void Main(string[] args) {
//創建udpclient對象
UdpClient client = new UdpClient();
while (true)
{
string message = Console.ReadLine();
byte[] data = Encoding.UTF8.GetBytes(message);
client.Send(data, data.Length, new IPEndPoint(IPAddress.Parse("192.168.1.101"), 7788));
}
client.Close();
Console.ReadKey();
}
}
}