// 伺服器程式碼 -------------------------------------------------------------------------------
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace g
{
class MainClass
{
public static void Main(string[] args)
{
UdpClient uc = new UdpClient(80);
// 公開IP
// IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("114.47.91.248"), 80);
// 內部IP,但外部還是可以連近來
IPEndPoint ipep = new IPEndPoint (IPAddress.Parse ("192.168.0.80"), 80);
Console.WriteLine("伺服器已啟動於 : " + ipep);
while (true)
{
Console.WriteLine(System.Text.Encoding.UTF8.GetString(uc.Receive(ref ipep)));
}
}
}
}
// 客戶端程式碼 -------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
UdpClient uc = new UdpClient();
byte[] b = System.Text.Encoding.UTF8.GetBytes("Hello 你好 ~");
uc.Send(b, b.Length, new IPEndPoint(IPAddress.Parse("114.47.91.248"), 80));
}
}
}
全站熱搜
留言列表