Search code examples
c#p2pinstantmessenger

C# P2P Instant Messenger General Basis Help


I want to start a simple windows P2P instant messenger in C#, similar to AOL, ICQ, etc, but much more simple (plain text messages between 2 guys)

I don't need examples on how to do it. I can find them myself.

What I do need is a general idea of how instant messaging works (P2P, not multichat) without many technical details.

For example:

  • Will I need a main server to make the communication between user1 and user2 happen or user1 can send the strings directly to user2? How is this called?

  • If user1 is logged in, how does he know of an incoming message from another user (or the online status of their friends)? Does the chat client app check every X seconds with a main server?

Any clues that might help me clear the general data flow idea will be very much appreciated. A flowchart may also be helpful if you find one to share.

Thanks in advance.

UPDATE (NEW QUESTION) - July 6

Let's say the user had successfully logged in, and the app needs now to get and populate the list of contacts (saved on my apache/php/mysql server).

  • How would you implement the data retrieval (important) and later population of the contacts list? Is WebClient.DownloadString[Async] a good approach? Is there a better way?

  • How often should the app check for updated list (online/offline statuses). Recommendations accepted.

  • How can I parse JSON data on C#.NET (Visual C# Studio 2010) I will get JSON strings.

Thanks!


Solution

  • If you really want to build a p2p app, there should be no server. However, this is not straightforward.

    There are lots of different approaches to creating a chat system, mostly involving servers. Research comet (a good solution if implemented properly, terrible otherwise), polling (checking every x seconds) or using sockets, however there are lots of issues to be considered - and caveats, particularly firewalls/nat routers. A socket solution could potentially be 'p2p', but the polling and comet ones are not.

    For your use case, I would go with a simple socket solution (one side as server, one as client) and configure your router firewall by opening a port at the server end.

    You could extend this so that both sides could be both servers (listening on a port) and clients, so you could both 'call' each other.

    You will need to have a permanent ip, or use a service like dyndns to get this to work properly.

    Update

    Yes, DownloadString or DownloadStringAsync would be a fine method. How often is really up to you. I assume that this is only for a few users from what you said in the question, so you don't need to worry about overloading the server. Once a minute sounds reasonable, but once a second would proabably be fine too if you feel that way inclined... Parsing JSON in .NET answers your final query.