스트리밍 서버에 연결하는이 코드가 있습니다. 서버가 수시로 끊어 지므로이를 감지하고 필요할 때 연결을 다시 시작하려고합니다.C# NetworkStream은 모든 종류의 연결 해제 및 예외를 감지합니다.
이 코드에서 어떻게 예외를 감지 할 수 있습니까?
이제 연결이 끊어 지므로이를 잡지 못합니다.
this.ns = new NetworkStream(server);
while (true)
{
// Incoming message may be larger than the buffer size.
do
{
byte[] myReadBuffer = new byte[10240 * 5];
await this.ns.ReadAsync(myReadBuffer, 0, myReadBuffer.Length).ContinueWith((numberOfBytesRead) =>
{
string message = Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead.Result);
p.Invoke(message);
});
}
while (this.ns.DataAvailable);
}
이 코드가 의도 한대로 작동하고 개선점이있을 경우이 질문을 [코드 검토] (https://codereview.stackexchange.com)에 게시해야합니다. –
@ s.m. 아니, 정말 예외가 발생하고 그들을 잡을 수 없어 ... – SexyMF
try/catch에서 올바른 예외에 대한 코드를 시도 했습니까? –