简体   繁体   English

缓冲存储器分配

[英]buffer memory allocation

I've got problems with buffer memory allocation. 我在缓冲存储器分配方面遇到了问题。 I have WF chat application with two threads: one sending, one receiving. 我有带有两个线程的WF聊天应用程序:一个发送,一个接收。 First client sends message to the server: 第一个客户端向服务器发送消息:

    private async void send_button_Click(object sender, EventArgs e)
    {
        string msg = this.messageTextBox.Text.Trim();
        await Task.Run(() =>
        {
            buf = Encoding.ASCII.GetBytes(msg); //buf-buffer for send/rcv
            clientSock.SendTo(buf, servEP);
            this.Invoke((Action)delegate
            {
                this.dialogTextBox.Text += string.Format("client: {0}", 
                      this.messageTextBox.Text.Trim());
            });
         buf = new byte[1024]; //allocate memory for receiving
        });
    }

Then server responds, and here I get a problem with buffer: 然后服务器响应,在这里我遇到了缓冲区问题:

    private async void Form1_Load(object sender, EventArgs e)
    {
        clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 
                             ProtocolType.Udp);
        servIPEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 50000);
        servEP = (EndPoint)servIPEP;

        clientIPEP = new IPEndPoint(IPAddress.Any, 43000);
        clientEP = (EndPoint)clientIPEP;
        clientSock.Bind(clientEP);
        while (true)
        {
            await Task.Run(() =>
            {
                //buf = new byte[1024] I tried to allocate here, but it's not working.

                int rcv = clientSock.ReceiveFrom(buf, 0, buf.Length, 
                    SocketFlags.None, ref servEP); // it returns right int value!
                msg = Encoding.ASCII.GetString(buf, 0, rcv); //but buf is empty..
                this.Invoke((Action)delegate
                {
                    this.dialogTextBox.Text += rcv;
                    this.dialogTextBox.Text += string.Format("server: {0},rcv: {1}", 
                        msg, rcv) + Environment.NewLine; //msg is empty...
                });
            });
        }
    }

It is really strange, because if the server re-sends the message, it receives in the client without a problem. 确实很奇怪,因为如果服务器重新发送消息,它将毫无问题地接收到客户端。 But first message always null. 但是第一个消息始终为空。 In fact it receives the first message, I tested 实际上,它收到了我测试过的第一条消息

int rcv = clientSock.ReceiveFrom(buf, ref servEP);

it returns right int, but buf values are zeros. 它返回right int,但buf值为零。

What is buf.Length? 什么是buf.Length? Also the line buf = new byte[1024]; 还有一行buf = new byte [1024]; //allocate memory for receiving confuses me //分配用于接收的内存使我感到困惑

Could you check what the size is the first time you run? 您能检查一下您第一次跑步的尺码是多少吗? Maybe its zero? 也许是零? I don't see where you declared it which is why its bothering me. 我看不到您在哪里声明它,这就是为什么它困扰我。 Why don't you write var buf2 = new byte[1024]; 你为什么不写var buf2 = new byte [1024]; and change all the buf to buf2. 并将所有buf更改为buf2。 Actually cant you simply write byte[] buf instead of buf =? 实际上,您不能简单地编写byte [] buf而不是buf =吗? and why doesnt it work? 为什么不起作用? It cant be a compile error? 它不能是编译错误吗?

-was a comment but i have a feeling this may solve so. -有评论,但我有感觉可以解决。 I'll delete if it isn't the case- 如果不是这种情况,我将删除-

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

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