0
我是網絡編程的新手,我遇到了一些我一直在測試的代碼作爲局域網聊天程序基礎的問題。非接收UDP問題
服務器代碼:
public static void Main()
{
UdpClient publisher = new UdpClient("230.0.0.1", 8899);
UdpClient subscriber = new UdpClient("230.0.0.2", 8800);
IPAddress addr = IPAddress.Parse("230.0.0.1");
subscriber.JoinMulticastGroup(addr);
Console.WriteLine("Running chat program at 230.0.0.1:8899");
while (true)
{
IPEndPoint ep = null;
byte[] chats = subscriber.Receive(ref ep);
string chatstring = Encoding.ASCII.GetString(chats);
Console.WriteLine(chatstring);
string msg = String.Format(chatstring);
byte[] sdata = Encoding.ASCII.GetBytes(msg);
publisher.Send(sdata, sdata.Length);
System.Threading.Thread.Sleep(500);
}
}
該客戶端程序:
static void Main(string[] args)
{
UdpClient subscriber = new UdpClient("230.0.0.1", 8899);
IPAddress addr = IPAddress.Parse("230.0.0.1");
subscriber.JoinMulticastGroup(addr);
IPEndPoint ep = null;
Thread SendChats = new Thread(Send);
SendChats.Start();
while (true)
{
byte[] receivedbytes = subscriber.Receive(ref ep);
string receivedchats = Encoding.ASCII.GetString(receivedbytes);
Console.WriteLine(receivedchats);
Thread.Sleep(500);
}
}
static void Send()
{
UdpClient publisher = new UdpClient("230.0.0.2", 8800);
while (true)
{
string msg = Console.ReadLine();
byte[] sdata = Encoding.ASCII.GetBytes(msg);
publisher.Send(sdata, sdata.Length);
Thread.Sleep(400);
}
}
通過我的盤算,服務器程序應該從客戶端接收數據,但很可惜,一個消息在輸入後並交付,從來沒有任何事情可以通過。我錯過了什麼嗎?
防止路由器?當這隻在局域網上的兩臺或更多臺計算機之間運行時... ...和捕獲的TY。我刪除了用戶的地址,並更改了addr變量。 – Bloodyaugust 2009-12-18 19:52:11
沒有probs。如果您嘗試通過不同的LAN網段發送組播數據包,路由器/交換機問題纔會起作用。如果客戶端和服務器在同一個LAN上,那麼這是無關緊要的。 – Paolo 2009-12-19 13:39:43