我有一个客户端-服务器应用程序。
服务器可以处理多个客户端连接。
当我单击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;
}
}
});
}
}
}
有任何想法吗?