Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
streamread can';t在c#中从NetworkStream读取任何数据,_C#_Sockets_Network Programming_Streamreader_Streamwriter - Fatal编程技术网

streamread can';t在c#中从NetworkStream读取任何数据,

streamread can';t在c#中从NetworkStream读取任何数据,,c#,sockets,network-programming,streamreader,streamwriter,C#,Sockets,Network Programming,Streamreader,Streamwriter,我有一个具有ip和端口192.168.2.44:3000的传感器 我使用herculas连接到设备,如图所示: 在此处输入图像描述 我需要用c#实现这个软件,所以我写了以下代码: private static void Main(string[] args) { try { byte[] buffer = new byte[2048]; // read in chunks of 2KB

我有一个具有ip和端口192.168.2.44:3000的传感器

我使用herculas连接到设备,如图所示:

在此处输入图像描述

我需要用c#实现这个软件,所以我写了以下代码:

private static void Main(string[] args)
        {
            try
            {
                byte[] buffer = new byte[2048]; // read in chunks of 2KB
                int bytesRead;
                var listener = new TcpListener(IPAddress.Any, 3000);
                listener.Start();
                NetworkStream network_stream;
                StreamReader read_stream;
                StreamWriter write_stream;
                var client = listener.AcceptTcpClient();
                network_stream = client.GetStream();

                read_stream = new StreamReader(network_stream);
                write_stream = new StreamWriter(network_stream);

                write_stream.WriteLine("00010002000B0300010004C380");
                write_stream.Flush();  //veriyi gönderiyor

                string gelen;
                gelen = read_stream.ReadLine();
                Console.WriteLine(gelen);
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
当我放置断点时,
gelen=read_stream.ReadLine()返回
null

最终代码:

class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = null;
            try
            {
                Int32 port = 3000;
               // IPAddress localAddr = IPAddress.Parse(IPAddress.Any);

                server = new TcpListener(IPAddress.Any, port);

                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                String data = null;

                while (true)
                {
                    Console.Write("Waiting for a connection... ");
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");


                    data = null;

                    NetworkStream stream = client.GetStream();

                    byte[] aaa = { 0, 1, 0, 2, 0, 11, 3, 0, 1, 0, 4, 195, 128 };
                    stream.Write(aaa, 0, aaa.Length);
                    int i;

                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                        Console.WriteLine("Received: {0}", data);

                        data = data.ToUpper();
                        byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
                        stream.Write(msg, 0, msg.Length);
                        Console.WriteLine("Sent: {0}", data);
                    }

                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                server.Stop();
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }
    }
可能重复的