开发者

Socket & access to many class with the same name

i listen to tcp port and when i received a first from source ip , then i create a new instance of special class for this new source ip

in the second packet from that source ip i do not need to create a new instance of class

my problem is how can i pass this second packet to that class i created for that source ip in particular although i created many classes for different sources ip

and if this is开发者_开发百科 a wrong way ,what is the best way to do that?

thanks in advance


Try using a Dictionary to store the mapping of IP addresses to processing objects. The class Session in the following code corresponds to your special processing class. The other classes and properties may need to be changed -- provide some of your code if you need more specifics.

private Dictionary<IPAddress,Session> activeSessions =  new Dictionary<IPAddress,Session>();

private void packetReceived(Packet pkt)
{
    Session curSession;
    if (!activeSessions.TryGetValue(pkt.SourceIP, out curSession))
    {
        curSession = new Session();
        activeSessions.Add(pkt.SourceIP, curSession);
    }

    curSession.ProcessPacket(pkt);
}


So you've got something that's listening in on a socket. When data comes in you inspect the source IP. If its a new IP, you instantiate an object to handle it. Going forward you want all subsequent packets from that source IP to go to the already instantiated class, right?

Just give your processing class a property such as SourceIp. In your class that is initially receiving the packets create an array/list of all instantiated classes. When a packet comes in, loop through the array and see if there's already an instantiated object.

UPDATE

I'm going to expand upon @Justin's code a little but I agree that a Dictionary is probably the best type. Let's say that you've got this class that process a packet:

class Processor
{
    public void ProcessPacket(Byte[] data)
    {
        //Your processing code here
    }
}

You first need to c In your code that receives the data I'm assuming you have both the data itself as well as the source IP. When data is received you look up the IP in the Dictionary and either create a new processor or re-use an existing one:

    //Holds our processor classes, each identified by IP
    private Dictionary<IPAddress, Processor> Processors = new Dictionary<IPAddress,Processor>();

    private void dataReceived(Byte[] data, IPAddress ip)
    {
        //If we don't already have the IP Address in our dictionary
        if(!Processors.ContainsKey(ip)){
            //Create a new processor object and add it to the dictionary
            Processors.Add(ip, new Processor());
        }
        //At this point we've either just added a processor for this IP
        //or there was one already in the dictionary so grab it based
        //on the IP
        Processor p = Processors[ip];
        //Tell it to process our data
        p.ProcessPacket(data);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜