所以我有一個客戶端服務器模型,它基於TCPClient流並基於安全目的將其轉換爲SSLStream,但每當客戶端想要向服務器發送新的東西時,它就會打開一個到服務器的新TCP連接,服務器結束連接。我將如何去傾聽來自同一個流的額外請求?不知道如何,這就是爲什麼我最終殺死流。對不起,如果它很混亂,我可以修改,如果不能理解。謝謝!在同一個流上監聽其他請求(TCPClient/SSLStream)?
public void ServerListen()
{
TCPListener Server = new TCPListener(IPAddress.Any, 8001);
while (true)
{
Server.Start();
TCPClient TempClient = Server.AcceptTCPClient();
HandleRequest NewRequest = new HandleRequest(TempClient); // Send to a data handler class and open a separate thread to allow for additional clients to connect
}
}
public class HandleRequest
{
TCPClient WorkingClient;
public HandleRequest(TCPClient TempClient)
{
WorkingClient = TempClient;
(new Thread(new ThreadStart(DoWork))).Start();
}
public static void DoWork()
{
// Do Something Here With Data From Client
ProvideResponse(SomeData);
}
public static void ProvideResponse(object Data)
{
SSLStream SecureStream = new SSLStream(WorkingClient, false); // Kill inner stream after creating a secure connection
SecureStream.AuthenticateAsServer(MyCertificate, false, SslProtocols.Tls, true);
XmlSerializer XS = new XMLSerializer(typeof(someclass));
someclass TempObject = new someclass(){ InnerData = Data};
if (SecureStream.CanWrite)
{
XS.Serialize(SecureStream, TempObject);
}
SecureStream.Close();
}
}
我其實覺得很有幫助。因此,一旦我設置SSLStream並進行身份驗證,將內部流打開,TCPClient就會使用底層SSL連接?如果是這樣的話,一旦「ProvideResponse」方法完成,我將如何監聽來自TCPClient的額外請求? – CodeJrMasterX
你可以使用'NetworkStream stream = myTCPClient.GetStream()',並且通過該流進行未加密的通信,並從中創建'SslStream'。 –
對不起,也許我錯過了。我可以創建SSLStream(請參閱提供響應),但是一旦我完成並完成了我需要的工作(發送響應),如何通過TCPClient接受進一步的請求?它會像TCPListener' While(WorkingClient.Connect){// do work}'嗎? – CodeJrMasterX