2

i am developing a small TCP Client/Server lib.

i am facing this problem when i create a client and connect it to server. it gives me this exception

Only one usage of each socket address (protocol/network address/port) is normally permitted

my code is.

 public TCPClient(string remoteIPAddress, int port)
        {
            this.remoteIPAddress = IPAddress.Parse(remoteIPAddress);
            this.port = port;

            IPEndPoint remoteEndPoint = new IPEndPoint(this.remoteIPAddress, this.port);
            tcpClient = new TcpClient(remoteEndPoint);
        }

and here is TCPServer

 public TCPServer(int listeningPort)
        {
            this.listeningPort = listeningPort;            

            tcpListenter = new TcpListener(this.listeningPort);
            workers = new List<TCPServerWorker>();
            this.keepRunning = false;
        }

any help that why i am getting this exception

Mohsan
  • 2,483
  • 6
  • 47
  • 62

1 Answers1

7

solved.

i used

 tcpClient = new TcpClient();
            tcpClient.Connect(remoteIPAddress, port);
Mohsan
  • 2,483
  • 6
  • 47
  • 62
  • If you put the remote ip and port in the TcpClient constructor, you are specifying the source ip/port. I'm guessing Mohsan was using the constructor before. – Kind Contributor May 28 '16 at 06:10