简体   繁体   English

需要有关具有多个发送的C#异步套接字程序的帮助

[英]Need help with a C# Asynchronous Socket Program that has multiple sends

So, I have a board game that uses Asynchronous socket to operate over LAN. 因此,我有一个棋盘游戏,该游戏使用异步套接字通过LAN进行操作。 The thing is, I have little to no understanding of Asynchronous socket programming, or of threads, but I do my best to try. 关键是,我对异步套接字编程或线程了解甚少,甚至不了解,但我会尽力而为。

I based my program off a chat program, so I use that part to send multiple strings. 我的程序基于聊天程序,因此我使用该部分发送多个字符串。

So, here's part of the code for the Client: 因此,这是客户端代码的一部分:

private void Connect(IAsyncResult iar)
    {
        try
        {
            Socket client_conn = (Socket)iar.AsyncState;
            client_conn.EndConnect(iar);
            g_bmsg = new byte[1024];
            check = true;

            string szData = "@Player " + lblName.Text + " connected.";
            sendingFunction(szData);

            g_client_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(Receive), g_client_conn);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "GG");
        }
    }

    private void Send(IAsyncResult iar)
    {
        Socket client_conn = (Socket)iar.AsyncState;
        client_conn.EndSend(iar);
    }

    private void Receive(IAsyncResult iar)
    {
        if (g_bmsg.Length != 0)
        {
            SetLabelText(Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length));
            check = false;
        }
    }

    private void SetLabelText(string txt)
    {
        if (lblBuffer.InvokeRequired)
            lblBuffer.Invoke(new MethodInvoker(delegate { SetLabelText(txt); }));
        else
        {
            lblBuffer.Text = txt;
        }

        if (lblBuffer.Text.StartsWith("@"))
        {
            lblStatmsg.Text = lblBuffer.Text.Replace("@", "");
        }
        if (lblBuffer.Text.StartsWith("$"))
        {
            lblStatmsg.Text = "Server Settings Received.";
            lblBuffer.Text = lblBuffer.Text.Replace("$", "");
            option_Postn = int.Parse(lblBuffer.Text.Substring(0, 1));
            option_First = int.Parse(lblBuffer.Text.Substring(2, 1));
        }
        if (lblBuffer.Text.StartsWith("#"))
        {
            MessageBox.Show(lblBuffer.Text);
        }
    }

And here's part of the code for the Server: 这是服务器代码的一部分:

private void Accept(IAsyncResult iar)
    {
        Socket server_conn = (Socket)iar.AsyncState;

        g_server_conn = server_conn.EndAccept(iar);

        g_bmsg = new byte[1024];

        check = true;

        g_server_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(Recieve), g_server_conn);
    }

    private void Send(IAsyncResult iar)
    {
        Socket server_conn = (Socket)iar.AsyncState;

        server_conn.EndSend(iar);
    }

    private void Recieve(IAsyncResult iar)
    {
        try
        {
            Socket server_conn = (Socket)iar.AsyncState;

            server_conn.EndReceive(iar);

            if (g_bmsg.Length != 0)
            {
                SetLabelText(Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length));
                check = false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "GG");
        }
    }

    private void SetLabelText(string txt)
    {
        if (lblBuffer.InvokeRequired)
            lblBuffer.Invoke(new MethodInvoker(delegate { SetLabelText(txt); }));
        else
        {
            lblBuffer.Text = txt;
        }

        if (lblBuffer.Text.StartsWith("@"))
        {
            lblStatmsg.Text = lblBuffer.Text.Replace("@", "");
        }
        else if (lblBuffer.Text.StartsWith("#"))
        {
            MessageBox.Show(lblBuffer.Text);
        }
        else if (lblBuffer.Text.StartsWith("%"))
        {
        }
    }

Basically, since the game sends more than messages (it can send settings, or game pieces, etc), I ran the sender function everytime I need to send something, and on the other side, the Receiver decodes the string sent based on the first character (@ means the string is a setting, for example). 基本上,由于游戏发送的信息多(它可以发送设置或游戏片段等),因此每次需要发送内容时,我都会运行发送方功能,另一方面,接收方会根据第一个发送方来解码发送的字符串字符(例如,@表示字符串是设置)。

The problem is, after the first time both host and client sent something to one another, they can't seem to send again. 问题是,在主机和客户端第一次相互发送东西之后,它们似乎无法再次发送。 No error, no message, no nothing. 没有错误,没有消息,什么都没有。 Just won't send. 只是不会发送。 Is there something wrong with the sendingFunction? sendFunction是否有问题? Or perhaps the delegate something? 还是代表什么? I don't know. 我不知道。 Some advice would be appreciated, guys. 伙计们,一些建议将不胜感激。 And thanks in advance. 并预先感谢。

You're never calling BeginReceive again. 您再也不会调用BeginReceive The typical practice in async socket programming is to process the received data, then call BeginReceive again so that you can then process the next bit of data that comes in. 异步套接字编程中的典型做法是处理接收到的数据,然后再次调用BeginReceive ,以便您可以随后处理BeginReceive的下一数据位。

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

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