Results 1 to 3 of 3

Thread: TcpClient/Listener Questions

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2010
    Posts
    20

    TcpClient/Listener Questions

    I'm having a hard time getting the fundamentals down about these TCP Socket classes.

    When you use TcpClient, you can set the IP Address or domain name to connect to the server end of the socket. I see examples that use "localhost" as the first parameter for TcpClient. Is this just a broadcast over the network to find the server that has a TcpListener listening on the dictated port of the second parameter of the TcpClient declaration? With any IP address for TcpClient (I realize that's an option, though), how does "localhost" find the TcpListener server?

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: TcpClient/Listener Questions

    Listener listens as it is obvious from its name. It only listens for connection attempts on the specified port. So in order to connect to it some client should specify its hostname (or IP address) and the correct port.

    TCPclient is a client to TCPlistener to put it simple. In order to connet it has to know listener's host name and the port it listens to.

    So the answer to your question is 'localhost' does not look up for your tcplistener. You should tell your app which port to listen to (on the server part) and which hostname on which port to connect to (on the client part).

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: TcpClient/Listener Questions

    localhost is just a name you can use to refer to the local PC that your program is running on - ie a computer that tries to send something to localhost is trying to send something to itself. So no matter what PC your program is running on, localhost will always refer to the computer the code is running on. It is known as a "loopback" address and it is the same as the IP address 127.0.0.1. If you use the command line PING utility for example (which just sends data packets to a specified computer and tells you if the computer responds), and you specify localhost as the target to ping, you are actually pinging the same computer you are sending the ping from. It is not a broadcast across the network, it is in fact the opposite.

    So basically dont ever use localhost when working with TcpClient/Listener unless you are actually wanting to refer to the same computer that the code is running on (when I say "the code" I do not just mean the server side code that is listening for connections, I mean whatever code it is that you have specified localhost in).

    In those examples you have seen that specify localhot as the address to connect to, it just means that the server side software (ie the code that is using the TcpListener) is running on the same PC.

    Hope that helps.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width