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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:11:55+00:00 2026-05-22T20:11:55+00:00

I’m working on a general server and a client programs. The problem I’m facing

  • 0

I’m working on a general server and a client programs. The problem I’m facing is when I’m in the OnDataReceived in my client and server I don’t know what to do with the data. Ideally it should output the received data into a window but I don’t know if it will be a Form or Console application. So the question is how do I create a general method that can handle both or if that is not possible what should I do instead?

The code I’m working with:

            SocketPacket theSockId = (SocketPacket)asyn.AsyncState;

            int iRx = theSockId.m_currentSocket.EndReceive(asyn);
            char[] chars = new char[iRx + 1];
            Decoder decode = Encoding.Default.GetDecoder();
            int charLength = decode.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
            String szData = new String(chars);

            //Handle Message here

            WaitForData();

And the socket packet class:

    class SocketPacket
    {
     public Socket m_currentSocket;
     public byte[] dataBuffer = new byte[1024];//Buffer to store the data by the client

     public SocketPacket(Socket socket)
     {
        m_currentSocket = socket;
     }
    }

PS.
I don’t know if needs to be known but I’m doing asynchronous Client/Server.

  • 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-22T20:11:55+00:00Added an answer on May 22, 2026 at 8:11 pm

    Your communication classes shouldn’t care what happens to the data they receive. Instead, they should either make the data available to a class that wants it. One way to do so would be to provide a getData() method, which received data and then returned it to the caller. Even better would be to provide a DataArrived event, which was fired whenever you received data. That way, any number of consumers could listen for data, but your communication code doesn`t have to know which classes are listening or what they plan to do with the data.

    EDIT:

    A simple example:

    public class MyClassWithEvent
    {
        public delegate void DataArrivedDelegate(string data);
        public event DataArrivedDelegate DataArrived;
    
        public void GetSomeData()
        {
            // Communication code goes here; stringData has the data
    
            DataArrivedDelegate handler = DataArrived;
            if (handler != null)
            {
                // If you want to raise the event on this thread, this is fine
                handler(stringData);
            }
        }
    }
    

    In your listener class:

    public MyListener
    {
    
        public MyListener(MyServer server)
        {
            // Sets MyListenerMethod to be called when DataArrived is raised
            server.DataArrived += MyListenerMethod;
        }
    
        public void MyListenerMethod(string data)
        {
            // Do something with the data
            Console.WriteLine(data);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.