English 中文(简体)
网络Stream. Read 正在屏蔽
原标题:networkStream.Read is blocking
  • 时间:2012-05-23 11:46:19
  •  标签:
  • c#

我将撰写一个简单的应用程序, 它将与服务器连接。 但是我想将简单的聊天命令发送正常( 见下文 < code> Console. readLine /code > ) 。 但是, 此脚本不会到达 < code>string message = console. readLine (); , 因为它在 < code> bytesRead = clientStream. read( 消息, 0, 4096); 被封锁 。

我想继续此脚本, 但是如果有字节来源, 它应该处理它们( 就像它现在正在做的那样) 如果没有字节来源, 它应该通过脚本, 等待用户输入 。 如何做到这一点?

        TcpClient tcpClient = (TcpClient)client;
        NetworkStream clientStream = tcpClient.GetStream();

        byte[] message = new byte[4096];
        int bytesRead;

        while (true)
        {
            bytesRead = 0;

            try
            {
                // Blocks until a client sends a message                    
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch (Exception)
            {
                // A socket error has occured
                break;
            }

            if (bytesRead == 0)
            {
                // The client has disconnected from the server
                break;
            }

            // Message has successfully been received
            ASCIIEncoding encoder = new ASCIIEncoding();

            // Output message
            Console.WriteLine("To: " + tcpClient.Client.LocalEndPoint);
            Console.WriteLine("From: " + tcpClient.Client.RemoteEndPoint);
            Console.WriteLine(encoder.GetString(message, 0, bytesRead));

            // Return message
            string Message = Console.ReadLine();
            if (Message != null)
            {
                byte[] buffer = encoder.GetBytes(Message);
                clientStream.Write(buffer, 0, buffer.Length);
                clientStream.Flush();
            }
最佳回答

您可以尝试使用 < a href=> 。 http:// msdn.microsoft. com/ en- us/library/ system. net. sockets. netrestregy. datarept. aspx" rel= "noreferr" > DataAAvailable 属性。 它会告诉您插座上是否还有东西在等待。 如果它是假的, 不要使用 < code> Read 呼叫 。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签