Problem with Sockets

Hi buddies, How are you !!

I got the following problem while i am developing a chat software

My software consists of two parts:

  • Server
  • Client

each in a seperate project, and each works very well when they are working alone, but when i try to test my application -I run the server then the client- i got the problem that when the client create the TcpClient object i got this message:

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

I think that is because i use the same port number for both applications but this is the only way to run both applications on the same machine.

the short version of my question is:

How can I start a TcpListener and a TcpClient on the same machine and the same port

I tried all the windows registry things but no change accured,

Thanks in advance.





Answer this question

Problem with Sockets

  • Raulsassaa

    Hi Timvw,

    yes my server binds the ip/port, and my client use a TcpClient to connect to the server, i think this is the normal way to do it, is there any way to connect to the server without using the TcpClient bound to the port



  • Hassank

    Could you show us the code please

    server:

    IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
    int port = 54321;

    TcpListener tcpListener = new TcpListener(ipAddress, port);

    // start, acceptsockets, close, ...


    client:

    TcpClient tcpClient = new TcpClient();
    tcpClient.Connect("127.0.0.1", 54321);

    // read, write, close, ...




  • DavidThi808

    If you're sure your firewall(s) and network configuration are accepting incoming connections on that ip:port than you should verifiy if the server is really running ;)

    (netstat -an might be helpful)


  • Kunal Sharma

    Hi Peter

    I just tested this:

    Server:

    TcpListener tcpl = new TcpListener(IPAddress.Any, 101);

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    tcpl.Start();
    tcpl.AcceptTcpClient();
    }

    Client:

    TcpClient tc = new TcpClient("localhost", 101);
    bool x = tc.Connected;

    x became true (no error or something else)

    Could it be that you have 2 server instances running or that some other application is already running on that specific port (skype always listens at port 80 for example)


  • gifuran

    Hi Tim, Hi Thomas

    My server code is something like this:

     

    this.listener = new TcpListener(IPAddress.Loopback, 12554);

    this.listener.Start();

    while (true)

    {

    try

    {

    this.socket = this.listener.AcceptSocket();

    this.stream = new NetworkStream(this.socket);

    this.reader = new BinaryReader(this.stream);

    this.writer = new BinaryWriter(this.stream);

    if (this.Authenticate())

    this.Redirect();

    }

    catch { }

    }

     

     

    and it run in a seperate thread

    my client code is:

    System.Net.IPEndPoint ee = new System.Net.IPEndPoint(System.Net.IPAddress.Loopback, 12554);

    TcpClient cl = new TcpClient(endPoint);

    try

    {

    this.stream = cl.GetStream();

    this.reader = new BinaryReader(this.stream);

    this.writer = new BinaryWriter(this.stream);

    }

    catch

    { }

     

     

    I am sure the problem is not because another application is using this ip/port, because the exception doesn't happen when i don't start the server

    When i compiled the code with .NetFX 1.1(VS 2003) the problem didn't accur

     



  • jeff357

    When i used the Connect method, the old exception didn't happen but a new one happens that says that the target machine refuses the connection, i checked my network configuration and my firewall setting and still got the same message

    if you have evered experienced that please help me, thank you :)



  • j_o_h_a_n_n_e_s

    If you use the TcpClient constructor that accepts an IPEndPoint you will bind to that address...


    The following code should do what you want:

    TcpClient cl = new TcpClient();
    cl.Connect(endPoint);


  • vije

    To me it seems that both your server and client are trying to bind to the same ip:port combination... Only the server should bind to a socket.. And the client should then connect/open (but not bind to) that socket...


  • Problem with Sockets