Client 端:
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_client
{
class Program
{
static void Main(string[] args)
{
String str1 = "AAAAAA\r\n";
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.100.1"), 5555);
Socket server = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
while(true)
{
server.SendTo(Encoding.UTF8.GetBytes(str1), ipep);
for (int j = 0; j < 10000; j++) ;
}
}
}
}
Server 端:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplicationserver
{
class Program
{
static void Main(string[] args)
{
//開啟伺服器的 5555 連接埠,用來接收傳送到本機的訊息
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,5555);
//建立接收的 socket,並使用Udp 的 Datagram 方式接收
Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
//綁定 Socket (network) 與 IPEndPoint(ipep),讓Socket 接收 5555 埠訊息
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client");
//建立Remote 物件以便取得封包的接收來源的 EndPoint 物件
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint Remote = (EndPoint)(sender);
while(true)
{
byte[] data=new byte[512];
int recv = newsock.ReceiveFrom(data, ref Remote);//接收對方傳來的封包
Console.WriteLine(Encoding.UTF8.GetString(data, 0, recv));
}
}
}
}
成功 !!
另一本書找到的範例:
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication_UDP
{
public partial class Form1 : Form
{
UdpClient U;
Thread Th;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
Th = new Thread(Listen);
Th.Start();
button1.Enabled = false;
}
//找出本機 IP
private string MyIP()
{
string hn = Dns.GetHostName(); //取得本機電腦名稱
IPAddress[] ip = Dns.GetHostEntry(hn).AddressList;//取得本機IP陣列(Maybe 多個)
foreach (IPAddress it in ip)
{
if (it.AddressFamily == AddressFamily.InterNetwork)
{
return it.ToString(); //回傳此IP字串
}
}
return " ";//找不到合格IP,回傳空字串
}
private void Listen()
{
int Port = int.Parse(textBox1.Text);
U = new UdpClient(Port);
IPEndPoint EP = new IPEndPoint(IPAddress.Parse("127.0.0.1"),Port);
while (true)
{
byte[] B = U.Receive(ref EP);
textBox2.Text = Encoding.Default.GetString(B);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
Th.Abort();
U.Close();
}
catch
{
}
}
private void button2_Click(object sender, EventArgs e)
{
string IP = textBox3.Text;
int Port = int.Parse(textBox4.Text);
byte[] B = Encoding.Default.GetBytes(textBox5.Text);
UdpClient S = new UdpClient();
S.Send(B, B.Length, IP, Port);
S.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text += " " + MyIP();
}
}
}
執行畫面:
另一本書找到的範例:
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace WindowsFormsApplication_UDP
{
public partial class Form1 : Form
{
UdpClient U;
Thread Th;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
Th = new Thread(Listen);
Th.Start();
button1.Enabled = false;
}
//找出本機 IP
private string MyIP()
{
string hn = Dns.GetHostName(); //取得本機電腦名稱
IPAddress[] ip = Dns.GetHostEntry(hn).AddressList;//取得本機IP陣列(Maybe 多個)
foreach (IPAddress it in ip)
{
if (it.AddressFamily == AddressFamily.InterNetwork)
{
return it.ToString(); //回傳此IP字串
}
}
return " ";//找不到合格IP,回傳空字串
}
private void Listen()
{
int Port = int.Parse(textBox1.Text);
U = new UdpClient(Port);
IPEndPoint EP = new IPEndPoint(IPAddress.Parse("127.0.0.1"),Port);
while (true)
{
byte[] B = U.Receive(ref EP);
textBox2.Text = Encoding.Default.GetString(B);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
Th.Abort();
U.Close();
}
catch
{
}
}
private void button2_Click(object sender, EventArgs e)
{
string IP = textBox3.Text;
int Port = int.Parse(textBox4.Text);
byte[] B = Encoding.Default.GetBytes(textBox5.Text);
UdpClient S = new UdpClient();
S.Send(B, B.Length, IP, Port);
S.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text += " " + MyIP();
}
}
}
跑的蠻順的....@@
沒有留言:
張貼留言