Erreur de NullPointerException avec un deleguate void
Bonjour,
Je suis en train de faire une .dll qui intercepte le Trafic de l'ordinateur.
Voici mes codes:
L'erreur est situé dans la classe AddressHandler, où c'est écrit // ERREUR NULLPOINTEREXCEPTION ICI À EVENT(P);
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
using System.Net;
using System.Net.Sockets;
namespace PacketReceiver
{
// This is the event handler. It takes a void
// that as for parameter a Packet which contains
// received Packet's data.
public delegate void PacketReceivedHandler(Packet packet);
// Every time a Packet is caught it will be treated
// by this class and then sent as parameter throught
// OnReceive.
public class PacketReceiver
{
// This is the event, every time a Packet is caught
// the class will fire this event.
public event PacketReceivedHandler OnReceive;
// Hosts on the current platform.
// There might be more than 1 address
// on the same platform.
private IPAddress[] Hosts;
// Handle the hosts.
private AddressHandler[] Handlers;
public PacketReceiver()
{
Hosts = Dns.Resolve(Dns.GetHostName()).AddressList;
Handlers = new AddressHandler[Hosts.Length];
if (Hosts.Length == 0)
{
return;
}
for (int i = 0; i < Hosts.Length; i++)
{
Handlers[i] = new AddressHandler(OnReceive, Hosts[i]);
}
}
public void StartReceiving()
{
for (int i = 0; i < Handlers.Length; i++)
Handlers[i].StartReceiving();
}
public void StopReceiving()
{
for (int i = 0; i < Handlers.Length; i++)
Handlers[i].StopReceiving();
}
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
using System;
using System.Net;
using System.Net.Sockets;
namespace PacketReceiver
{
class AddressHandler
{
// This is the OnReceive event
// is this class, and it's going to
// be called when a Packet is caught.
private event PacketReceivedHandler Event;
// Handle the host passed as parameter.
private IPAddress Host;
// Handle bytes of incomming Packet.
private byte[] Buffer;
// Socket that handles incomming Packets.
private Socket Entry;
public AddressHandler(PacketReceivedHandler evt, IPAddress host)
{
Event = evt;
Host = host;
Buffer = new byte[65535];
}
public void StartReceiving()
{
Entry = new Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
Entry.Bind(new IPEndPoint(Host, 0));
Entry.IOControl(-2147483648 | 0x18000000 | 1, BitConverter.GetBytes((int)1), null);
Entry.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.PacketReceived), null);
}
public void StopReceiving()
{
Entry.Close();
Entry = null;
}
private void PacketReceived(IAsyncResult iar)
{
int received = Entry.EndReceive(iar);
byte[] packet = new byte[received];
Array.Copy(Buffer, 0, packet, 0, received);
Packet p = new Packet(packet);
// ERREUR DE NULLPOINTEREXCEPTION ICI À EVENT(P);
Event(p);
Entry.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, new AsyncCallback(this.PacketReceived), null);
}
}
} |
Merci de m'aider.