[C#]多线程TcpListener自定义事件

目的:
1:Server:可以接受多个连接
创建一个等待连接线程,处理新的客户TCP连接请求。
为每一个TCP连接建立一个服务线程。
在接受到请求后,触发外部的一个方法。所以具有onRequest事件
发布一个serverPort的属性,供外部连接。
2:Client:可以连接多个Server
需要知道远程Server的remoteHostName和remotePort属性
具有ProcessRequest方法,供外部调用
————–server code———————
///

/// 为程序安装一个Server,侦听所有连接请求
///
public class Server
{
  public event RequestEventHandler OnRequest;
private IPEndPoint localEndPoint;
public Server(int serverPort)
{
string localName=Dns.GetHostName();
IPHostEntry ipEntry=Dns.GetHostByName(localName);
IPAddress ip=ipEntry.AddressList[0];
localEndPoint=new IPEndPoint (ip,serverPort);
}
public void Start()
{
//创建新的TCP连接需要一个线程,他一直在运行。
//这里只需要把这个线程启动起来
            Thread createServer=new Thread(new ThreadStart(CreateServer));
            createServer.Name="createServer";
createServer.Start();
}
private void CreateServer()
{
//为每一个新的客户端连接请求建立TCP连接
TcpListener listener;
    listener = new TcpListener(localEndPoint);
listener.Start();
//Log.Write("Start to Listen at :"+_localEndPoint.ToString());
while (true)
{
Socket connection;
connection = listener.AcceptSocket();//接受到请求
Connection conHandler=new Connection();
conHandler.connection=connection;
conHandler.OnRequest+=new RequestEventHandler(connectionHandler_OnRequest);//一个连接的每个请求都会执行一个Server的方法
    Thread clientThread =new Thread(new ThreadStart(conHandler.HandleConnection));
clientThread.Start();
}
}
public void Stop()
{
 
}
private string connectionHandler_OnRequest(string request)
{
//这个方法用于触发使用该server的一个事件。
string response;
if(this.OnRequest!=null)
response=OnRequest(request);
else
response="SERVER ERROR : NOT DISPATCH THE SERVER METHODE";
return response;
}
}
public delegate string RequestEventHandler(string request);
///

/// 处理一个连接的每个请求
/// 通过事件OnRequest向外发布事件
///
internal class Connection
{
private NetworkStream socketStream;
private BinaryWriter writer;
private BinaryReader reader;
public event RequestEventHandler OnRequest;
private Socket _connection;
public Socket connection
{
set{this._connection=value;}
}
public void HandleConnection()
{
//处理一个TCP连接的每个请求
while (true)
{
socketStream = new NetworkStream(_connection);
writer = new BinaryWriter(socketStream);
reader = new BinaryReader(socketStream);
string request = "";
do
{
try
{
request = reader.ReadString();
string response=OnRequest(request);//引发处理事件
writer.Write(response);
}
catch (Exception)
{
break;
}
} while (request != "CLIENT>>>EXIT" && _connection.Connected);
writer.Close();
reader.Close();
socketStream.Close();
_connection.Close();
}
}
}
——————–end server code—————————-
——————–client code——————————–
///

/// 客户端,可以发布请求
///
public class Client
{
  private NetworkStream output;
private BinaryWriter writer;
private BinaryReader reader;
        TcpClient client ;
public Client(string remoteHostName,int remoteHostPort)
{
IPHostEntry ipEntry=Dns.GetHostByName(remoteHostName);
IPAddress remoteIP=ipEntry.AddressList[0];
  client = new TcpClient();
client.Connect(remoteIP,remoteHostPort);
output = client.GetStream();
writer = new BinaryWriter(output);
reader = new BinaryReader(output);
            //writer.Write("this is the client to connect");
}
public string ProcessRequest(string request)
{
    writer.Write(request);
//这里是同步消息,需要加入一个异步处理
string response=reader.ReadString();
return response;
}
public void Close()
{
writer.Close();
reader.Close();
output.Close();
client.Close();
}
}
————————end client code——————————

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏