开发者

What would be a good approach to create this application?

I'd like to create a simple application that lists all computers on a given computers network开发者_JAVA技巧. Then I'd like them to be able to send messages to and from each other. Pretty simple. :)

What would the simplest approach be?


That's actually not automatically doable. On a Windows network you can try the equivalent of "net view", which gives you a list of clients. In general, though, there's no guarantee that the computers are discoverable. There are approaches that sort-of work like doing an nslookup on all addresses in your local network but that's brute force and if a computer doesn't register its name in DNS that won't work either.

Assuming you've seolved the problem of finding out which IP address you want to talk to all you need is a socket (UDP for packets or TCP for streams) and then you can start chatting (or sending messages from app to app. Any introduction to TCP/IP will have examples to do that.

In C# it's very easy to do using the Socket or UDPClient classes


To List all computers of a available network:

public void Main()
{
    DirectoryEntry root = new DirectoryEntry("WinNT:");
    DirectoryServices.DirectoryEntries parent = default(DirectoryServices.DirectoryEntries);
    parent = root.Children;
    DirectoryEntries d = parent;
    foreach (DirectoryEntry complist in parent) {
        foreach (DirectoryEntry c in complist.Children) {
            if ((c.Name != "Schema")) {
                Console.WriteLine(c.Name);
            }
        }
    }
}

There is a sample application at codeproject enables to send windows popup messages over the network.

The NetSend Plus


In part this depends on platform and available services. I'm going to assume windows due to the C# tag (although, I know it could be on linux or even the iphone due to mono).

If the right ports are open and the services are running you can use NET SEND to broadcast a message to all machines in a given domain. (example).

If net send doesn't work for you then you'll need, at minimum, a client app capable of announcing its presence to any willing to listen. This is how Windows for WorkGroups worked in days long past. You'll want to use some type of UDP broadcasting.

For a more robust solution you'll also need a server which clients log into. On start the client app will contact the server and announce that it's available. The server will record the address the client came from. For a local chat service this is good enough. When a message comes in for that client the server will simply forward it along.

Unfortunately due to a lot of networking restrictions this doesn't always work well. If latency is okay, then you can just have each client check in with a server every so often to see if there are new messages(kind of like email).

If high latency isn't okay, the client will have to maintain an open socket to the server so that the server can immediately send a message down the wire. (kind of how exchange works).

Regardless, the server option is best if either the clients are distributed across multiple networks OR there are security policies in place preventing discovery of remote applications. Incidentally, most discovery mechanisms today involve scanning the local subnet for a particular open port. A client listens on that port and responds to the scan. I wouldn't recommend this approach.


There may be an easier way, but I would turn something like this into a client/server application. The problem though is you would need to install a client on each machine on the network.

But the implementation would be to have each client register with the server, then the server side application would just be the mediator that would handle the message delivery between each application.

Like I said, there may be an easier way, but I think you'll run into a lot of security issues.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜