簡體   English   中英

主機中的軟件中止了已建立的連接

[英]An established connection was aborted by the software in host machine

我在某些主題中找不到答案。 我有一個客戶端/服務器代碼。 我在單獨的程序中有客戶端和服務器端。 這是客戶端:

 try
        {

        byte[] bytes = new byte[1024];
        string text = "";
        text = uitbtextmsg.Text;
        byte[] msg = Encoding.ASCII.GetBytes(text);
        int bytesSent = User.Send(msg);
        int bytesRec = User.Receive(bytes);

        uirtbmsg.Text = uirtbmsg.Text + '\n' + " " + text;
        }
        catch (Exception E)
        {
            MessageBox.Show(E.ToString());
        }

這是當我連接到服務器並且想與該套接字聊天時使用的。 這是我的聯系:

            byte[] bytes = new byte[1024];
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[2];
            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);
            Socket User = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);
            User.Connect(remoteEP);

這是服務器端:

 byte[] bytes = new Byte[1024];
        IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
        IPAddress ipAddress = ipHostInfo.AddressList[2];
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
        Socket listener = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);
        try
        {
            listener.Bind(localEndPoint);
            listener.Listen(10000);
            while (true)
            {
                Socket handler = listener.Accept();
                data = null;
                while (true)
                {
                    bytes = new byte[1024];
                    int bytesRec = handler.Receive(bytes);
                    data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
                    TextRecieved = data;
                    Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
                    textbox.Start();
                    break;
                }

                //uirtbreport.Text = data;
                byte[] msg = Encoding.ASCII.GetBytes(data);
                handler.Send(msg);
                handler.Shutdown(SocketShutdown.Both);
                handler.Close();
            }

        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }

當客戶端第一次向服務器發送消息時,此過程就可以了! 但是,當它發送另一條消息時,將引發異常:

System.Net.Socket.SocketException(0x80004005):已建立的連接被主機中位於System.Net.Sockets.Socket.Receive(Byte []緩沖區,Int32偏移量,Int32大小,SocketFlags socketFlags)的軟件中止.Net.Sockets.Socket.Receive(Byte [] buffer)\\ r \\ n在Client.Form1.uibtnsendmsg_Click(Object sender,EventArgs e)中...

可能是因為您打破了while循環(請看我的評論)

while (true)
{
    Socket handler = listener.Accept();
    data = null;

    while (true)
    {
        bytes = new byte[1024];
        int bytesRec = handler.Receive(bytes);

        data += Encoding.ASCII.GetString(bytes, 0, bytesRec);
        TextRecieved = data;
        Thread textbox = new Thread(new ThreadStart(WriteInReachTextBox));
        textbox.Start();

        // -----------> HERE!!!!!!

        break;
    }

    //uirtbreport.Text = data;
    byte[] msg = Encoding.ASCII.GetBytes(data);
    handler.Send(msg);
    handler.Shutdown(SocketShutdown.Both);
    handler.Close();
}

為什么要在“中斷”循環中創建線程?

另外,不要忘記這是一個流套接字,這意味着“數據包”可以作為一個緩沖區發送或拆分為多個“數據包”。

目前尚不清楚對我有什么textbox.Start()的做法,但在我看來,該服務器從客戶端讀取一些數據后,它打破了的“而(真)”,並關閉套接字連接。 這就是為什么您只能發送一條消息的原因。

Socket handler = listener.Accept();
...
while (true)
{
    .... read data
    textbox.Start();
    break;
}

/* Here you close the connection */
handler.Shutdown(SocketShutdown.Both);
handler.Close();

我認為您想在同一套接字上建立持久的tcp連接。

只是不要破壞循環而不關閉套接字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM