私はクライアントサーバーアプリケーションを持っています。
サーバーは、複数のクライアント接続を処理できます。
クライアントのボタンをクリックする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();
}
}
}
RemoteBatcherServer :
Client.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;
}
}
Listener.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;
}
}
});
}
}
}
何か案は?