本小节通过设计一个和例5-1相同网络聊天功能的程序来说明如何编写异步TCP应用程序。之所以在这个例子中仍然完成和同步聊天相同的功能,是为了让读者能通过代码更好地体会同步和异步之间的差别。
【例5-3】利用基于IAyncResult的异步设计模式,编写一个与例5-1相同的网络聊天程序。
5.5.1 服务器端编程
根据系统要求,服务器必须能识别不同的客户,而且需要指明与哪个客户通信,服务器端的程序具体编写步骤如下。
(1)创建一个名为AsyncTcpServer的Windows应用程序项目,将Form1.cs换名为FormServer.cs,设计界面如图5-9所示。
图5-9 FormServer.cs的设计界面
(2)在解决方案资源管理器中,用鼠标右击项目名,选择【添加】→【类】,添加一个类文件User.cs,用于保存与客户通信需要的信息。代码如下:
class User
{
public TcpClient client { get; private set; }
public BinaryReader br { get; private set; }
public BinaryWriter bw { get; private set; }
public string userName { get; set; }
public User(TcpClient client)
{
this.client = client;
NetworkStream networkStream = client.GetStream();
br = new BinaryReader(networkStream);
bw = new BinaryWriter(networkStream);
}
public void Close()
{
br.Close();
bw.Close();
client.Close();
}
}
(3)切换到FormServer的代码编辑方式下,添加对应按钮的Click事件以及其他代码,源程序如下:
public partial class FormServer : Form
{
/// <summary>保存连接的所有用户</summary>
private List<User> userList = new List<User>();
/// <summary>使用的本机IP地址</summary>
IPAddress localAddress;
/// <summary>监听端口</summary>
private const int port = 51888;
private TcpListener myListener;
/// <summary>是否正常退出所有接收线程</summary>
bool isExit = false;
public FormServer()
{
InitializeComponent();
listBoxStatus.HorizontalScrollbar = true;
IPAddress[] addrIP = Dns.GetHostAddresses(Dns.GetHostName());
//localAddress = addrIP[0];
foreach (var ip in addrIP)
{
//判断是否为IPv4地址
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localAddress = ip;
break;
}
}
buttonStop.Enabled = false;
}
/// <summary>【开始监听】按钮的Click事件</summary>
private void buttonStart_Click(object sender, EventArgs e)
{
myListener = new TcpListener(localAddress, port);
myListener.Start();
AddItemToListBox(string.Format("开始在{0}:{1}监听客户连接", localAddress, port));
Thread myThread = new Thread(ListenClientConnect);
myThread.Start();
buttonStart.Enabled = false;
buttonStop.Enabled = true;
}
/// <summary>监听客户端请求</summary>
private void ListenClientConnect()
{
TcpClient newClient = null;
while (true)
{
ListenClientDelegate d = new ListenClientDelegate(ListenClient);
IAsyncResult result = d.BeginInvoke(out newClient, null, null);
//使用轮询方式来判断异步操作是否完成
while (result.IsCompleted == false)
{
if (isExit)
{
break;
}
Thread.Sleep(250);
}
//获取Begin方法的返回值和所有输入/输出参数
d.EndInvoke(out newClient, result);
if (newClient != null)
{
//每接受一个客户端连接,就创建一个对应的线程循环接收该客户端发来的信息
User user = new User(newClient);
Thread threadReceive = new Thread(ReceiveData);
threadReceive.Start(user);
userList.Add(user);
AddItemToListBox(string.Format("[{0}]进入", newClient.Client.RemoteEndPoint));
AddItemToListBox(string.Format("当前连接用户数:{0}", userList.Count));
}
else
{
break;
}
}
}
private delegate void ListenClientDelegate(out TcpClient client);
/// <summary>接受挂起的客户端连接请求</summary>
private void ListenClient(out TcpClient newClient)
{
try
{
newClient = myListener.AcceptTcpClient();
}
catch
{
newClient = null;
}
}
/// <summary>处理接收的客户端数据</summary>
private void ReceiveData(object userState)
{
User user = (User)userState;
TcpClient client = user.client;
while (isExit == false)
{
string receiveString = null;
ReceiveMessageDelegate d = new ReceiveMessageDelegate(ReceiveMessage);
IAsyncResult result = d.BeginInvoke(user, out receiveString, null, null);
//使用轮询方式来判断异步操作是否完成
while (result.IsCompleted == false)
{
if (isExit)
{
break;
}
Thread.Sleep(250);
}
//获取Begin方法的返回值和所有输入/输出参数
d.EndInvoke(out receiveString, result);
if(receiveString==null)
{
if (isExit == false)
{
AddItemToListBox(string.Format("与[{0}]失去联系,已终止接收该用户信息", client.Client.RemoteEndPoint));
RemoveUser(user);
}
break;
}
AddItemToListBox(string.Format("来自[{0}]:{1}", user.client.Client.RemoteEndPoint, receiveString));
string[] splitString = receiveString.Split(',');
switch (splitString[0])
{
case "Login":
user.userName = splitString[1];
AsyncSendToAllClient(user, receiveString);
break;
case "Logout":
AsyncSendToAllClient(user, receiveString);
RemoveUser(user);
return;
case "Talk":
string talkString = receiveString.Substring(splitString[0].Length + splitString[1].Length + 2);
AddItemToListBox(string.Format("{0}对{1}说:{2}",
user.userName, splitString[1], talkString));
AsyncSendToClient(user, "talk," + user.userName + "," + talkString);
foreach (User target in userList)
{
if (target.userName == splitString[1] && user.userName != splitString[1])
{
AsyncSendToClient(target, "talk," + user.userName + "," + talkString);
break;
}
}
break;
default:
AddItemToListBox("什么意思啊:" + receiveString);
break;
}
}
}
delegate void ReceiveMessageDelegate(User user, out string receiveMessage);
/// <summary>接受客户端发来的信息</summary>
private void ReceiveMessage(User user, out string receiveMessage)
{
try
{
receiveMessage = user.br.ReadString();
}
catch (Exception ex)
{
AddItemToListBox(ex.Message);
receiveMessage = null;
}
}
/// <summary>异步发送message给user</summary>
private void AsyncSendToClient(User user, string message)
{
SendToClientDelegate d = new SendToClientDelegate(SendToClient);
IAsyncResult result = d.BeginInvoke(user, message, null, null);
while (result.IsCompleted == false)
{
if (isExit)
{
break;
}
Thread.Sleep(250);
}
d.EndInvoke(result);
}
private delegate void SendToClientDelegate(User user, string message);
/// <summary>发送message给user</summary>
private void SendToClient(User user, string message)
{
try
{
//将字符串写入网络流,此方法会自动附加字符串长度前缀
user.bw.Write(message);
user.bw.Flush();
AddItemToListBox(string.Format("向[{0}]发送:{1}",
user.userName, message));
}
catch
{
AddItemToListBox(string.Format("向[{0}]发送信息失败",
user.userName));
}
}
/// <summary>异步发送信息给所有客户</summary>
private void AsyncSendToAllClient(User user, string message)
{
string command = message.Split(',')[0].ToLower();
if (command == "login")
{
for (int i = 0; i < userList.Count; i++)
{
AsyncSendToClient(userList[i], message);
if (userList[i].userName != user.userName)
{
AsyncSendToClient(user, "login," + userList[i].userName);
}
}
}
else if (command == "logout")
{
for (int i = 0; i < userList.Count; i++)
{
if (userList[i].userName != user.userName)
{
AsyncSendToClient(userList[i], message);
}
}
}
}
/// <summary>移除用户</summary>
private void RemoveUser(User user)
{
userList.Remove(user);
user.Close();
AddItemToListBox(string.Format("当前连接用户数:{0}", userList.Count));
}
private delegate void AddItemToListBoxDelegate(string str);
/// <summary>在ListBox中追加状态信息</summary>
/// <param name="str">要追加的信息</param>
private void AddItemToListBox(string str)
{
if (listBoxStatus.InvokeRequired)
{
AddItemToListBoxDelegate d = AddItemToListBox;
listBoxStatus.Invoke(d, str);
}
else
{
listBoxStatus.Items.Add(str);
listBoxStatus.SelectedIndex = listBoxStatus.Items.Count - 1;
listBoxStatus.ClearSelected();
}
}
/// <summary>【停止监听】按钮的Click事件</summary>
private void buttonStop_Click(object sender, EventArgs e)
{
AddItemToListBox("开始停止服务,并依次使用户退出!");
isExit = true;
for (int i = userList.Count - 1; i >= 0; i--)
{
RemoveUser(userList[i]);
}
//通过停止监听让myListener.AcceptTcpClient()产生异常退出监听线程
myListener.Stop();
buttonStart.Enabled = true;
buttonStop.Enabled = false;
}
/// <summary>关闭窗口时触发的事件</summary>
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (myListener != null)
{
//引发buttonStop的Click事件
buttonStop.PerformClick();
}
}
}
监听、发送数据和接收数据均使用异步方式调用同步的方法。实现监听功能时,先声明和ListenClient方法具有相同签名的委托ListenClientDelegate,公共语言运行时会自动为该委托定义BeginInvoke方法和EndInvoke方法。在方法ListenClientConnect()中通过调用委托ListenClientDelegate的对象d的BeginInvoke方法开始异步执行,调用BeginInvoke方法后,该方法会返回IAsyncResult类型的接口result,然后通过轮询方式检查result.IsCompleted的值以判断异步调用是否完成。如果没有完成,则将该线程挂起250ms。在轮询过程中,d的BeginInvoke方法在ThreadPool中创建的线程会继续执行异步方法。如果异步调用尚未完成,则d的EndInvoke会一直阻止调用线程,直到异步调用完成。异步调用完成后得到与之成功建立连接的TcpClient类型的客户端对象newClient。发送和接收数据的处理方式与之相同。
(4)按