Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have some problems with TCP client on net Compact-Framework 3.5. My server application is written in C++ plus QT library and it's working on desktop computer. One of my clients applications is written in java on android platform and works fine. Also another desktop client written in C++ + QT works fine. I think that server application should be ok an there must be some problem with NET client.

Now I'm trying to write client on Windows Mobile (5.0 - 6.5) platform, using NET CF 3.5.

Problem is that TCP client disconnects with out any reason after one sequence:

client -> server - sends request
server -> client - sends replay
I' have checked communication using Wireshark and below are results:

client -> server [Push]
server -> client [Ack], [Push]
client -> server [Ack], [Fin]
so disconnection is initialized from client side.

I spouse that probably my socket is initialized in wrong way. I'm new in C# and NET stuff so maybe someone more experienced can help me.

Below is a parat of code which opens connection:

C#
public void Open (ConnectionInfo info) // throws SocketEx ;
    {
        if (m_socket != null)
        {
            m_socket.Close();
        }
        try
        {
            m_info = info;
            IPAddress ip = IPAddress.Parse((string)m_info.host);

            TcpClient tcp = new TcpClient();
            tcp.Connect(ip, (int)m_info.port);

            m_socket = tcp.Client;

            if(!m_socket.Connected)
                throw new Ex.SocketEx();

            readLength = 0;
        }
        catch (Exception e)
        {
            throw new Ex.SocketEx();
        }
    }

Writes data into socket:

C#
public void Write ( Buffor data) // throws SocketEx ;
    {
        if (m_socket == null)
            throw new Ex.SocketEx();
        int startTickCount = Environment.TickCount;
        int sent = 0;  // how many bytes is already sent
        int offset = 0;
        int timeout = 1000;//ms
        int size = data.Size();
        byte[] buffer = data.ToArray();
        do
        {
            if (Environment.TickCount > startTickCount + timeout)
                throw new Ex.SocketEx();
            try
            {
                sent += m_socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
            }
            catch (SocketException e)
            {
                // socket buffer is probably full, wait and try again
                Debug.WriteLine(e.ToString());
                Thread.Sleep(50);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
                throw new Ex.SocketEx();
            }
        } while (sent < size);
    }

and reads data from socket

C#
public Buffor Read() // throws SocketEx ;
    {
        if (m_socket == null)
            throw new Ex.SocketEx();
        Buffor buffor = new Buffor();
        try
        {
            if (readLength == 0)
            {
                //read length
                if (m_socket.Available >= 2)
                {
                    byte[] pick = new byte[2];
                    m_socket.Receive(pick, 2, SocketFlags.None);
                    readLength = (((int)pick[0]) << 8) + ((int)pick[1]) - 2;
                }
            }
            if ((readLength != 0) & (m_socket.Available >= readLength))
            {
                //read 
                byte[] buff = new byte[readLength];
                m_socket.Receive(buff, readLength, SocketFlags.None);
                buffor = new Buffor(buff);
                readLength = 0;
            }
        }
        catch (Exception e)
        {
            throw new Ex.SocketEx();
        }
        return buffor;
    }

"Connection" is running in a separate thread than User Interface. Threads is checking periodically if new message is ready to read, if so reads the message, handles it and gets back to waiting for new one. From the other side if UI wants to send something, message is putted at the end of the send buffer. Connection thread in each iteration is checking if there is something in send buffer, if so sends it.

In short whay it looks lake this:

C#
void run()
{
    while (!m_quit)
    {
        if(m_socket.Avaliable >= 2)
            doRead();
        if(m_send_buff.Length != 0)
            doSend();

        //do somthing else

        Thread.Sleep(50);
    }
}
Posted

1 solution

you need to do a full IISRESET. I believe the "modern" recommended method when needing to have a web-hosted app fully restart is recycling the app pool as thus: iisapp /a <app_pool_id> /r (Windows 2003) or appcmd recycle apppool /apppool.name:<app_pool_name> (Windows 2008).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900