0

So I need to use the same port for two sockets, therefore I set ExclusiveAddressUse to false and ReuseAddress to true. However, as I run this code I am told that "Bind has already been called for this socket". My question is then: it seems like the TcpClient constructor does the binding, so how can I possibly set ExclusiveAddressUse?

serverSock = new TcpClient();
serverSock.Client.ExclusiveAddressUse = false;
serverSock.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
serverSock.Connect(serverEP);
Jontahan
  • 113
  • 8
  • You can't use the same local ip/port and remote ip/port combination for two sockets. See answer here : https://stackoverflow.com/questions/11129212/tcp-can-two-different-sockets-share-a-port?rq=1 – o_weisman Nov 16 '17 at 14:11
  • @o_weisman Then how can I implement P2P hole punching in C#? Check out section 4 here: http://www.bford.info/pub/net/p2pnat/index.html – Jontahan Nov 17 '17 at 09:24
  • There is an example here: https://vocalsteve.wordpress.com/2011/03/16/port-reuse-in-c-tcplistener-tcpclient/ . Notice however, that the remote port connected to by the TCP clients is different so the rule that the local / remote combination must be unique for each socket is not broken, which from what I've read about hole punching, is ok as you are connecting to the Nat server and to the peer client which are different as a client and only opening a local listening server socket for the incoming connection from the other peer. – o_weisman Nov 19 '17 at 06:55

0 Answers0