简体   繁体   English

在从 NetworkStream 读取之前,我的程序冻结

[英]My program freeze, before reading from NetworkStream

Hello I am a student and I'm not used to c# yet.你好,我是一名学生,我还不习惯 c#。 I am writing a program with server and client, which are connected with socket.我正在编写一个带有服务器和客户端的程序,它们通过套接字连接。 I am trying to implement a way to read from stream, when data is available, with while loop.我正在尝试实现一种从 stream 读取数据的方法,当数据可用时,使用 while 循环。 I can avoid chrashing if I show message with MessageBox.Show() before reading/writing stream.如果在读/写 stream 之前使用 MessageBox.Show() 显示消息,我可以避免崩溃。 I don't know why program without displayed message isn't working...我不知道为什么没有显示消息的程序不起作用...

public partial class Form1 : Form
{
    const int port = 22222;
    const string ip = "127.0.0.1";
    IPAddress ipServer = IPAddress.Parse(ip);
    TcpListener server = null;
    TcpClient client = null;
    Thread thServer = null;
    Thread thClient = null;
    NetworkStream dataStream = null;
    const int a = 11;
    const int n = 251;
    string receivedMessage = "";
    bool? izbira = null;



    public Form1()
    {
        InitializeComponent();

        thServer = new Thread(new ThreadStart(startServer));
        thServer.IsBackground = true;
        thServer.Start();    
    }

    void startServer() {

        server = new TcpListener(ipServer, port);
        server.Start();
        textBox4.Invoke(new Action(() => textBox4.AppendText("Strežnik: zagnan na: IP: " + ip + ", port:" + port)));
        client = new TcpClient();
        client = server.AcceptTcpClient();
        NetworkStream dataStream = client.GetStream();
        textBox4.Invoke(new Action(() => textBox4.AppendText(Environment.NewLine + "Strežnik: Sprejet nov uporabnik")));
        if (izbira == true)
        {
            byte[] message = new byte[1024];
            if(dataStream.DataAvailable)
                dataStream.Read(message, 0, message.Length);
            receivedMessage = Encoding.UTF8.GetString(message);
            textBox4.Invoke(new Action(() => textBox4.AppendText(Environment.NewLine + "Strežnik: Dobil sem sporočilo: " + receivedMessage)));

            message = new byte[1024];
            message = Encoding.UTF8.GetBytes("drugo sporocilo!");
            dataStream.Write(message, 0, message.Length);
        }
        else {
            byte[] message = new byte[1024];
            message = Encoding.UTF8.GetBytes("serbus");
            dataStream.Write(message, 0, message.Length);


        }

    }

    void button1_Click(object sender, EventArgs e) {
        if (izbira == null) {
            textBox4.Invoke(new Action(() => textBox4.AppendText(Environment.NewLine + "Izbrati morate ali boste datoteko prenesli ali poslali!")));
            return;
        }
        this.button1.Enabled = false;
        client = new TcpClient();
        IPAddress insertedIp = IPAddress.Parse(textBox1.Text);

        client.Connect(insertedIp, Convert.ToInt32(textBox3.Text));
        dataStream = client.GetStream();


        if (izbira == true)
        {
            byte[] message = new byte[1024];
            message = Encoding.UTF8.GetBytes("hejj");

            dataStream.Write(message, 0, message.Length);

            message = new byte[1024];

            MessageBox.Show("");
            while (true)
            {
                if (dataStream.DataAvailable)
                {

                    dataStream.Read(message, 0, message.Length);
                    break;
                }
            }

            receivedMessage = Encoding.UTF8.GetString(message);
            textBox4.Invoke(new Action(() => textBox4.AppendText(Environment.NewLine + "Strežnik: Dobil sem sporočilo: " + receivedMessage)));
        }
        else {

            byte[] message = new byte[1024];
            MessageBox.Show("Serbus");
            while (true)
            {
                if (dataStream.DataAvailable)
                {

                    dataStream.Read(message, 0, message.Length);
                  break;
                }

            }
            receivedMessage = Encoding.UTF8.GetString(message);
            textBox4.Invoke(new Action(() => textBox4.AppendText(Environment.NewLine + "Strežnik: Dobil sem sporočilo: " + receivedMessage)));
        }
    }

    void buttonUpload_Click(object sender, EventArgs e) {
        izbira = true;
        this.buttonDownload.Enabled = false;
    }
    void buttonDownload_Click(object sender, EventArgs e) {
        izbira = false;
        this.buttonUpload.Enabled = false;
        this.button2.Text = "Prenesi";
    }

Now this is mostly guessing, as we barely have enough code to say anything for certain:现在这主要是猜测,因为我们几乎没有足够的代码来肯定地说什么:

What about the JiT Compiler? JiT 编译器呢?

One of it's purposes is dead code detection.它的目的之一是检测死代码。 It cuts out code it predicts will not have an effect.它削减了它预测不会产生影响的代码。 Unfortunately, it is still only a computer programm, so false positives happen.不幸的是,它仍然只是一个计算机程序,所以会发生误报。 For example, trying to force a OOM Exception for x32 Framework installations, I had to go out of my way to not have the code cut out by the JiT:例如,试图强制 x32 框架安装出现 OOM 异常,我不得不 go不让JiT 删除代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OOM_32_forced
{
    class Program
    {
        static void Main(string[] args)
        {
            //each short is 2 byte big, Int32.MaxValue is 2^31.
            //So this will require a bit above 2^32 byte, or 2 GiB
            short[] Array = new short[Int32.MaxValue];

            /*need to actually access that array
            Otherwise JIT compiler and optimisations will just skip
            the array definition and creation */
            foreach (short value in Array)
                Console.WriteLine(value);
        }
    }
}

A MessageBox will prevent this whole function from being cut out as dead code. MessageBox 将防止整个 function 被删除为死代码。 Outputting something towards the user is alwasy asumed to "have a effect".向用户输出某些东西总是被假定为“有效果”。 However seperate parts of it are still eligible for dead code detection.然而,它的单独部分仍然有资格进行死代码检测。

Unforunately you have not provided us with a minimal, complete verifyable example.很遗憾,您没有向我们提供最小的、完整的可验证示例。 So we can not really help you figure it out any further.因此,我们无法真正帮助您进一步弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM