Testing Network Applications C#
I'm working on a program that will have several users connected to each other via p2p. I would like to test run my program by opening several instances on one computer and have these instances communicate as though they are on separate machines (i.e: as if it's the real thing). Any 开发者_如何学JAVAidea how to approach this?
Thanks,
PM
One of the major issues with doing this is that the Windows Sockets API which .NET uses internally only allow for one application to bind to a given endpoint (IP address and port) at once.
In order to support multiple instances of the application on a single machine you need to overcome this limitation. There are a number of possible ways to do this including
- Run multiple virtual machines to allow one instance per machine to bind to the endpoint just as multiple real client machines could. This is a good strategy, however it can become quite expensive with respect to the resources required to run multiple guest OSes plus a host.
- Add support to the application for running multiple instances on a single machine. This generally involves using an interprocess communication mechanism to deliver data to the instance that is bound to the endpoint.
- Have each instance use a different endpoint and use UDP broadcast or multicast, or some other means of IPC to inform each instance where the others are bound so that they can all "see" each other.
In the diagram above Instance A was started first and holds the socket that is listening on the EndPoint. When instance B is started it will check for another instance[1*] and upon finding it is not the only instance will establish an IPC connection to instance A [2*]
When sending a message, instance B will write the message to the IPC channel between itself and instance A. Instance B will include in the message an unique identifier, instance A uses this identifier to tell which instance a message originated from, and to route messages that contain the identifier for Instance B back to the instance.
[1*] One way to check for a second instance is to use a named Mutex. On startup the application should try to acquire the Mutex, if it succeeds then it is the only instance and should bind to the listening endpoint, if acquiring the Mutex fails, then another instance of the application is running, and the second instance should try to acquire an IPC channel with the instance bound to the listening endpoint.
[2*] How one acquires an IPC channel is specific to the communication mechinism being used, if for example you were using Named Pipes, then the first instance would create a pipe using a name known to all instances of your app, and subsequent instances would open pipes that connected to that pipe.
This is by no means a complete answer, but I hope it has helped
Try virtualization. That is, using VMWare or VirtualBox (For example) you can run various "Machines" on your single system. Just create three or four virtual machines and voila ... you have the chance to run various instances as if it were the real thing.
Make sure you install them using NAT networking, giving each of your machines their own "internal" ip address which you can then talk to with your program.
精彩评论