TcpClient Exception: "The requested address is not valid in its context"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Horst Walter

    #1

    TcpClient Exception: "The requested address is not valid in its context"

    What is wrong here?

    IPAddress ipAddress = IPAddress.Parse ("10.10.20.1 ");
    IPEndPoint ipEndPoint = new IPEndPoint(ipAd dress, this.port);
    this.tcpClient = new TcpClient(ipEnd Point); // PROBLEM HERE

    => Exception: "The requested address is not valid in its context"


    This works:
    this.tcpClient = new TcpClient("10.1 0.20.1", this.port);

    However, the latter is somehow slow, because it seems that in this
    case a DNS resolution takes place. This is exactly what I'd like to
    avoid.

    Any ideas?
    Regards HW
  • Ignacio Machin \( .NET/ C#  MVP \)

    #2
    Re: TcpClient Exception: "The requested address is not valid in its context"


    This may be cause the IP is not a valid IP on your computer, or maybe the
    port is binded to another application.

    If you dont really need to especify the IP then create the IPEndPoint with a
    0:
    IPEndPoint ipEndPoint = new IPEndPoint( 0, this.port);

    Hope this help,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation

    "Horst Walter" <unkwb@web.de > wrote in message
    news:53867fbe.0 312051037.77f7d 656@posting.goo gle.com...[color=blue]
    > What is wrong here?
    >
    > IPAddress ipAddress = IPAddress.Parse ("10.10.20.1 ");
    > IPEndPoint ipEndPoint = new IPEndPoint(ipAd dress, this.port);
    > this.tcpClient = new TcpClient(ipEnd Point); // PROBLEM HERE
    >
    > => Exception: "The requested address is not valid in its context"
    >
    >
    > This works:
    > this.tcpClient = new TcpClient("10.1 0.20.1", this.port);
    >
    > However, the latter is somehow slow, because it seems that in this
    > case a DNS resolution takes place. This is exactly what I'd like to
    > avoid.
    >
    > Any ideas?
    > Regards HW[/color]


    Comment

    • Laszlo Szijarto

      #3
      Re: TcpClient Exception: &quot;The requested address is not valid in its context&quot;

      Is "10.10.20.1 " the IP of your own machine?
      Just guessing. Maybe the second overload of TcpClientConstr uctor can handle
      that, but an IP on your own machine doesn't qualify as an "EndPoint".

      "Horst Walter" <unkwb@web.de > wrote in message
      news:53867fbe.0 312051037.77f7d 656@posting.goo gle.com...[color=blue]
      > What is wrong here?
      >
      > IPAddress ipAddress = IPAddress.Parse ("10.10.20.1 ");
      > IPEndPoint ipEndPoint = new IPEndPoint(ipAd dress, this.port);
      > this.tcpClient = new TcpClient(ipEnd Point); // PROBLEM HERE
      >
      > => Exception: "The requested address is not valid in its context"
      >
      >
      > This works:
      > this.tcpClient = new TcpClient("10.1 0.20.1", this.port);
      >
      > However, the latter is somehow slow, because it seems that in this
      > case a DNS resolution takes place. This is exactly what I'd like to
      > avoid.
      >
      > Any ideas?
      > Regards HW[/color]


      Comment

      • Horst Walter

        #4
        Re: TcpClient Exception: &quot;The requested address is not valid in its context&quot;

        Firstly, thanks a lot for your help!

        However, the solution still is not clear:

        "10.10.20.1 " in my case is a valid IP and NOT the IP of the computer
        itself (where the prg. runs).

        Furthermore this works (SAME PORT /IP):
        this.tcpClient = new TcpClient("10.1 0.20.1", this.port);

        So I do not believe it is some kind of general network problem.....

        Regards
        KB


        "Laszlo Szijarto" <lszijarto@nosp am.mrdoc.cc> wrote in message news:<bqqndr$c6 9$1@sulawesi-fi.lerc.nasa.go v>...[color=blue]
        > Is "10.10.20.1 " the IP of your own machine?
        > Just guessing. Maybe the second overload of TcpClientConstr uctor can handle
        > that, but an IP on your own machine doesn't qualify as an "EndPoint".
        >
        > "Horst Walter" <unkwb@web.de > wrote in message
        > news:53867fbe.0 312051037.77f7d 656@posting.goo gle.com...[color=green]
        > > What is wrong here?
        > >
        > > IPAddress ipAddress = IPAddress.Parse ("10.10.20.1 ");
        > > IPEndPoint ipEndPoint = new IPEndPoint(ipAd dress, this.port);
        > > this.tcpClient = new TcpClient(ipEnd Point); // PROBLEM HERE
        > >
        > > => Exception: "The requested address is not valid in its context"
        > >
        > >
        > > This works:
        > > this.tcpClient = new TcpClient("10.1 0.20.1", this.port);
        > >
        > > However, the latter is somehow slow, because it seems that in this
        > > case a DNS resolution takes place. This is exactly what I'd like to
        > > avoid.
        > >
        > > Any ideas?
        > > Regards HW[/color][/color]

        Comment

        • Rich Blum

          #5
          Re: TcpClient Exception: &quot;The requested address is not valid in its context&quot;

          unkwb@web.de (Horst Walter) wrote in message news:<53867fbe. 0312051037.77f7 d656@posting.go ogle.com>...[color=blue]
          > What is wrong here?
          >
          > IPAddress ipAddress = IPAddress.Parse ("10.10.20.1 ");
          > IPEndPoint ipEndPoint = new IPEndPoint(ipAd dress, this.port);
          > this.tcpClient = new TcpClient(ipEnd Point); // PROBLEM HERE
          >
          > => Exception: "The requested address is not valid in its context"
          >
          >
          > This works:
          > this.tcpClient = new TcpClient("10.1 0.20.1", this.port);
          >[/color]
          Horst -

          You are mixing apples with oranges. The TcpClient constructor that
          uses the IPEndPoint defines a local endpoint for the socket to bind
          to, while the constructor that uses the string defines a remote
          endpoint for the socket to connect to. Thus, the first constructor is
          looking to bind the socket to the local IP address and port specified
          in the IPEndPoint object. Since you gave it a remote IP address it
          couldn't perform the task and threw the Exception.

          If you want to connect to a remote endpoint using an IPEndPoint
          object, try using the Socket object. This allows you to use an
          EndPoint, and shouldn't try the DNS lookup of the IP address. This
          would look something like this:

          Socket sock = new Socket(AddressF amily.InterNetw ork,
          SocketType.Stre am,
          ProtocolType.Tc p);
          IPEndPoint ipep = new IPEndPoint(IPAd dress.Parse("10 .10.20.1",
          this.port);
          sock.Connect(ip ep);
          NetworkStream ns = new NetworkStream(s ock);

          Hope this helps solve your problem.

          Rich Blum - Author
          "C# Network Programming" (Sybex)

          "Network Performance Open Source Toolkit" (Wiley)

          Comment

          • Horst Walter

            #6
            Re: TcpClient Exception: &quot;The requested address is not valid in its context&quot;

            Thanks a lot!
            HW



            rich_blum@juno. com (Rich Blum) wrote in message news:<ccdac0da. 0312062020.7098 93b4@posting.go ogle.com>...[color=blue]
            > unkwb@web.de (Horst Walter) wrote in message news:<53867fbe. 0312051037.77f7 d656@posting.go ogle.com>...[color=green]
            > > What is wrong here?
            > >
            > > IPAddress ipAddress = IPAddress.Parse ("10.10.20.1 ");
            > > IPEndPoint ipEndPoint = new IPEndPoint(ipAd dress, this.port);
            > > this.tcpClient = new TcpClient(ipEnd Point); // PROBLEM HERE
            > >
            > > => Exception: "The requested address is not valid in its context"
            > >
            > >
            > > This works:
            > > this.tcpClient = new TcpClient("10.1 0.20.1", this.port);
            > >[/color]
            > Horst -
            >
            > You are mixing apples with oranges. The TcpClient constructor that
            > uses the IPEndPoint defines a local endpoint for the socket to bind
            > to, while the constructor that uses the string defines a remote
            > endpoint for the socket to connect to. Thus, the first constructor is
            > looking to bind the socket to the local IP address and port specified
            > in the IPEndPoint object. Since you gave it a remote IP address it
            > couldn't perform the task and threw the Exception.
            >
            > If you want to connect to a remote endpoint using an IPEndPoint
            > object, try using the Socket object. This allows you to use an
            > EndPoint, and shouldn't try the DNS lookup of the IP address. This
            > would look something like this:
            >
            > Socket sock = new Socket(AddressF amily.InterNetw ork,
            > SocketType.Stre am,
            > ProtocolType.Tc p);
            > IPEndPoint ipep = new IPEndPoint(IPAd dress.Parse("10 .10.20.1",
            > this.port);
            > sock.Connect(ip ep);
            > NetworkStream ns = new NetworkStream(s ock);
            >
            > Hope this helps solve your problem.
            >
            > Rich Blum - Author
            > "C# Network Programming" (Sybex)
            > http://www.sybex.com/sybexbooks.nsf/Booklist/4176
            > "Network Performance Open Source Toolkit" (Wiley)
            > http://www.wiley.com/WileyCDA/WileyT...471433012.html[/color]

            Comment

            Working...