using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace 并发编程
{
class tcpServer
{
private Socket ServerSocket = null; //当前的socket对象
private IPEndPoint endpoint = null;
private TaskFactory taskFactory; //任务工厂对象
private Queue<Action> TaskQueue; //任务队列
public tcpServer(string IP, int Port)
{
TaskQueue = new Queue<Action>();
taskFactory = new TaskFactory();
endpoint = new IPEndPoint(IPAddress.Parse(IP), Port);
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Bind(endpoint);
ServerSocket.Listen(0);
}
/// <summary>
/// 开始接收客户端的连接
/// </summary>
public void StartAccept()
{
ServerSocket.BeginAccept(AcceptCallBack, null);
}
private void AcceptCallBack(IAsyncResult ar)
{
Socket client = ServerSocket.EndAccept(ar);
Console.WriteLine("有一个客户端连接进来");
tcpClient clientSocket = new tcpClient(client, this);
clientSocket.BeginReceive();
AddTask(StartAccept); //添加接收客户端连接的任务到任务队列
}
/// <summary>
/// 循环处理队列里面的任务
/// </summary>
public void AssignedTasks()
{
while (true)
{
if (TaskQueue.Count == 0) continue;
while (TaskQueue.Count > 0) //假如当前队列里面有任务
taskFactory.StartNew(TaskQueue.Dequeue()); //开启一个线程去执行
}
}
/// <summary>
/// 添加一个任务到任务队列
/// </summary>
/// <param name="action"></param>
public void AddTask(Action action)
{
TaskQueue.Enqueue(action);
}
}
class tcpClient
{
private Socket ClientSocket = null; //当前与客户端通信的对象
private tcpServer ServerSocket = null; //服务器对象的引用
private byte[] DataBuf; //数据缓冲区
public tcpClient(Socket client, tcpServer server)
{
DataBuf = new byte[256];
this.ClientSocket = client;
this.ServerSocket = server;
}
/// <summary>
/// 接收消息的事件
/// </summary>
public void BeginReceive()
{
ClientSocket.BeginReceive(DataBuf, 0, 256, SocketFlags.None, ReceiveCallBack, null);
}
/// <summary>
/// 接收消息完成之后的回调
/// </summary>
/// <param name="ar"></param>
private void ReceiveCallBack(IAsyncResult ar)
{
int count = ClientSocket.EndReceive(ar);
string msg = Encoding.UTF8.GetString(DataBuf, 0, count);
Console.WriteLine("接收到消息 = " + msg); //输出消息
ClientSocket.BeginSend(DataBuf, 0, count, SocketFlags.None, null, null);
ServerSocket.AddTask(BeginReceive); //把任务给主线程
}
}
class Program
{
static void Main(string[] args)
{
tcpServer server = new tcpServer("192.168.179.1", 7788);
server.StartAccept(); //开始接收客户端的连接
server.AssignedTasks(); //循环处理任务
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
namespace 并发编程
{
class tcpServer
{
private Socket ServerSocket = null; //当前的socket对象
private IPEndPoint endpoint = null;
private TaskFactory taskFactory; //任务工厂对象
private Queue<Action> TaskQueue; //任务队列
public tcpServer(string IP, int Port)
{
TaskQueue = new Queue<Action>();
taskFactory = new TaskFactory();
endpoint = new IPEndPoint(IPAddress.Parse(IP), Port);
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Bind(endpoint);
ServerSocket.Listen(0);
}
/// <summary>
/// 开始接收客户端的连接
/// </summary>
public void StartAccept()
{
ServerSocket.BeginAccept(AcceptCallBack, null);
}
private void AcceptCallBack(IAsyncResult ar)
{
Socket client = ServerSocket.EndAccept(ar);
Console.WriteLine("有一个客户端连接进来");
tcpClient clientSocket = new tcpClient(client, this);
clientSocket.BeginReceive();
AddTask(StartAccept); //添加接收客户端连接的任务到任务队列
}
/// <summary>
/// 循环处理队列里面的任务
/// </summary>
public void AssignedTasks()
{
while (true)
{
if (TaskQueue.Count == 0) continue;
while (TaskQueue.Count > 0) //假如当前队列里面有任务
taskFactory.StartNew(TaskQueue.Dequeue()); //开启一个线程去执行
}
}
/// <summary>
/// 添加一个任务到任务队列
/// </summary>
/// <param name="action"></param>
public void AddTask(Action action)
{
TaskQueue.Enqueue(action);
}
}
class tcpClient
{
private Socket ClientSocket = null; //当前与客户端通信的对象
private tcpServer ServerSocket = null; //服务器对象的引用
private byte[] DataBuf; //数据缓冲区
public tcpClient(Socket client, tcpServer server)
{
DataBuf = new byte[256];
this.ClientSocket = client;
this.ServerSocket = server;
}
/// <summary>
/// 接收消息的事件
/// </summary>
public void BeginReceive()
{
ClientSocket.BeginReceive(DataBuf, 0, 256, SocketFlags.None, ReceiveCallBack, null);
}
/// <summary>
/// 接收消息完成之后的回调
/// </summary>
/// <param name="ar"></param>
private void ReceiveCallBack(IAsyncResult ar)
{
int count = ClientSocket.EndReceive(ar);
string msg = Encoding.UTF8.GetString(DataBuf, 0, count);
Console.WriteLine("接收到消息 = " + msg); //输出消息
ClientSocket.BeginSend(DataBuf, 0, count, SocketFlags.None, null, null);
ServerSocket.AddTask(BeginReceive); //把任务给主线程
}
}
class Program
{
static void Main(string[] args)
{
tcpServer server = new tcpServer("192.168.179.1", 7788);
server.StartAccept(); //开始接收客户端的连接
server.AssignedTasks(); //循环处理任务
Console.ReadKey();
}
}
}