1

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!

Dandy
  • 303
  • 5
  • 14

1 Answers1

2

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.

Community
  • 1
  • 1
Tom
  • 7,994
  • 8
  • 45
  • 62
  • Thanks! So if I use the sockets approach, can we say that both users are server and client at the same time? or the first to create the socket connection is the server and the other is the client until the connection closes? – Dandy Jul 04 '11 at 16:23
  • An additional question: To register login/logout states and get the online buddies list, etc, I do will have to use a server, and the only P2P part is the chat window itself, correct? – Dandy Jul 04 '11 at 16:26
  • Both sides create a listening socket to listen for incoming connections (server). In order to establish a connection to the other side, either side can connect to the others' listening socket. – Tom Jul 04 '11 at 16:39
  • As for knowing who's online... well in your situation, one side can just inform the other periodically that its still there. Its up to you how you do it. But be aware this approach will not scale. – Tom Jul 04 '11 at 16:49
  • Thanks a lot. This really helped me with the approach I need to use. – Dandy Jul 05 '11 at 04:35
  • Any comments on the Updated Questions? – Dandy Jul 07 '11 at 15:28