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:
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#....
NetworkStream
contains aSocket
, 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