使用C#實(shí)現(xiàn)基于TCP和UDP協(xié)議的網(wǎng)絡(luò)通信程序的基本示例
C#中使用TCP通信
TCP通信需要通信雙方都在線,所以需要先啟動(dòng)服務(wù)端進(jìn)行監(jiān)聽(tīng),客戶端才能獲得連接,服務(wù)端代碼:
static void Main(string[] args)
{
TcpClient client = null;
NetworkStream stream = null;
byte[] buffer = null;
string receiveString = null;
IPAddress localIP = IPAddress.Parse("127.0.0.1");
int localPort = 11000;
TcpListener listener = new TcpListener(localIP, localPort);//用本地IP和端口實(shí)例化Listener
listener.Start();//開(kāi)始監(jiān)聽(tīng)
while (true)
{
client = listener.AcceptTcpClient();//接受一個(gè)Client
buffer = new byte[client.ReceiveBufferSize];
stream = client.GetStream();//獲取網(wǎng)絡(luò)流
stream.Read(buffer, 0, buffer.Length);//讀取網(wǎng)絡(luò)流中的數(shù)據(jù)
stream.Close();//關(guān)閉流
client.Close();//關(guān)閉Client
receiveString = Encoding.Default.GetString(buffer).Trim('\0');//轉(zhuǎn)換成字符串
Console.WriteLine(receiveString);
}
}
只有服務(wù)端開(kāi)啟監(jiān)聽(tīng)后,客戶端才能正確連接,所以服務(wù)端要一直開(kāi)啟監(jiān)聽(tīng),客戶端每次發(fā)送數(shù)據(jù),都要首先與服務(wù)端建立連接,連接建立完成后才進(jìn)行數(shù)據(jù)發(fā)送。客戶端代碼:
static void Main(string[] args)
{
string sendString = null;//要發(fā)送的字符串
byte[] sendData = null;//要發(fā)送的字節(jié)數(shù)組
TcpClient client = null;//TcpClient實(shí)例
NetworkStream stream = null;//網(wǎng)絡(luò)流
IPAddress remoteIP = IPAddress.Parse("127.0.0.1");//遠(yuǎn)程主機(jī)IP
int remotePort = 11000;//遠(yuǎn)程主機(jī)端口
while (true)//死循環(huán)
{
sendString = Console.ReadLine();//獲取要發(fā)送的字符串
sendData = Encoding.Default.GetBytes(sendString);//獲取要發(fā)送的字節(jié)數(shù)組
client = new TcpClient();//實(shí)例化TcpClient
try
{
client.Connect(remoteIP, remotePort);//連接遠(yuǎn)程主機(jī)
}
catch (System.Exception ex)
{
Console.WriteLine("連接超時(shí),服務(wù)器沒(méi)有響應(yīng)!");//連接失敗
Console.ReadKey();
return;
}
stream = client.GetStream();//獲取網(wǎng)絡(luò)流
stream.Write(sendData, 0, sendData.Length);//將數(shù)據(jù)寫(xiě)入網(wǎng)絡(luò)流
stream.Close();//關(guān)閉網(wǎng)絡(luò)流
client.Close();//關(guān)閉客戶端
}
}

C#中使用UDP通信
UDP通信是無(wú)連接通信,客戶端在發(fā)送數(shù)據(jù)前無(wú)需與服務(wù)器端建立連接,即使服務(wù)器端不在線也可以發(fā)送,但是不能保證服務(wù)器端可以收到數(shù)據(jù)。
服務(wù)器端代碼:
static void Main(string[] args)
{
UdpClient client = null;
string receiveString = null;
byte[] receiveData = null;
//實(shí)例化一個(gè)遠(yuǎn)程端點(diǎn),IP和端口可以隨意指定,等調(diào)用client.Receive(ref remotePoint)時(shí)會(huì)將該端點(diǎn)改成真正發(fā)送端端點(diǎn)
IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
client = new UdpClient(11000);
receiveData = client.Receive(ref remotePoint);//接收數(shù)據(jù)
receiveString = Encoding.Default.GetString(receiveData);
Console.WriteLine(receiveString);
client.Close();//關(guān)閉連接
}
}
客戶端代碼:
static void Main(string[] args)
{
string sendString = null;//要發(fā)送的字符串
byte[] sendData = null;//要發(fā)送的字節(jié)數(shù)組
UdpClient client = null;
IPAddress remoteIP = IPAddress.Parse("127.0.0.1");
int remotePort = 11000;
IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);//實(shí)例化一個(gè)遠(yuǎn)程端點(diǎn)
while (true)
{
sendString = Console.ReadLine();
sendData = Encoding.Default.GetBytes(sendString);
client = new UdpClient();
client.Send(sendData, sendData.Length, remotePoint);//將數(shù)據(jù)發(fā)送到遠(yuǎn)程端點(diǎn)
client.Close();//關(guān)閉連接
}
}

相關(guān)文章
使用C#9中records作為強(qiáng)類(lèi)型ID的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于使用C#9中records作為強(qiáng)類(lèi)型ID的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C# 通過(guò)反射初探ORM框架的實(shí)現(xiàn)原理(詳解)
下面小編就為大家分享一篇C# 通過(guò)反射初探ORM框架的實(shí)現(xiàn)原理詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
C#使用MiniExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)到Excel/CSV文件
MiniExcel是一個(gè)簡(jiǎn)單、高效避免OOM的.NET處理Excel查、寫(xiě)、填充數(shù)據(jù)的工具,這篇文章主要介紹了C#如何使用MiniExcel實(shí)現(xiàn)導(dǎo)入導(dǎo)出數(shù)據(jù)到Excel/CSV文件,需要的可以參考下2024-02-02
C#實(shí)現(xiàn)簡(jiǎn)單點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C# 結(jié)合 Javascript 測(cè)試獲取天氣信息
本文將介紹如何使用 C# 并結(jié)合 JavaScript 獲取天氣信息,獲取的數(shù)據(jù)來(lái)源于360瀏覽器首頁(yè)數(shù)據(jù),對(duì)C# 獲取天氣信息示例代碼感興趣的朋友一起看看吧2024-08-08
C#?委托與?Lambda?表達(dá)式轉(zhuǎn)換機(jī)制及弱事件模式下的生命周期詳解
本文介紹了C#委托和Lambda表達(dá)式的工作原理,包括委托的內(nèi)部結(jié)構(gòu)、Lambda表達(dá)式的轉(zhuǎn)換機(jī)制以及弱事件模式下的生命周期管理,感興趣的朋友一起看看吧2025-02-02

