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

  • Home
  • SEARCH
  • 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 6758885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:51:30+00:00 2026-05-26T13:51:30+00:00

I’ve developing (or trying to, anyway) a program that uses Asynchronous Socket to, supposedly,

  • 0

I’ve developing (or trying to, anyway) a program that uses Asynchronous Socket to, supposedly, pass strings to and fro the server and client, at any time.

This program requires no more than one client be connected to a server. I tried Socket Programming, but I found out it blocks the program until either one receives something.

Since I have only a basic understanding of Asynchronous socket programming, I just went for the simplest one I could find, or at least, the simplest one I could understand.

Here’s my code for the Server:

    public Socket g_server_conn;
    public byte[] g_bmsg;
    public bool check = false;  

    private void net_As_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(net_As_Receive), g_server_conn);
    }

    private void net_As_Send(IAsyncResult iar)
    {
        Socket server_conn = (Socket)iar.AsyncState;
        server_conn.EndSend(iar);
    }

    private void net_As_Receive(IAsyncResult iar)
    {
        try
        {
            Socket server_conn = (Socket)iar.AsyncState;
            server_conn.EndReceive(iar);
            if (g_bmsg.Length != 0)
            {
                net_Data_Receive(Encoding.ASCII.GetString(g_bmsg, 0, g_bmsg.Length));
                check = false;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "GG");
        }
    }

    public void net_Data_Send(string msg2snd) // Function for sending through socket
    {
        MessageBox.Show(msg2snd);
        byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg2snd);
        g_server_conn.BeginSend(byData, 0, byData.Length, SocketFlags.None, new AsyncCallback(net_As_Send), g_server_conn);
        g_server_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(net_As_Receive), g_server_conn);
    }

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

        if (txt.StartsWith("&"))
        {
            // Do something              
        }
    }

And here’s my code for the Client:

    private void net_As_Connect(IAsyncResult iar)
    {
        try
        {
                Socket client_conn = (Socket)iar.AsyncState;
                client_conn.EndConnect(iar);
                g_bmsg = new byte[1024];
                check = true;
                string toSendData = "&" + net_Name;
                net_Data_Send(toSendData);
                g_client_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(net_As_Receive), g_client_conn);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "GG");
        }
    }

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

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

    public void net_Data_Send(string msg2snd)
    {
        byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg2snd);

        g_client_conn.BeginSend(byData, 0, byData.Length, SocketFlags.None, new AsyncCallback(net_As_Send), g_client_conn);
        g_client_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(net_As_Receive), g_client_conn);
    }

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

        if (txt.StartsWith("&"))
        {
            // Do Something              
        }
        else if (txt.StartsWith("$"))
        {
            // Do something Else
        }

    }

Now, the Client can connect to the Server fine. The Client can even send in a string containing the user’s name to the Server, which will then be displayed on the Server. The Server then sends out the name of its user to the Client, which the client receives and displays. Whatever is sent is stored in a Label (lblBuffer)

But afterwards, say I have the following code:

    private void btnSendData_Click(object sender, EventArgs e)
    {
        string posMov = "Stuff to send";
        net_Data_Send(posMov);
    }

The Client receives nothing. Putting a Message Box in net_Data_Send(msg2snd) function reveals that the server does in fact send out the message. In fact, putting in the Message Box in that function makes it work (the Client receives it), for reasons I don’t know. Since I haven’t tried sending a message from the Client to the Server (other than the name when the Client Connects), I assume the Client will have the same problem sending to the Server.

Also, when it does send the second message (by putting a Message Box in the net_Data_Send function), only parts of the Label (lblBuffer) are overwritten. So if I my name is “Anon E. Moose”, and the Server sends that when the Client connects, and I try to send out, say, “0.0” (via button press) the Label on the Client would then read “0.0n E. Moose”.

What did I do wrong? Can I have some help on this, please?

Perhaps I have a problem with net_Data_Receive and net_Data_Send?

  • 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-26T13:51:31+00:00Added an answer on May 26, 2026 at 1:51 pm

    I think you need to call BeginReceive on your client again, it looks like you are only calling it once, so after it has received the server name, it isn’t listening for any more data from the server

    private void net_As_Receive(IAsyncResult iar)
    {
        var bytesRead = g_client_conn.EndReceive(iar);
    
    
        if (bytesRead != 0)
        {
            net_Data_Receive(Encoding.ASCII.GetString(g_bmsg, 0, bytesRead));
            check = false;
        }
    
        g_client_conn.BeginReceive(g_bmsg, 0, g_bmsg.Length, SocketFlags.None, new AsyncCallback(net_As_Receive), g_client_conn);
    }
    

    also, as I mentioned in my comment, use the bytesRead value to work out how much of the buffer you need to use.

    You will need to work out if the data you have received from the socket is the full amount, or if you need to read more data to make up the current message from the other side.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.