Socket Bind problem

Hey Guys,

I have a server class that recieves requests, an sends response to a engine. when no more requests are recieved and there are no more repsonse to send, the server most exit. so the program can accept input from a user again. but when i do a request again the server gives me a Bind error.

Can someone help me with this error, why doesn’t the server Bind the IPEndPoint.

i get this error…

#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message: 
#### Microsoft.SPOT.Net.SocketNative::bind [IP: 0000] ####
#### System.Net.Sockets.Socket::Bind [IP: 0016] ####
#### OPI_POS.Classes.Server::Start [IP: 0028] ####
#### MatrixButtons.Desktop::Send [IP: 0035] ####
#### MatrixButtons.Desktop::Action [IP: 0165] ####
#### MatrixButtons.Desktop::gridPanelDemo_Action [IP: 0005] ####
#### MatrixButtons.GridPanelDemo::DemoGridTouchDown [IP: 0055] ####
#### Microsoft.SPOT.Presentation.UIElement::OnTouchDown [IP: 000f] ####
#### Microsoft.SPOT.Presentation.UIElement::OnTouchDownThunk [IP: 000b] ####
#### Microsoft.SPOT.RoutedEventArgs::InvokeHandler [IP: 0029] ####
#### Microsoft.SPOT.EventRoute::InvokeHandlers [IP: 00a3] ####
#### Microsoft.SPOT.Presentation.UIElement::RaiseEvent [IP: 0052] ####
#### Microsoft.SPOT.Input.InputManager::ProcessStagingArea [IP: 00a5] ####
#### Microsoft.SPOT.Dispatcher::PushFrameImpl [IP: 004a] ####
#### Microsoft.SPOT.Dispatcher::PushFrame [IP: 001d] ####
#### Microsoft.SPOT.Dispatcher::Run [IP: 0006] ####
#### Microsoft.SPOT.Application::Run [IP: 0062] ####
#### MatrixButtons.Program::Main [IP: 012b] ####
#### SocketException ErrorCode = 10048
#### SocketException ErrorCode = 10048

A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10048
#### SocketException ErrorCode = 10048

here is the start funcition that gives the error.

        /// <summary>
        /// Start the server
        /// </summary>
        public void Start()
        {
            Debug.Print("server started");
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Debug.Print("Server Bind");
            server.Bind(new IPEndPoint(IPAddress.Any, this.port));
            Debug.Print("Listening");
            server.Listen(1);
            

            while (true)
            {
                Debug.Print("In while true");
                try
                {
                    Debug.Print("Request: "+ request + ", Response: " + response);
                    if (request != 0 && response == 0)
                    {
                        Debug.Print("last response send");
                        server.Close();
                        break;
                    }

                    Debug.Print("Waiting for client connecting");
                    // Wait for a client to connect. 
                    communicationSocket = server.Accept();
                    Debug.Print("Accepting");
                    response = ++request;
                    Debug.Print("Response in while: " + response);
                    // Process the response to eft
                    Process.Start();
                    Thread.Sleep(3000);
                }
                catch { }
            }
            Debug.Print("Out of while");
            request = 0;
            response = 0;
            Debug.Print("Request: " + request + ", Response: " + response);
        }

here is the function that starts the server

        private void Send(string data)
        {
            try
            {
                client = new Client("192.168.2.32");
                client.Connect();
            }
            catch (System.Net.Sockets.SocketException se)
            {
                throw se;
            }

            try
            {
                client.Send(data);  // send the request
            }
            catch(Exception e)
            {
                throw e;
            }
            try
            {
                new Server().Start();   // Start Server
            }
            catch(System.Net.Sockets.SocketException se)
            {
                throw se;
            }

        }

error 10048 is Address In Use

see: [url]http://code.tinyclr.com/project/346/socket-error-codes/[/url]

What can i do to avoid this error…
why cant i start the server again. am i not closing the sockets properly

Sockets can be vexing, especially when used with TCP. Data you sent may still be in the process of being transmitted when you close the socket, delaying the actual closure (sometimes minutes at a a time). This is especially true if the other side does not close its side of the connection cleanly.

Take a look at the socket options ([url]Microsoft Learn: Build skills that open doors in your career), especially the [italic]NoDelay[/italic] at TCP level and [italic]DontLinger[/italic] at the socket level.

Also worth looking into is [italic]SendTimeout[/italic] [url]Microsoft Learn: Build skills that open doors in your career

Lastly, Fiddler or similar may give you some hints about what is going on.