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 7955113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:38:16+00:00 2026-06-04T03:38:16+00:00

I had a code (lets suppose a simple chat application), I used Tcp with

  • 0

I had a code (lets suppose a simple chat application), I used Tcp with sync. but since Async way is better at all although it is harder to setup, I decided to change the way from sync to async. but now I am facing some problems using async.

  1. Buffer size: I know that we need to consider a limited size of bytes to get the bytes from peer. I read many articles but my problem not solved, I have a very simple but worthless approach to cope with this, putting a character at the end of each message; then the receiver can understand when message is fully received. but I have another problem which prevents me from performing this approach further to another problems it has for me.

Let me explain my problem by the following code:

public void Send(byte[] message, Socket connection)
{
    connection.BeginSend(message, 
                         0, 
                         message.Length, 
                         SocketFlags.None, 
                         new AsyncCallback(OnSend), 
                         connection);
}

and on OnSend method, I just use connection.EndSend(result) where result is IAsyncResult. but when I then call Send method twice like the following code, OnReceive call back will receive both of them as a one single message.

Send(Encoding.Unicode.GetBytes("Hello"));
Send(Encoding.Unicode.GetBytes("Bye"));

my OnReceiveMethod will use Console.WriteLine(message); where message is the string received from peer. and the output using sync way must be

Hello

Bye

but in async way I receive it as a one message not two, so it is printed

HelloBye

Sincerely yours,
Peyman Mortazavi

  • 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-06-04T03:38:18+00:00Added an answer on June 4, 2026 at 3:38 am

    With the synchronous call, you are guaranteeing that the first message is completely sent before you send the second one. With the asynchronous methods, however, you are just adding the two messages to the outgoing queue, waiting to be sent. If you happen to hit it at the right time, it may send them as two separate messages, but since you add them both so fast, it’s more than likely going to send both together. If you think of TCP/IP socket communication as a single ongoing stream of data, you’ll be much better off. You need to format your messages in a way where you can determine where they begin and end. When you receive data through the socket, you should keep adding the received messages to a buffer until you’ve determined (based of the formatting of the data) that you have received a complete message. You should not be relying on each read from the socket to be a single complete message. You should expect that it may only be a partial message or even multiple messages.

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

Sidebar

Related Questions

Suppose you had code like this: _READERS = None _WRITERS = None def Init(num_readers,
First, apologies,this should be simple but I've had too much coffee and cannot wrap
lets say I had a file(.csh) File Contents Script_1 { code of script1 }
Let's say I had: protected void performLogic(List<Object> docs) { ... } In the code
I had code that worked fine when running in the context of the main
I had some code that ran commands through Runtime.getRuntime.exec(String) , and it worked on
I had a code segment on an asp.net page in the code-behind file. I
I had this code: int foo(void){ return 1; } int main(void){ static const int
I was looking at an authentication system which had this code: # prevents a
Recently I attended a lecture concerning some design patterns: The following code had been

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.