Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
 
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:
 
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
 
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:
 
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 16-Dec-11 6:14am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 /r (Windows 2003) or appcmd recycle apppool /apppool.name: (Windows 2008).
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 410
1 Sergey Alexandrovich Kryukov 329
2 Afzaal Ahmad Zeeshan 264
3 BillWoodruff 235
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 16 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100