'Two Identical applications which communicate over a TCP socket connection on the localhost

C#/XAML .NET WPF ... Both applications need to have the ability to connect with each other, and both should have listeners. How would I do this as I know that usually a TCP socket is client/server and what I need is basically two servers that act as clients? Is it possible?



Solution 1:[1]

First off, I'm sorry there's no code for this answer. I'm not a C# developer (yet). I'm working on a very similar situation and it will be my first foray into the language.

For my scenario I need two copies of the same app to talk to one another but one of them will always start the connection first. It is a very basic game in which two players take turns. I can't hardcode IPs because they need to be able to talk even when not on localhost. My solution is:

  1. One user selects "start game."
  2. App one will open a socket and begin listening. This puts it in the role of server.
  3. App one will then display the IP and Port #.
  4. User two selects "join game"
  5. User two enters the socket info provided.
  6. App two will open a socket for sending, thus becoming the client.
  7. Once the connection is fully established those sockets will be handed off to the code which handles the actual game play. This is possible because game play is identical for both parties regardless of who started.

For your scenario you can skip the displaying of the IP and Port. Both sides will begin listening and begin sending. Once a connection is established they can stop doing what isn't needed. i.e. if one successfully opens a client socket it stops listening as it no longer needs to be in the server role.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ginger McMurray