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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:06:13+00:00 2026-05-26T17:06:13+00:00

Ok I want to connect to a Socket and read a network stream using

  • 0

Ok I want to connect to a Socket and read a network stream using the System.Net.Sockets.NetworkStream class. This is what I have so far:

NetworkStream myNetworkStream;
Socket socket;

socket = new Socket(AddressFamily.InterNetwork, SocketType.Raw, 
    ProtocolType.IPv4);
socket.Connect(IPAddress.Parse("8.8.8.8"), 8888);

myNetworkStream = new NetworkStream(socket);

byte[] buffer = new byte[1024];
int offset = 0;
int count = 1024;

myNetworkStream.BeginRead(buffer, offset, count, ??, ??);

Now I need an AsyncCallback and an Object state to complete my BeginRead method but I’m not even sure if this is going to work. I’m a bit lost at this point! Where do I need to go from here?

  • 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-26T17:06:14+00:00Added an answer on May 26, 2026 at 5:06 pm

    Basically, when you call the Begin* method on an asynchronous operation, there needs to be a call to a corresponding End* statement (for more detailed information, see the Asynchronous Programming Overview on MSDN, specifically the section titled “Ending an Asynchronous Operation”).

    That said, you generally want to pass a method/anonymous method/lambda expression which will do one or two things:

    1) Call the corresponding End* method, in this case, Stream.EndRead. This call will not block when called because the callback will not be called until the operation is complete (note that if an exception occurred during the async call then this exception will be thrown when the End* method is called).

    2) Possibly start a new asynchronous call. In this case, it’s assumed you’ll want to read more data, so you should start a new call to Stream.BeginRead

    Assuming you want to do #2, you can do the following:

    // Declare the callback.  Need to do that so that
    // the closure captures it.
    AsyncCallback callback = null;
    
    // Assign the callback.
    callback = ar => { 
        // Call EndRead.
        int bytesRead = myNetworkStream.EndRead(ar);
    
        // Process the bytes here.
    
    
        // Determine if you want to read again.  If not, return.
        if (!notMoreToRead) return;
    
        // Read again.  This callback will be called again.
        myNetworkStream.BeginRead(buffer, offset, count, callback, null);
    };
    
    // Trigger the initial read.
    myNetworkStream.BeginRead(buffer, offset, count, callback, null);
    

    However, if you are using .NET 4.0, this becomes much easier using the FromAsync method on the TaskFactory class.

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

Sidebar

Related Questions

When using java.net.Socket.connect() , both a refused connection and a timeout result in a
I want to connect to DB using the iSeries Client Access driver. I use
I want to connect from home using SQL Server 2005 to another PC. I
I have a box built via html borders. I want to connect the bottom
How to connect to a server using basic http auth thru sockets in python
Sometimes when reading a socket from the TcpClient class , the stream comes in
I am using a Socket to receive data via TCP, and TextReader.ReadLine to read
I have a Solaris daemon written in Java6. Clients can connect to it using
Hi i have a client connecting to a socket server in php. I want
I want to connect to a Socket but I found 2 different connection methods:

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.