Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Establish a P2P Connection in C#

Tags:

c#

networking

p2p

I realize this question is similar to some others, but I figured my situation is sufficiently different to warrant its own question (hopefully).

What I'm planning on is deploying a program on another person's computer which will open a connection to my computer after which, I'm assuming, the computers should be able to communicate with each other. Once the program starts, it should be able to pull the address information and port (if they aren't blocked) to create a connection, right?

What's more is the internet configuration of the dorm I'm living in. Every room is assigned a unique port and a static IP address assigned by a DHCP server. How do I factor this into the design of my program?

I'm thinking that this setup does not require a server as an intermediate access point, as my address details will always remain the same and the host computer can simply connect to my computer without further information. Is this correct?

Finally, I am reading a few pages about creating a connection, but am confused with all the possibilities of TcpConnection, WCF, CORBA, etc. Which one would actually be the simplest one for me to start with assuming I only want to send messages to the other machine?

Update:

The address is static in the sense that it doesn't change. My IP address is of the form 130.83.20.xxx and I can either wait for the DHCP server to assign me this address, or I can manually enter it myself using a static IP configuration.

As for the messages itself, simple text messages will suffice for the start. The ports mentioned before are the switch ports and do not come into play during network programming I believe.

like image 810
IAE Avatar asked Aug 18 '11 15:08

IAE


People also ask

What is P2P connection?

In peer-to-peer (P2P) networking, a group of computers are linked together with equal permissions and responsibilities for processing data. Unlike traditional client-server networking, no devices in a P2P network are designated solely to serve or to receive data.

How do P2P networks communicate?

In a peer-to-peer network, computers on the network are equal, with each workstation providing access to resources and data. This is a simple type of network where computers are able to communicate with one another and share what is on or attached to their computer with other users.

Does P2P have a central server?

A peer-to-peer (P2P) architecture consists of a decentralized network of peers, which are nodes that can act as both clients and servers. Without a central server's need, P2P networks distribute workload among peers, and all peers contribute and consume resources within the network.


1 Answers

I would go with TcpClient and TcpListener. Check out the example code on MSDN, copy and paste it into two C# console projects and build them.

I would use 127.0.0.1 (localhost) for testing purposes on port 5001 (a commonly used test port).

TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 5001); 

Then you should be able to test a simple client/server that runs on your computer only. Once you get that working, you can take the client to another computer in your dorm and make sure it still works. Once that works, you can go to a local coffee shop and take the client with you. Leave the server running at a known IP address in your dorm. In this case, have the server bind to your actual external IP (not localhost). You can do this by just specifying a port to the TcpListener constructor: `

TcpListener server = new TcpListener(5001);

Once you get all that working by yourself or with a friend, then send it external. Better to get these things working in a demo before sending it to your customer and having him troubleshoot with you. :)

Reasoning behind my answer:

Simple TCP client/server is very straightforward and will allow a simple chat program between two computers. Then you can beef it up to send any data stream you want. You can add code like this to get access to a StreamWriter:

NetworkStream stream = client.GetStream( );
StreamWriter writer = new StreamWriter(stream);
like image 106
mattypiper Avatar answered Oct 13 '22 00:10

mattypiper