Skip to content

Set the client port in the TcpClent class

Marko Ceric

Asked Nov 3, 2008, 2:01 AM EST3 answers
Share
LinkedInX
Hello.

I'm using TcpClient class to connect to a TCP server.

Is there any way to set the client port to open when connecting to the server by TcpClient class (not the port of the server).

I'm googling it for 2 days and nothing...

I'm doing some simulation for some hardware which uses an predefined port, but my TcpClient uses the first available port. Not good. :S

Thank's

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    +1
    you can set your set port number from the client side using the Tcp.Connect()
    for example
    Code
    TcpClient tcpClient = new TcpClient ();
    tcpClient.Connect ("www.contoso.com", 11002);
    But 11002 is in fact the server por,  In general the client port value is already set to 0, and it has to be in order to let system in the client side decide what is the suitable port to use,  therefore it has no needs to set the tcp client port
    +1
  • +1
    Thank's for that. But I know how to set the port on the server side. I would like to know if this also can be done on the client side.
    +1
  • Bechir Bejaoui
    +1
    In general the client port value is already set to 0, and it has to be in order to let system in the client side decide what is the suitable port to use,  therefore it has no needs to set the tcp client port
    +1