C#,NET TCP连接无故关闭 [英] C#, NET TCP connection is closed without any reason

查看:128
本文介绍了C#,NET TCP连接无故关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Net Compact-Framework 3.5上的TCP客户端有一些问题.我的服务器应用程序是用C ++和QT库编写的,并且可以在台式计算机上使用.我的一个客户应用程序是在Android平台上用Java编写的,并且运行良好.另外,另一个用C ++ + QT编写的桌面客户端也可以正常工作.我认为服务器应用程序应该可以,并且NET客户端必须存在一些问题.

现在,我正在尝试使用NET CF 3.5在Windows Mobile(5.0-6.5)平台上编写客户端.

问题是一个顺序后,TCP客户端无缘无故地断开连接:

客户->服务器-发送请求
服务器->客户端-发送重播
我''已检查使用Wireshark的通信,以下是结果:

客户->服务器[推送]
服务器->客户[Ack],[Push]
客户->服务器[Ack],[Fin]
因此断开连接是从客户端初始化的.

我配偶,可能是我的套接字以错误的方式初始化了.我是C#和NET方面的新手,所以也许更有经验的人可以帮助我.

下面是打开连接的一段代码:

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;
    }


连接"在与用户界面不同的线程中运行.线程正在定期检查是否已准备好读取新消息,如果已读取,则对其进行处理并回到等待新消息的状态.从另一面看,如果UI要发送内容,则将消息放在发送缓冲区的末尾.每次迭代中的连接线程都在检查发送缓冲区中是否有东西,如果有则发送它.

简而言之,看起来这是湖:


"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);
    }
}

推荐答案

,您需要执行完整的IISRESET.我认为,当需要完全重新启动网络托管的应用程序时,推荐的现代"方法是按如下方式回收应用程序池:iisapp/a< app_pool_id> /r(Windows 2003)或appcmd回收apppool/apppool.name:<app_pool_name> (Windows 2008).
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).


这篇关于C#,NET TCP连接无故关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆