0

Ok this is kind of frustrating, from past few hours i was struggling with .Net Socket based application, in short this application acts a async server, and this server can be instantiated in 2 ways :-

  • First way is to start the server is by getting local DNS name of the computer and then bind it to the socket object.(this works totally fine)
  • Second way is to start the server is by getting host name from the user input such as "192.168.1.2" and then i was trying to bind it to the socket object but it keeps shooting the exception. Precisely speaking this exception only raised when i try to use IPv4 Private network based IP's.
    I seriously have no idea why is that happening

Exception : "The requested address in not valid in its context".

Code below is used to start the server



    public void StartServer(int port, IPAddress ipAddress)
            {
                byte[] bytes = new Byte[1024];
                IPAddress[] ips = Dns.GetHostAddresses(ipAddress.ToString());
                Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                this._socket = listener;
                try
                {

                    IPEndPoint bindEndPoint;
                    bindEndPoint = new IPEndPoint(ipAddress, port);
                    listener.Bind(bindEndPoint);
                    listener.Listen(100);

                    WriteStatus("\nStarted -" + bindEndPoint.ToString() + " on " + Convert.ToString(port));
                    IsUp = true;

                    while (IsUp)
                    {
                        allDone.Reset();
                        WriteStatus("\nWaiting for a connection......");
                        try
                        {
                            listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
                        }
                        catch (ObjectDisposedException)
                        {
                            return;
                        }

                        allDone.WaitOne();
                    }
                }
                catch (SocketException se)
                {
                    MessageBox.Show(se.Message, "Error : Start Server",  MessageBoxButton.OK, MessageBoxImage.Error);

                }

            }


    

This way it loads the Pre-configured DNS name



    private void LoadStaticIp()
            {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            _ipAddress = ipHostInfo.AddressList[0].ToString();
            }

Please help.

Saubar
  • 135
  • 11
  • What is the class this is nested in? It'll help me generally understand your code. – A. Abramov Feb 20 '15 at 19:41
  • P.S Did you use breakpoints? and which line did it collapse? – A. Abramov Feb 20 '15 at 19:41
  • Yes i do use break points & it collapses on _`listener.Bind(bindEndPoint)`_, and do you want the whole overview of the class? – Saubar Feb 20 '15 at 19:57
  • just name, general idea. Just to understand the concept. It's not gonna be easy, but i'm going to try my best. :) – A. Abramov Feb 20 '15 at 20:05
  • this is based on microsoft's Asynchronous Server Socket Example [link](https://msdn.microsoft.com/en-us/library/fx6588te%28v=vs.110%29.aspx) – Saubar Feb 20 '15 at 20:12
  • Put a breakpoint in that specific line, and tell me what does the bindEndPoint address is. – A. Abramov Feb 20 '15 at 20:26
  • `bindEndPoint = new IPEndPoint(ipAddress, port);` very much same as the parameter _ipAddress_. – Saubar Feb 20 '15 at 20:29
  • I understand that, But as the exception says, It's probably misplaced - something is not assigned right. List down the parameters you think might be relevant. – A. Abramov Feb 20 '15 at 20:42
  • Why are you binding the listening socket to a specific IP address in the first place? Why not just let Winsock default the IP address? If you really need to bind the listening socket, are you _sure_ that you are binding to a valid address for the host machine? I.e. is that machine actually assigned the address **"192.168.1.2"**? – Peter Duniho Feb 21 '15 at 02:14
  • Specific IP is required only because there are certain devices which are pre-configured to communicate on the defined IP address which may be **192.168.1.2** and yea the address is valid for the host machine. – Saubar Feb 21 '15 at 07:12

0 Answers0