[英]C# Async TCP Server overkill?
這實際上是一個實現問題,所以我覺得最好從我的具體案例開始。
我有一個C#服務器,它從移動客戶端異步偵聽TCP連接。 當移動客戶端連接新線程啟動時,客戶端發送一些(通常<100字節)文本消息並接收相似大小的文本消息。 服務器響應后,它會關閉連接並結束線程。
當前的基本用法是用戶登錄,有時最多5分鍾檢查一些內容, 發送少量消息,從而快速連續地在服務器上創建新線程 ,並且幾小時后它們僅斷開連接。 此外,每個用戶都有自己的PC上運行的服務器,因此大多數服務器在任何給定時間只能連接一個客戶端,在RARE情況下只有兩個。
現在我遇到了以下錯誤, 現有連接被遠程主機強行關閉 ,它讓我思考, 我做錯了嗎?
所以我的問題:
回答這里的一些問題:
public void StartListening()
{
//Data buffer for incoming data.
byte[] bytes = new Byte[1024];
//Establish the local endpoint for the socket.
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Port);
//Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.DontLinger,1);
listener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,1);
//Bind the socket to the local endpoint and listen for
//incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);
while (listening)
{
//Set the event to nonsignaled state.
allDone.Reset();
//Start an asychronous socket to listen for connections.
Print("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
//Wait until a connection is made before continuing.
allDone.WaitOne();
}
}
catch (Exception e)
{
Print(e.ToString());
}
listener.Close();
}
public void AcceptCallback(IAsyncResult arg)
{
//Signal the main thread to continue.
allDone.Set();
try
{
//Get the socket that handles the client request.
Socket listener = (Socket) arg.AsyncState;
Socket handler = listener.EndAccept(arg);
//Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
catch (ObjectDisposedException ex)
{
Print("Server terminated from another thread.");
}
}
public void ReadCallback(IAsyncResult arg)
{
String content = String.Empty;
//Retrieve the state object and the handler socket
//from the asynchronous state object.
StateObject state = (StateObject) arg.AsyncState;
Socket handler = state.workSocket;
//Read data from the client socket.
int bytesRead = 0;
try
{
bytesRead = handler.EndReceive(arg);
}
catch (ObjectDisposedException ex)
{
Print("Process was terminated from another thread.");
}
if (bytesRead > 0)
{
//There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(
state.buffer,0,bytesRead));
//Check for end-of-file tag. If it is not there, read
//more data.
content = state.sb.ToString();
if (content.IndexOf("<EOF>") > -1)
{
content = content.Remove(content.Length-6);
//All the data has been read from the
//client. Display it on the console.
Print("Read " + content.Length + " bytes from socket. \n Data : " + content);
Respond(handler, content);
}
else
{
//Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
}
}
private void Send(Socket handler, String data)
{
//Convert the string data to byte data using ASCII encoding.
byte[] byteData = Encoding.ASCII.GetBytes(data);
//Begin sending the data to the remote device.
handler.BeginSend(byteData,0,byteData.Length,0,
new AsyncCallback(SendCallback),handler);
}
private void SendCallback(IAsyncResult arg)
{
try
{
//Retrieve the socket from the state object.
Socket handler = (Socket) arg.AsyncState;
//Complete sending the data to the remote device.
int bytesSent = handler.EndSend(arg);
Print("Sent " + bytesSent + " bytes to client.");
handler.Shutdown(SocketShutdown.Both);
//need to make this not linger around
handler.LingerState = new LingerOption(true,1);
handler.Close();
}
catch (Exception e)
{
Print(e.ToString());
}
}
理想情況下,您將使用.NET線程池,這比為每個連接創建新線程更有效。 你可以分享你確切的“異步”代碼 - 如果你在TCPListener上使用現有的異步模式,那么你可能已經在使用線程池了。
關於異常,當您的客戶端與服務器斷開連接時,您會看到這種情況。 是否在您設法接收所有數據之前發生了? 你在客戶端刷新插座嗎?
在完全崩潰服務器方面,只需繼續測試,並記錄任何全局未處理的異常。 這樣你就可以了解可以預期的一切。
您可能希望看一下這篇文章 ,其中列出了一些需要檢查的好東西。 例如,當您在套接字上使用.Listen()時,您的待辦事項是什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.