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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:16:48+00:00 2026-05-24T23:16:48+00:00

I have a server that streams data out on a TCP port once you

  • 0

I have a server that streams data out on a TCP port once you connect to it. You can see this if you telnet to the TCP port and data starts flowing.

I am looking for an example that will let me connect to an IP address and port and receive the data in a stream in the application.

All examples I can find are with a client-server model with the client sending (which is not what I need) and the server binding to a port on itself and receiving.

I need this to be asynchronous and believe this can be done, but I could do with a good leg up!

  • 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-24T23:16:48+00:00Added an answer on May 24, 2026 at 11:16 pm

    If you are using a TcpClient, using GetStream for getting the NetworkStream and then use the stream for reading the data sent by the server. You can always use the stream asynchronously, using BeginRead.

    If you are using a plain old Socket, you can use BeginReceive to receive asynchronously once connected.

    Either way, as soon as you have a NetworkStream or a Socket object that is bound and connected, there is no difference between the server or the client. You can use the server example of reading in the client code, most often with no (or little) modification.

    For an example:

    byte[] readBuffer = new byte[1000]; 
    
    {
        TcpClient tcpClient = new TcpClient(serverHost, serverPort);
        NetworkStream stream = tcpClient.GetStream();
    
        stream.BeginRead(readBuffer, 0, 1000, readHandler, tcpClient);
    }
    
    ...
    
    byte[] tempBuff = new byte[1000];
    int tempBuffSize = 0;
    
    // This is a rough example of reading and handling data
    // that is delimited by \r\n. This code most probably is
    // missing many edge case handling, so use it only as a starting
    // point
    void readHandler(IAsyncResult result)
    {
        TcpClient tcpClient = (TcpClient)result.AsyncState;
        int dataLen = tcpClient.GetStream().EndRead();
    
        int currStart = 0;
        int currEnd = -1;
    
        for (int i = 0; i < dataLen; i++)
        {
            if (readBuffer[i] == '\r' && i < (readBuffer.Length - 1) &&
                readBuffer[i + 1] == '\n')
            {
                // Set the end of the data
                currEnd = i - 1;
    
                // If we have left overs from previous runs:
                if (tempBuffSize != 0)
                {
                    // Allocate enough space for the joined buffer
                    byte[] joinedData = new byte[tempBuffSize + (currEnd - currStart + 1)];
    
                    // Get the leftover from the previous read
                    Array.Copy(tempBuff, 0, joinedData, 0, tempBuffSize);
    
                    // And add the current read as well
                    Array.Copy(readBuffer, currStart, joinedData, tempBuffSize, (currEnd - currStart + 1));
    
                    // Now handle it
                    HandleData(joinedData, 0, joinedData.Length);
    
                    // Mark that we don't have any leftovers anymore
                    tempBuffSize = 0;
                }
                else
                {               
                    // Handle the data, from the start to the end, between delimiter
                    HandleData(readBuffer, currStart, currEnd);
                }
    
                // Set the new start - after our delimiter
                currStart = i + 2;
            }
        }
    
        // See if we still have any leftovers
        if (currStart < dataLen)
        {
            Array.Copy(readBuffer, currStart, tempBuff, 0, dataLen - currStart);
            tempBuffSize = dataLen - currStart;
        }
    
        // Set the asynchronous read again
        stream.BeginRead(readBuffer, 0, 1000, readHandler, tcpClient);
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a server that sends data via a socket, the data is a
I have a server that is running Ubuntu Linux Server Edition. I once had
I have a server that connects to multiple clients using TCP/IP connections, using C
I have a server that hosts my Subversion code base. That server is currently
I have a server that listens for a connection on a socket: public class
I have a server that has several virtual machines running on it. I'm trying
I have a server that I have no control over, it's JSON based and
Let me try to explain what I need. I have a server that is
I have a php server that is running my domain name. For testing purposes
I have a server application that receives information over a network and processes it.

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.