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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:39:00+00:00 2026-06-12T06:39:00+00:00

I have sent data as byte using TcpClient and I wanted to send my

  • 0

I have sent data as byte using TcpClient and I wanted to send my own class instead bytes of data.

By bytes of data, what I meant is that I am sending the data converted into bytes like this:

using (MemoryStream bufferStream = new MemoryStream())
{
    using (BinaryWriter bufferData = new BinaryWriter(bufferStream))
    {
        // Simple PONG Action
        bufferData.Write((byte)10);
    }

    _logger.Info("Received PING request, Sending PONG");
    return bufferStream.ToArray();
}

And instead I would like to send it like this, without having to declare its size or w/e

public class MyCommunicationData
{
    public ActionType Action { get; set; }
    public Profile User { get; set; }
    ...
}

Normally, when I send my data as bytes the first 5 bytes I use to indicate the action and the message size.

But if I migrate to serialize all the data as a single class, do I still need to send what action and size it is or using serialized messages the client and server would know what to read etc or is there a way to do so I can send it without having to specify things out of the serialization object ?

Not sure if this matters here, I am using AsyncCallback to read and write to the network stream:

_networkStream = _client.tcpClient.GetStream();
_callbackRead = new AsyncCallback(_OnReadComplete);
_callbackWrite = new AsyncCallback(_OnWriteComplete);

Let me know if you need me to post any other functions.

  • 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-12T06:39:01+00:00Added an answer on June 12, 2026 at 6:39 am

    If you use a text based serializer(for ex, Json), you can utilize StreamReader’s ReadLine and StreamWriter’s WriteLine (created from tcpClient.GetStream).

    Your code would be something like

     writer.WriteLine(JsonConvert.SerializeObject(commData))
    

    and to get the data on the other end

    var myobj = JsonConvert.DeserializeObject<MyCommunicationData>(reader.ReadLine())
    

    –EDIT–

    //**Server**
    Task.Factory.StartNew(() =>
    {
        var reader = new StreamReader(tcpClient.GetStream());
        var writer = new StreamReader(tcpClient.GetStream());
        while (true)
        {
            var myobj = JsonConvert.DeserializeObject<MyCommunicationData>(reader.ReadLine());
            //do work with obj 
            //write response to client
            writer.WriteLine(JsonConvert.SerializeObject(commData));
        }
    }, 
    TaskCreationOptions.LongRunning);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that uses sockets to send and receive data asynchronously over
I have a form that is capturing data sent from a google LocalSearch API.
i have been sent a directory tree of source code that i want to
I have a file that is sent over https and I am handling with
I have a unit test that fails because headers are already sent. However, the
I have a linux C program that handles request sent to a TCP socket
I have a web service that requires special headers to be sent in the
I have developed a small server that recives connections (using basically the example here:
i am using java library rxtxx to send data to micro controller the application
I have a simple form that uploads an image to a database. Using a

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.