Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi!
I am developing a peer to peer application using sockets over TCP/IP.
First I connect to a IP 72.55.168.241 at port 2000 using:
C#
tcpPeer.Connect(IPAddress.Parse("72.55.168.241"), 2000);

After the connection I get LocalEndPoint 59.99.51.255 at port 4215 and RemoteEndPoint 72.55.168.241 at port 2000.

Now i connect to the other IP 115.117.148.142 at port 3000.

C#
tcpPeer.Connect(IPAddress.Parse("115.117.148.142"), 3000);

After the connection is established I want that the end point address will be like this:
LocalEndPoint 59.99.51.255 at port 4215 and RemoteEndPoint 115.117.148.142 at port 3000.

What I want to say is that every time when I connect to each of the remote machines my LocalEndPoint port number will be the same, e.g using a specific port such as port 4215.

[Edit: Moved OP's explanation from a comment]
Application Scenario

Please read the following lines carefully especially point No. 3. I want to implement this and that's why I need to use the same port.
Suppose that client A wishes to set up a TCP connection with client B and both are behind the NAT. We assume as usual that both A and B already have active TCP connections with a well-known rendezvous server S. The server records each registered client's public and private endpoints.

  1. Client A uses its active TCP session with S to ask S for help connecting to B.
  2. S replies to A with B's public and private TCP endpoints, and at the same time sends A's public and private endpoints to B.

  3. From the same local TCP ports that A and B used to register with S, A and B each asynchronously make outgoing connection attempts to the other's public and private endpoints as reported by S, while simultaneously listening for incoming connections on their respective local TCP ports.

  4. A and B wait for outgoing connection attempts to succeed, and/or for incoming connections to appear. If one of the outgoing connection attempts fails due to a network error such as “connection reset” or “host unreachable,” the host simply re-tries that connection attempt after a short delay (e.g., one second), up to an application-defind maximum timeout period.

  5. When a TCP connection is made, the hosts authenticate each other to verify that they connected to the intended host. If authentication fails, the clients close that connection and continue waiting for others to succeed. The clients use the first successfully authenticated TCP stream resulting from this process.



Please help.
Thanks in advance!
Posted
Updated 11-Feb-11 2:58am
v6
Comments
Manfred Rudolf Bihy 10-Feb-11 9:38am    
Added code tags, minor edits.

Manfred provided an excellent answer, but there are a few more things to consider.

IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList[0];

May result in an ipv6 host address

IPEndPoint(ipAddress, 0);

will as Manfred points out give you a "dynamically" allocated port. You need to send this information to the other application in a manner simmilar to the FTP PORT[^] command for an ipv4 end point and like the FTP EPRT[^] command for ipv6. You are obviously free to implment your own command format; PORT and EPRT are just examples showing how it has been done before.

It goes like this:

1. Server listens on configured port
2. client connects
3. client allocates a port "dynamically" - IPEndPoint(ipAddress, 0);
4. client listens on "dynamically" allocated port on a separate thread, or async
5. client sends endpoint info to server (like FTP PORT or FTP EPRT)
6. server connects back to client using the provided endpoint info

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 10-Feb-11 16:48pm    
I'm not sure if OP wanted the server to connect back to the client, but still very useful information there Espen! 5+
Espen Harlinn 10-Feb-11 17:01pm    
Thanks Manfred - I understood the question as requireing this kind of functionality :)
Manfred Rudolf Bihy 11-Feb-11 8:54am    
And it looks like you were right! See the explanation OP put in a comment to my answer.
Sergey Alexandrovich Kryukov 10-Feb-11 16:48pm    
Well-defined scenario, important case of ipv6, my 5.
--SA
Espen Harlinn 10-Feb-11 17:01pm    
Thanks SAKryukov!
You can do that by using the TCPClient class' constructor that lets you specify a local endpoint like this:

C#
//Example was taken from MSDN TCPClient Constructor with IEndPoint parameter
//Creates a TCPClient using a local end point.
IPAddress ipAddress = Dns.GetHostEntry (Dns.GetHostName ()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 0);
TcpClient tcpClientA = new TcpClient (ipLocalEndPoint);
//Later you can connect to the remote machine using
IEndPoint remoteEndpoint new IPEndPoint(yourRemoteIPAdress, yourRemotePort);
tcpClientA.Connect(remoteEndPoint);


I would advise you though to let the system specify the local end point by choosing an available port. It would be very nice of you if you told us why you stated this requirement or what circumstances made you choose this way.

Best Regards,
Manfred
 
Share this answer
 
Comments
Nish Nishant 10-Feb-11 9:48am    
Voted 5, good response.
Sergey Alexandrovich Kryukov 10-Feb-11 11:52am    
Agree, 5, Manfred.
One note to OP: not connect with TcpClient. It's TcpClient connects to the TCP-based server.
--SA
fjdiewornncalwe 10-Feb-11 15:06pm    
+5. Excellent.
Espen Harlinn 10-Feb-11 16:23pm    
Nearly there, my 5
sufi2008123 11-Feb-11 0:56am    
thank u sir.
You want to know why i need this. Please read the following line carefully specially point No. 3.I want to implement this, that why i need to use the same port.

Suppose that client A wishes to set up a TCP connection with client B and both are behind the NAT. We assume as usual that both A and B already have active TCP connections with a well-known rendezvous server S. The server records each registered client's public and private endpoints.

1. Client A uses its active TCP session with S to ask S for help connecting to B.
2.S replies to A with B's public and private TCP endpoints, and at the same time sends A's public and private endpoints to B.

3.From the same local TCP ports that A and B used to register with S, A and B each asynchronously make outgoing connection attempts to the other's public and private endpoints as reported by S, while simultaneously listening for incoming connections on their respective local TCP ports.

4.A and B wait for outgoing connection attempts to succeed, and/or for incoming connections to appear. If one of the outgoing connection attempts fails due to a network error such as “connection reset” or “host unreachable,” the host simply re-tries that connection attempt after a short delay (e.g., one second), up to an application-defind maximum timeout period.

5.When a TCP connection is made, the hosts authenticate each other to verify that they connected to the intended host. If authentication fails, the clients close that connection and continue waiting for others to succeed. The clients use the first successfully authenticated TCP stream resulting from this process.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900