上篇中使用了ThreadPool加上Socket同步方式实现多客户端和单服务器端通讯,稍加修改,得到异步编程模型实现方式
主要使用到Socket的BeginSend, EndSend, BeginAccept, EndAccept, BeginReceive, EndReceive
代码:
// Server端
namespace SocketAPMServer
{
public partial class Form1 : Form
{
Socket socket;
public Form1()
{
InitializeComponent();
InitSocket();
}
private void InitSocket()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipEndPoint = new IPEndPoint(ipHostEntry.AddressList[3], 8092);
socket.Bind(ipEndPoint);
socket.Listen(20);
}
private void btnBeginRec_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem((_) =>
{
while (true)
{
Socket socketAccept = null;
if (socket.Poll(-1, SelectMode.SelectRead))
{
socket.BeginAccept(arAcpt => {
socketAccept = socket.EndAccept(arAcpt);
if (socketAccept != null)
{
ThreadPool.QueueUserWorkItem((o) =>
{
while (true)
{
byte[] byteArray = new byte[100];
socketAccept.BeginReceive(byteArray, 0, byteArray.Length, SocketFlags.None, (arRec) => {
socketAccept.EndReceive(arRec);
string strRec = System.Text.Encoding.UTF8.GetString(byteArray);
if (this.txtMsg.InvokeRequired)
{
this.txtMsg.Invoke(new ChangeText(ShowMsg), strRec);
}
}, null);
System.Threading.Thread.Sleep(100);
}
});
}
}, null);
}
}
});
System.Threading.Thread.Sleep(100);
}
delegate void ChangeText(string obj);
private void ShowMsg(string obj)
{
this.txtMsg.AppendText(obj + " ");
}
}
}
{
public partial class Form1 : Form
{
Socket socket;
public Form1()
{
InitializeComponent();
InitSocket();
}
private void InitSocket()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipEndPoint = new IPEndPoint(ipHostEntry.AddressList[3], 8092);
socket.Bind(ipEndPoint);
socket.Listen(20);
}
private void btnBeginRec_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem((_) =>
{
while (true)
{
Socket socketAccept = null;
if (socket.Poll(-1, SelectMode.SelectRead))
{
socket.BeginAccept(arAcpt => {
socketAccept = socket.EndAccept(arAcpt);
if (socketAccept != null)
{
ThreadPool.QueueUserWorkItem((o) =>
{
while (true)
{
byte[] byteArray = new byte[100];
socketAccept.BeginReceive(byteArray, 0, byteArray.Length, SocketFlags.None, (arRec) => {
socketAccept.EndReceive(arRec);
string strRec = System.Text.Encoding.UTF8.GetString(byteArray);
if (this.txtMsg.InvokeRequired)
{
this.txtMsg.Invoke(new ChangeText(ShowMsg), strRec);
}
}, null);
System.Threading.Thread.Sleep(100);
}
});
}
}, null);
}
}
});
System.Threading.Thread.Sleep(100);
}
delegate void ChangeText(string obj);
private void ShowMsg(string obj)
{
this.txtMsg.AppendText(obj + " ");
}
}
}
// Client端
namespace SocketAPMClient
{
class Program
{
static void Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipEndPoint = new IPEndPoint(ipHostEntry.AddressList[3], 8092);
socket.Connect(ipEndPoint);
while (true)
{
string input = Console.ReadLine();
try
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(input);
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, (ar) => {
socket.EndSend(ar);
}, null);
}
catch (Exception ex)
{
if (socket != null)
{
socket.Close();
}
Console.WriteLine("Client Error: " + ex.Message);
}
System.Threading.Thread.Sleep(100);
}
}
}
}
{
class Program
{
static void Main(string[] args)
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPHostEntry ipHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipEndPoint = new IPEndPoint(ipHostEntry.AddressList[3], 8092);
socket.Connect(ipEndPoint);
while (true)
{
string input = Console.ReadLine();
try
{
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(input);
socket.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, (ar) => {
socket.EndSend(ar);
}, null);
}
catch (Exception ex)
{
if (socket != null)
{
socket.Close();
}
Console.WriteLine("Client Error: " + ex.Message);
}
System.Threading.Thread.Sleep(100);
}
}
}
}