I been trying to learn the TCP protocol. so I am able to transfer data between two computers on the same network as:

\\the server:

var server = new TcpListener(ipAddress, port); //create a server
server.Start(); //start the server

//wait here until a client get's connected to this computer
TcpClient someClient = server.AcceptTcpClient();

NetworkStream stream = someClient.GetStream(); \\create a network stream object in order to read and send data to the other connected computer

once the server is running, the client application will be able to connect to the server as:

//Client:
var client = new TcpClient(serverIP, port);
NetworkStream stream = client.GetStream();
//initialize data to by some byte array. the data you wish to send
stream.Write(data, 0, data.Length);



byte[] bytes = new bytes[1000] //instantiate a byte array
connection.stream.Read(bytes, 0, bytes.Length)  // wait here until data is send 

// in this line data has been received and it will be placed the bytes array

I label each node with a different letter. for example note how computer A and computer C happen to be on the same network:

enter image description here

Hope this picture helps me explain what I mean.

Establish connection between computer A and C:

If I want to establish a connection between computer A and C then that will probably be the easiest case. In other words I will not have to do any kind of port forwarding in order to establish the connection.

Establish connection between computer A and server S:

if the client computer happens to be computer A and the server computer happens to be computer S then computer A will be able to find server S. So this connection should be simple too.

Establish connection between computer A and computer B: (here is my problem)

So if computer A wants to find computer B then router Y will have to forward all traffic from port 'somePort P' to computer B and A will have to provide the ip address of router Y or the WAN ip address.

Until this point my title question should not make sense let me explain what I mean when I say "Pass tcp connection to different computer"

I actually need to establish a connection between computer A and computer B without having to configure the routers. I been thinking a lot and I think this should be possible: (it's a little crazy though)

if you remember from my previous code once the connection was established, everything that was send/written through the network stream is received by the other end of the connection. so because computer A is not able to connect to computer B then make A connect to the server S. So far there is a connection between computer A and server S. Ok now establish a tcp connection between computer B and server S. by now the server S should have two distinct tcp connections, one to computer A and the other to computer B.

Because the server has two distinct tcp connections it should have also two NetworkStream objects. let's say that the tcp connection between server S and computer A has the object NetworkStream streamA; and the tcp connection with computer B has the object NetworkStream streamB;

So here comes the crazy part. The server S serializes the instantiated and working object streamB and sends that object to computer A. then computer A deserializes that object and cast it to a NetworkStream object. Computer A should now have a copy of object streamB. WHAT WILL HAPPEN IF THEN YOU SEND DATA THROUGH OUT THAT STREAM!? theorically computer B should receive the data I think. I mean maybe I will have to modify each package header information so that router Y treats those packages as if they where coming from Server S!

the reason why I want to do this is because I have created a server application and client application where it will sync files between two computers on a specific folder. a lot of times users using this program do not know how to enable port forwarding. I also know I may be able to solve this using what is called as "tcp puch holing" but I have not been able to find a working method on c#....

  • 2
    NetworkStream contains a Socket, which is actually the responsible party in your communication scenario. Sockets are not serializable and are bound to the operating system. – Paul Walls Sep 4 '11 at 4:50
up vote 2 down vote accepted

The method you have suggested won't work. A and B are both sending traffic to S. No matter what S sends to A or B, A and B will still have their routers (X and Y) passing traffic only to S. What you want is, as you mentioned, NAT hole punching.

The basic idea of NAT hole punching is to have A send traffic to B and B send traffic to A simultaneously. If done correctly, each router (X and Y) will think the packet from the other side is a reply to the packet it sent, and they will establish NAT entries allowing A and B to talk to each other directly. This is almost impossible to do for TCP, but can be done for UDP.

  • How can I send udp traffic simultaneously? So what you mean is send udp packages from computer A to the wan ip address (ip address found at whatismyipaddress.com) of computer B. And same thing with computer B in the oposite direction? – Tono Nam Sep 4 '11 at 4:53
  • Yes, exactly. You can arrange it by having server S tell the two computers to do it. – David Schwartz Sep 4 '11 at 16:54
  • Schwarts. Thanks a lot! I been trying to do this for so long. Ok now I know how tcp hole punching works. I have actually opened a bounty question similar to this question at: stackoverflow.com/questions/7225150/… Maybe you'll be able to help. I will really appreciate your help – Tono Nam Sep 5 '11 at 19:37

Trying to send streams or connections over connections is akin to trying to send a telephone over a telephone line. It is what philosophers call a category mistake. You can't do it. A stream is an aspect of a socket, and a socket is an endpoint of an existing connection. Period. It can't be sent anywhere.

  • @ EJP sorry I did not knew about tcp puch holing. TCP punch holing is similar to this question. instead of passing the object you pass the parameters needed to establish a new tcp connection. anyways I have a bounty question at: stackoverflow.com/questions/7225150/… that is similar to this question. I will appreciate so much if you can help – Tono Nam Sep 5 '11 at 19:41

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.