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

C#中使用UDP通信
UDP通信是無連接通信,客戶端在發(fā)送數(shù)據(jù)前無需與服務(wù)器端建立連接,即使服務(wù)器端不在線也可以發(fā)送,但是不能保證服務(wù)器端可以收到數(shù)據(jù)。
服務(wù)器端代碼:
static void Main(string[] args)
{
UdpClient client = null;
string receiveString = null;
byte[] receiveData = null;
//實例化一個遠程端點,IP和端口可以隨意指定,等調(diào)用client.Receive(ref remotePoint)時會將該端點改成真正發(fā)送端端點
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);//實例化一個遠程端點
while (true)
{
sendString = Console.ReadLine();
sendData = Encoding.Default.GetBytes(sendString);
client = new UdpClient();
client.Send(sendData, sendData.Length, remotePoint);//將數(shù)據(jù)發(fā)送到遠程端點
client.Close();//關(guān)閉連接
}
}

相關(guān)文章
一文帶你快速學(xué)會C#中WinForm框架的使用詳解
WinForm是一門非常經(jīng)濟實惠的技術(shù),就是說,可以在短時間內(nèi)學(xué)會,并迅速借此進行項目開發(fā)。本文就來和大家聊聊WinForm框架的使用方法,希望對大家有所幫助2023-02-02
C# Csv實現(xiàn)基本的讀寫和轉(zhuǎn)換DataTable
本文主要介紹了C# Csv實現(xiàn)基本的讀寫和轉(zhuǎn)換DataTable,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
C#實現(xiàn)文件與字符串互轉(zhuǎn)的方法詳解
這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)文件與字符串互轉(zhuǎn)效果,文中的示例代碼講解詳細,對我們學(xué)習(xí)C#有一定幫助,需要的可以參考一下2022-08-08
Unity 實現(xiàn)框選游戲戰(zhàn)斗單位的思路詳解
這篇文章主要介紹了Unity 如何實現(xiàn)框選游戲戰(zhàn)斗單位,本文簡單介紹如何實現(xiàn)即時戰(zhàn)略游戲中框選戰(zhàn)斗單位的功能,需要的朋友可以參考下2022-12-12
C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法
這篇文章主要介紹了C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及表的方法,以實例形式分析了創(chuàng)建access數(shù)據(jù)庫及在access數(shù)據(jù)庫中建表的完整過程,是非常實用的技巧,需要的朋友可以參考下2014-12-12

