Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:08:22+00:00 2026-05-24T01:08:22+00:00

So, I have a board game that uses Asynchronous socket to operate over LAN.

  • 0

So, I have a board game that uses Asynchronous socket to operate over 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? Or perhaps the delegate something? I don’t know. Some advice would be appreciated, guys. And thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:08:22+00:00Added an answer on May 24, 2026 at 1:08 am

    You’re never calling BeginReceive again. 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have simple program that emulates a board game with a number of
I've been programming a software version of a board game. Thus far I have
I am working on a game that will have a two dimensional board of
I have an image view that I draw my game board in for my
I'm implementing a card game. In this game I have a Board that is
I have to represent a board game using lisp. To do that i have
I have a board game app that creates a lot (hundreds) of bitmaps, and
I have a game that's based on a 25x20 HTML table (the game board).
I have some Linq code, that works fine. It retrieves a list of board
I'm new to microcontroller programming and I have interfaced my microcontroller board to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.