Binding Problems

  1. Mountaineer Eth Mainboards DO have socket communication abilities correct?

  2. I’m stepping through my code and when i try to listen for socket messages i continue to get exceptions when when i try to bind the LocalEndPoint. Since it gets exceptions I never get to listen to the port. Here is part of my code.

            public void StartListening()
            {
                IPHostEntry ipHostInfo = null;
                IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("192.168.1.50"), port);
                if (ipAddress == string.Empty)
                    localEP = new IPEndPoint(ipHostInfo.AddressList[2], port);
                else
                    localEP = new IPEndPoint(IPAddress.Any, port);

                Socket listener = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                while (!bShutdown)
                {
                    try
                    {
                        listener.Bind(localEP);
                        listener.Listen(10);
                        listener.Accept();

Its always a SocketException even though i thought that i was creating the socket the correct way but apparently i can’t access it. How can i fix this?

I originally had

IPAddress.Parse(ipaddress)

where

IPAddress.Any

is now but that isn’t the problem at all as far as i know.

you are not telling us where you are getting the exception.

you should only bind and listen once. not sure why you have it in a while loop.

you should also be interested in the return fron the accept statement since it is the socket you should use to communicate with the remote host who has just connected.

also use the ipaddress any for the bind.

included with the MS SDK are some good socket examples.

Sorry i left half the code out.

            public void StartListening()
            {
                IPHostEntry ipHostInfo = null;
                IPEndPoint localEP = new IPEndPoint(IPAddress.Parse("192.168.1.50"), port);
                if (ipAddress == string.Empty)
                    localEP = new IPEndPoint(ipHostInfo.AddressList[2], port);
                else
                    localEP = new IPEndPoint(IPAddress.Any, port);

                Socket listener = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream, ProtocolType.Tcp);
                while (!bShutdown)
                {
                    try
                    {
                        listener.Bind(localEP);
                        listener.Listen(10);
                        listener.Accept();
                        int iLastLength = 0;
                        //wait for all data to come in before parsing it (200ms character timeout)
                        do
                        {
                            iLastLength = listener.Available;
                            Thread.Sleep(200);
                        } while (iLastLength != listener.Available);
                        if (listener.Available > 0)
                        {
                            string sReceived = "";
                            byte[] buf = new byte[listener.Available];
                            //pull data from socket
                            listener.Receive(buf);

                            //convert buffer to string
                            foreach (byte b in buf)
                                sReceived += (char)b;
                            listener.Close();
                            OnDataEvent(new DataEventArgs(sReceived, null, string.Empty, 0, false, false, true));
                        }
                    }
                    catch (Exception e)
                    {
                        if (!bShutdown)
                            OnDataEvent(new DataEventArgs(null, "Listener Waiting Error: " + e.Message, "", 0, true, false, false));
                    }
                }
                bShutdown = true;
            }

So you’re saying i should bind it before the while loop? Also, I’m working on debugging it and i might need to change where the

bShutdown = true

is.

check this sample code. http://www.tinyclr.com/codeshare/entry/220

The binding issue is fixed now thanks.