0
votes

I am developing TCP Client/Server application using C# socket programming. Sometimes, I encounter a very strange problem as the server (windows service) is running on port (8089) but it is not listening to any client request, and when I test the port with a port scanner it told me that the port is not responding! here is my server code :

First,

private void MainThread() { byte[] bytes = new Byte[1024];

        IPEndPoint localEndPoint = new IPEndPoint(0, this.port);

        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp );

        try {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (active) {
                mainDone.Reset();

                listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);

                while (active)
                    if (mainDone.WaitOne(100, true))
                        break;
            }
            listener.Close();

        } catch (Exception e) {
            if (OnError != null)
                OnError(this, e.ToString());
            LogManager.LogError(e, "TCPSimpleServer MainThread"); 
        }

Then,

 private void AcceptCallback(IAsyncResult ar) {
        Socket handler = null;
        try
        {
            mainDone.Set();

            Socket listener = (Socket)ar.AsyncState;
            handler = listener.EndAccept(ar);

            if (OnConnect != null)
                OnConnect(this, handler);

            StateObject state = new StateObject();

            state.workSocket = handler;
            state.endPoint = (IPEndPoint)handler.RemoteEndPoint;
            stateObjectDictionary.Add(state, state.workSocket);
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
        }
        catch (ObjectDisposedException)
        {
            // Connection closed by client
            if (OnDisconnect != null)
                OnDisconnect(this, (IPEndPoint)handler.RemoteEndPoint);
            return;
        }
        catch (Exception ex)
        {
            LogManager.LogError(ex, "TCPSimpleServer AcceptCallback");
            return;
        }

and Finally,

private void ReadCallback(IAsyncResult ar) {
        try
        {
            String content = String.Empty;
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;

            int bytesRead = 0;
            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (Exception ex)
            {
                // Connection closed by client
                if (OnDisconnect != null)
                    OnDisconnect(this, state.endPoint);
                handler.Close();
                return;
            }

            if (bytesRead > 0)
            {
                string data = Encoding.Default.GetString(state.buffer, 0, bytesRead);

                if (OnDataAvailable != null)
                    OnDataAvailable(this, handler, data);
                try
                {
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReadCallback), state);
                }
                catch (Exception e)
                {
                    if (OnError != null)
                        OnError(this, e.Message);
                    LogManager.LogError(e, "TCPSimpleServer ReadCallback");
                    handler.Close();
                }
            }
            else
            {
                // Connection closed by peer
                if (OnDisconnect != null)
                    OnDisconnect(this, state.endPoint);
            }
        }
        catch (Exception ex)
        {
            LogManager.LogError(ex, "TCPSimpleServer ReadCallback");
        }
    }

I think the problem is in the last method ReadCallback() if problen occured in EndReceive() method the socket (handler) never release the port. any help please?

1

1 Answers

0
votes

Could it be, that a client can block the server at:

while (active) 
    if (mainDone.WaitOne(100, true)) 
        break;