2010-03-17 112 views
0

不久前,我創建了一個類來處理我的局域網聯網程序。我最近將我的一臺筆記本電腦升級到了Windows 7,並且重新設置了Windows 7(或者至少我設置的方式)只支持IPv6,但是我的桌面仍然在Windows XP中運行,只使用IPv4。我創建的類使用UdpClient類,並且目前設置爲僅與IPv4一起工作。有沒有辦法修改我的代碼以允許發送和接收IPv6和IPv4數據包? 這將是很難放棄類代碼,我的很多程序依賴於這個類。我想讓課程保持原來的狀態,所以我不需要修改舊的程序,只需要更新舊的程序。在IPv4和IPv6中使用UdpClient?

感謝您的任何和所有幫助, 最大

發送:

using System.Net.Sockets;UdpClient tub = new UdpClient(); 
    tub.Connect (new IPEndPoint (ToIP, ToPort)); 
    UdpState s = new UdpState(); 
    s.client = tub; 
    s.endpoint = new IPEndPoint (ToIP, ToPort); 

    tub.BeginSend (data, data.Length, new AsyncCallback (SendCallBack),s); 

    private void SendCallBack (IAsyncResult result) 
    { 
     UdpClient client = (UdpClient)((UdpState)(result.AsyncState)).client; 
     IPEndPoint endpoint = (IPEndPoint)((UdpState)(result.AsyncState)).endpoint; 
     client.EndSend (result); 
    } 

接收:

UdpClient tub = new UdpClient (ReceivePort); 

    UdpState s = new UdpState(); 
    s.client = tub; 
    s.endpoint = new IPEndPoint (ReceiveIP, ReceivePort); 
    s.callback = cb; 
    tub.BeginReceive (new AsyncCallback (receivedPacket), s); 

    public void receivedPacket (IAsyncResult result) 
    { 
     UdpClient client = (UdpClient)((UdpState)(result.AsyncState)).client; 
     IPEndPoint endpoint = (IPEndPoint)((UdpState)(result.AsyncState)).endpoint; 
     Byte[] receiveBytes = client.EndReceive (result, ref endpoint); 
     Packet ThePacket = new Packet (receiveBytes); 
     client.Close(); 
     //Do what ever with the 'ThePacket' now 
    } 

回答

0
+0

我看到的是,我無法訪問谷歌圖書... – Ben 2010-03-17 21:18:06

+0

這也是答案保留了http://blogs.msdn.com/malarch/archive/2005/11/18/494769.aspx的鏈接,這種鏈接完全符合你的要求 - 它啓動一個可以接受IPv4和IPv6連接的套接字。 – Ragesh 2010-03-18 02:53:14