0

我有一个客户端-服务器应用程序。
服务器可以处理多个客户端连接。

当我单击Close客户端中的按钮时,我希望它会断开连接,并从服务器表单中的客户端列表中删除。

但是,它进入了一个无限循环,导致客户端从表单中删除ListView,然后再次添加到表单中ListView

这是我的代码:

RemoteBatcherClientForm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace RemoteBatcherClient
{
    public partial class RemoteBatcherClientForm : Form
    {
        Socket clientSock;

        public RemoteBatcherClientForm()
        {
            InitializeComponent();
            clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        private void connectBtn_Click(object sender, EventArgs e)
        {
            clientSock.Connect("127.0.0.1", 8);
            MessageBox.Show("Connected");
        }

        private void sendBtn_Click(object sender, EventArgs e)
        {
            if (clientSock.Send(Encoding.Default.GetBytes(txtMsg.Text)) > 0)
            {
                MessageBox.Show("Data Sent");
            }
        }

        private void closeBtn_Click(object sender, EventArgs e)
        {
            clientSock.Close();
            clientSock.Dispose();
            Close();
        }
    }
}

远程批处理服务器

客户端.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace RemoteBatcherServer
{
    class Client
    {
        public string ID
        {
            get;
            private set;
        }

        public IPEndPoint EndPoint
        {
            get;
            private set;
        }

        Socket serverSock;

        public Client(Socket accepted)
        {
            serverSock = accepted;

            ID = Guid.NewGuid().ToString();
            EndPoint = (IPEndPoint)serverSock.RemoteEndPoint;
            serverSock.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
        }

        void callback(IAsyncResult ar)
        {
            try
            {
                serverSock.EndReceive(ar);

                byte[] receiveBuffer = new byte[8192];

                int rec = serverSock.Receive(receiveBuffer, receiveBuffer.Length, 0);

                if (rec < receiveBuffer.Length)
                {
                    Array.Resize<byte>(ref receiveBuffer, rec);
                }

                 if (Receieved != null)
                {
                    Receieved(this, receiveBuffer);
                }

                serverSock.BeginReceive(new byte [] {0}, 0, 0, 0, callback, null);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                Close();

                if (Disconnected != null)
                {
                    Disconnected(this);
                }
            }
        }

        public void Close()
        {
            serverSock.Close();
            serverSock.Dispose();
        }

        public delegate void ClientReceievedHandler(Client sender, byte[] data);
        public delegate void ClientDisconnectedHandler(Client sender);

        public event ClientReceievedHandler Receieved;
        public event ClientDisconnectedHandler Disconnected;
    }
}

监听器.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace RemoteBatcherServer
{
    class Listener
    {
        Socket sck;

        public bool Listening
        {
            get;
            private set;
        }

        public int Port
        {
            get;
            private set;
        }

        public Listener(int port)
        {
            Port = port;
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        public void Start()
        {
            if (Listening)
                return;

            sck.Bind(new IPEndPoint(0, Port));
            sck.Listen(0);

            sck.BeginAccept(callback, null);

            Listening = true;
        }

        public void Stop()
        {
            if (!Listening)
                return;

            sck.Close();
            sck.Dispose();
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        }

        void callback(IAsyncResult ar)
        {
            try
            {
                Socket acceptSocket = sck.EndAccept(ar);

                if (SockeetAccepted != null)
                {
                    SockeetAccepted(acceptSocket);
                }

                sck.BeginAccept(callback, null);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        public delegate void SocketAcceptHandler(Socket acceptSocket);
        public event SocketAcceptHandler SockeetAccepted;
    }
}

RemoteBatcherServerForm.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace RemoteBatcherServer
{
    public partial class RemoteBatcherServerForm : Form
    {
        Listener listener;

        public RemoteBatcherServerForm()
        {
            InitializeComponent();
            listener = new Listener(8);
            listener.SockeetAccepted += new Listener.SocketAcceptHandler(listenerSocketAccepted);

            Load += new EventHandler(MainLoad);
        }

        void MainLoad(object sender, EventArgs e)
        {
            listener.Start();
        }

        void listenerSocketAccepted(Socket socket)
        {
            Client client = new Client(socket);
            client.Receieved += new Client.ClientReceievedHandler(clientReceived);
            client.Disconnected += new Client.ClientDisconnectedHandler(clientDisconnected);

            Invoke((MethodInvoker)delegate
            {
                ListViewItem item = new ListViewItem();
                item.Text = client.EndPoint.ToString();
                item.SubItems.Add(client.ID);
                item.SubItems.Add("XX");
                item.SubItems.Add("XX");
                item.Tag = client;
                lstClients.Items.Add(item);
            });
        }

        void clientReceived(Client sender, byte[] data)
        {
            Invoke((MethodInvoker)delegate
            {
                for (int i = 0; i < lstClients.Items.Count; i++)
                {
                    Client client = lstClients.Items[i].Tag as Client;

                    if (client.ID == sender.ID)
                    {
                        lstClients.Items[i].SubItems[2].Text = Encoding.Default.GetString(data);
                        lstClients.Items[i].SubItems[3].Text = DateTime.Now.ToString();
                        break;
                    }
                }
            });
        }

        void clientDisconnected(Client sender)
        {
            Invoke((MethodInvoker)delegate
            {
                for (int i = 0; i < lstClients.Items.Count; i++)
                {
                    Client client = lstClients.Items[i].Tag as Client;

                    if (client.ID == sender.ID)
                    {
                        lstClients.Items.RemoveAt(i);
                        break;
                    }
                }
            });
        }       
    }
}

有任何想法吗?

4

2 回答 2

0

除非我误解了您的代码,否则您似乎正在混合同步和异步调用。我认为您正在无限期地阻止您的 serverSock.Receive 呼叫。请参阅:http: //msdn.microsoft.com/en-us/library/8s4y8aff.aspx - “如果没有数据可供读取,则接收方法将阻塞直到数据可用,除非超时值由使用 Socket.ReceiveTimeout"

serverSock.EndReceive(ar);
byte[] receiveBuffer = new byte[8192];
int rec = serverSock.Receive(receiveBuffer, receiveBuffer.Length, 0);

尝试检查您是否收到 0 个字节。您可能还想将接收呼叫换成 BeginReceive。这可能看起来有点令人困惑,因为您实际上希望继续重新调用 BeginReceive 直到收到 0 个字节,这表明对方已关闭连接:

int i = serverSock.EndReceive(ar);
if (i > 0)
    {
        serverSock.BeginReceive(receiveBuffer, 0, receiveBuffer.Length, 0, 
                                 new AsyncCallback(callback), null);
    }
else
    {
        serverSock.Close();
    }

请注意,您可以将 null 参数交换为您传递的状态对象。您可以在 Client 构造函数中的初始 BeginReceive 调用中执行此操作 - 这可能是传递缓冲区的更好方法,用某种 State 类包装它。

于 2013-01-30T15:50:39.103 回答
0

这是一个快速的答案:修改Client.cs'void callback(IAsyncResult ar)方法以检查Socket.Available 属性是否返回 0(当客户端关闭时会发生这种情况):

void callback(IAsyncResult ar)
{
    try
    {
        //add these lines
        if (serverSock.Available == 0)
        {
            Disconnected(this);
            return;
        }

        serverSock.EndReceive(ar);

        byte[] receiveBuffer = new byte[8192];

        int rec = serverSock.Receive(receiveBuffer, receiveBuffer.Length, 0);

        if (rec < receiveBuffer.Length)
        {
            Array.Resize<byte>(ref receiveBuffer, rec);
        }

        if (Receieved != null)
        {
            Receieved(this, receiveBuffer);
        }

        serverSock.BeginReceive(new byte[] { 0 }, 0, 0, 0, callback, null);
    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
        Close();

        if (Disconnected != null)
        {
            Disconnected(this);
        }
    }
}
于 2013-01-30T15:52:18.690 回答