? ? ? ?在選擇使用協(xié)議的時候,選擇UDP必須要謹慎。在網(wǎng)絡(luò)質(zhì)量令人十分不滿意的環(huán)境下,UDP協(xié)議數(shù)據(jù)包丟失會比較嚴重。但是由于UDP的特性:它不屬于連接型協(xié)議,因而具有資源消耗小,處理速度快的優(yōu)點,所以通常音頻、視頻和普通數(shù)據(jù)在傳送時使用UDP較多,因為它們即使偶爾丟失一兩個數(shù)據(jù)包,也不會對接收結(jié)果產(chǎn)生太大影響。比如我們聊天用的ICQ和QQ就是使用的UDP協(xié)議。
服務(wù)端代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace UDP服務(wù)器
{
public partial class Form1 : Form
{
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
textBox1.Text = "127.0.0.1";
textBox2.Text = "8401";
}
/// <summary>
/// 獲取本地IP
/// </summary>
private void label1_Click(object sender, EventArgs e)
{
string ip = IPAddress.Any.ToString();
textBox1.Text = ip;
}
Socket server;
private void button2_Click(object sender, EventArgs e)
{
server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
server.Bind(new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)));//綁定端口號和IP
//server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
listBox1.Items.Add("服務(wù)器已經(jīng)成功開啟!");
//開啟接收消息線程
Thread t = new Thread(ReciveMsg);
t.IsBackground = true;
t.Start();
}
/// <summary>
/// 向特定ip的主機的端口發(fā)送數(shù)據(jù)
/// </summary>te
void SendMsg()
{
EndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8400); //向指定的IP和端口發(fā)送消息
string msg = textBox3.Text;
server.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
/// <summary>
/// 接收發(fā)送給本機ip對應(yīng)端口號的數(shù)據(jù)
/// </summary>
void ReciveMsg()
{
while (true)
{
EndPoint point = new IPEndPoint(IPAddress.Any, 0);//用來保存發(fā)送方的ip和端口號
byte[] buffer = new byte[1024 * 1024];
int length = server.ReceiveFrom(buffer, ref point);//接收數(shù)據(jù)報
string message = Encoding.UTF8.GetString(buffer, 0, length);
listBox1.Items.Add(point.ToString() + ":" + message);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox3.Text != "")
{
//開啟發(fā)送消息線程
Thread t3 = new Thread(SendMsg);
t3.Start();
listBox1.Items.Add(textBox1.Text + ":" + textBox2.Text + ":" + textBox3.Text);
}
}
}
}
客戶端代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace UDP客戶端
{
public partial class Form1 : Form
{
public Form1()
{
CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
textBox1.Text = "127.0.0.1";
textBox2.Text = "8400";
}
/// <summary>
/// 創(chuàng)建客戶端
/// </summary>
Socket client;
private void button2_Click(object sender, EventArgs e)
{
///創(chuàng)建客戶端
client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.Bind(new IPEndPoint(IPAddress.Parse(textBox1.Text), int.Parse(textBox2.Text)));
///線程問題
Thread thread = new Thread(ReciveMsg);
thread.IsBackground = true;
thread.Start(client);
listBox1.Items.Add("客戶端已成功開啟!");
}
/// <summary>
/// 向特定ip的主機的端口發(fā)送數(shù)據(jù)
/// </summary>
void SendMsg()
{
EndPoint point = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8401); //向指定的IP和端口發(fā)送消息
///發(fā)送內(nèi)容
string msg = textBox3.Text;
///將數(shù)據(jù)發(fā)送到指定的ip的主機的端口
client.SendTo(Encoding.UTF8.GetBytes(msg), point);
}
/// <summary>
/// 接收發(fā)送給本機ip對應(yīng)端口號的數(shù)據(jù)
/// </summary>
void ReciveMsg(object o)
{
while (true)
{
try
{
///用來保存發(fā)送方的ip和端口號
EndPoint point = new IPEndPoint(IPAddress.Any, 0);
//MessageBox.Show(point.ToString());
///定義客戶端接收到的信息大小
byte[] buffer = new byte[1024 * 1024];
///接收到的信息大小(所占字節(jié)數(shù))
int length = client.ReceiveFrom(buffer, ref point);
string message = Encoding.UTF8.GetString(buffer, 0, length);
listBox1.Items.Add(point.ToString() + ":" + message);
}
catch (Exception)
{
client.Close();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox3.Text != "")
{
//開啟發(fā)送消息線程
Thread t2 = new Thread(SendMsg);
t2.Start();
listBox1.Items.Add(textBox1.Text + ":" + textBox2.Text + ":" + textBox3.Text);
}
}
}
}
效果展示
代碼下載地址:文章來源:http://www.zghlxwxcb.cn/news/detail-525583.html
鏈接:https://pan.baidu.com/s/1r-V80I5fJ-8noF8YgMOlLA?
提取碼:qx3s文章來源地址http://www.zghlxwxcb.cn/news/detail-525583.html
到了這里,關(guān)于C# 使用UDP進行網(wǎng)絡(luò)通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!