不久前,我创建了一个类来处理我的 LAN 网络程序。我最近将我的一台笔记本电脑升级到了 Windows 7,并发现 Windows 7(或者至少我设置它的方式)只支持 IPv6,但我的桌面仍然回到 Windows xp 时代,并且只使用 IPv4。我创建的类使用 UdpClient 类,目前设置为仅适用于 IPv4。有没有办法修改我的代码以允许发送和接收 IPv6 和 IPv4 数据包?很难废弃类代码,我的很多程序都依赖于这个类。我想保持类接近它的原始状态,所以我不需要修改我的旧程序,只需将旧类切换为更新的类。
感谢您的所有帮助,马克斯
发送:
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
}