我对C#编程非常陌生,我正在开发一个使用TUN-TAP网络适配器的应用程序。
我收到system.IO.IOException错误:连接到系统的设备无法运行。
要将数据从服务器接收到我的应用程序,我的编码简单如下。
public static void TunInterfaceThread()
{
const string UsermodeDeviceSpace = "\\\\.\\Global\\";
// Getting the device GUId
string devGuid = GetDeviceGuid();
Console.WriteLine("Device GUID: " + devGuid);
Console.WriteLine(HumanName(devGuid));
// Create a win32file for manipulating the TUN/TAP interface
IntPtr ptr = CreateFile(UsermodeDeviceSpace + devGuid + ".tap", FileAccess.ReadWrite,
FileShare.ReadWrite, 0, FileMode.Open, FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_OVERLAPPED, IntPtr.Zero);
// Have Windows consider the interface now connected
int ioctl_len;
IntPtr pstatus = Marshal.AllocHGlobal(4);
Marshal.WriteInt32(pstatus, 1);
DeviceIoControl(ptr, TAP_CONTROL_CODE(6, METHOD_BUFFERED) /* TAP_IOCTL_SET_MEDIA_STATUS */, pstatus, 4,
pstatus, 4, out ioctl_len, IntPtr.Zero);
// Configure with the right IP address + mask and switch to TUN mode (by default the interface runs in TAP mode)
IntPtr ptun = Marshal.AllocHGlobal(12);
Marshal.WriteInt32(ptun, 0, 0x0101a8c0); //192.168.1.1: IP address of the TUN interface
Marshal.WriteInt32(ptun, 4, 0x0001a8c0); //192.168.1.0: IP address of the TUN interface's network
Marshal.WriteInt32(ptun, 8, unchecked((int)0x00ffffff)); // 255.255.255.0: netmask of the TUN interface
DeviceIoControl(ptr, TAP_CONTROL_CODE(10, METHOD_BUFFERED) /* TAP_IOCTL_CONFIG_TUN */, ptun, 12,
ptun, 12, out ioctl_len, IntPtr.Zero);
SafeFileHandle handleValue = null;
handleValue = new SafeFileHandle(ptr, true);
Tap = new FileStream(handleValue, FileAccess.ReadWrite, FILE_STREAM_SIZE, true);
// A buffer used for storing packets as they come in
// Since this is single threaded, we are handling only one packet at a time
byte[] TUNBuffer = new byte[BUFFER_SIZE];
// Creating a state variable for reading and writing
object read_state = new int();
object write_state = new int();
// Creating event wait handles, one for reading and the other for writing
// and initializing them to be signaled automatically
ReadWaitObject = new EventWaitHandle(false, EventResetMode.AutoReset);
WriteWaitObject = new EventWaitHandle(false, EventResetMode.AutoReset);
// Creating two asynchronous callback functions
AsyncCallback readCallback = new AsyncCallback(ReadDataCallback);
AsyncCallback writeCallback = new AsyncCallback(WriteDataCallback);
IAsyncResult read_res, write_res;
int number_of_tun_msgs = 0;
int number_of_tcpRecv_msgs = 0;
while (true)
{
{
// Begin an asynchronous read. The callback function readCallback() will be called
try
{
read_res = Tap.BeginRead(TUNBuffer, 0, BUFFER_SIZE, readCallback, read_state);
// blocks the current thread until the read is complete and the event wait handle is signaled with ReadWaitObject.Set()
ReadWaitObject.WaitOne();
}
catch (Exception e)
{
Console.WriteLine("Tap read exception " + e.ToString());
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.Read();
}
}
Thread.Sleep(100);
}
}
收到数据后,我将其复制到缓冲区并进行其他处理。
这个方法有一段时间,在一段时间后我收到IO异常错误,我不得不重启我的应用程序才能工作。
请帮助我了解IO异常错误背后的主要原因是什么?是我的应用程序有问题还是在机器上运行的TAP-Adapter有一些问题。