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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:26:31+00:00 2026-06-10T20:26:31+00:00

I have a proxy server I’m writing in C#. I also have a Java

  • 0

I have a proxy server I’m writing in C#. I also have a Java applet that receives MJPEG data from a video server through this C# proxy. My problem is that when no more MJPEG data is currently available from the video server, the proxy is stuck in a blocking read call and I cannot terminate it.

// write the forwarded output
// blocking on remoteServerResponseStream.Read
while (m_running && (read = remoteServerResponseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    bytesRead += read;

    output.Write(buffer, 0, read);

    output.Flush();
}

This should be terminated by the Java applet closing the stream (variable output in code above). However, the Java applet cannot close this connection because the proxy never acknowledges the close request while it is stuck in remoteServerResponseStream.Read waiting for data.

I have been stuck on this problem for a week. I think I might have thought of a solution but I’m not sure if it will work. I’d love to hear any feedback on this.

My thought was to have the removeServerResponseStream.Read on another thread and use a shared Queue to transfer data. The thread would read data and place it on the queue. My main thread would then forward any data available in the queue to output. This way, I can constantly check if output.CanWrite ever becomes false, in which case I can abort the read thread (this is the only way I know of to interrupt a blocking stream read). Is this a viable solution? If so, should I constantly poll the queue for data available or should I create an event? I’d love to hear any thoughts on this problem! Thanks in advance.

  • 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-10T20:26:32+00:00Added an answer on June 10, 2026 at 8:26 pm

    In these situations I just end the stream reader from another thread. When you close the reader it will break the loop. Going through extra synchronized queues is just more boilerplate to do unnecessarily. Eventually somewhere you have to stop the blocking reader. Putting values in a queue only stops your consumer from being blocked, but not your reader.

    Here is an example with a socket blocking read. I will be blocking on reading 1 byte which I’ll intentionally never send. A couple seconds later I’ll dispose of the socket, the socket will bail out of its blocking read and the app will exit gracefully. I’ll log what each thread is doing and when it happens (the number prefixing each log line is the thread id and I’ll indent separate threads).

    In your class that uses the blocking reader you should implement the Disposable pattern and close/dispose of your reader there.

    static void Main(string[] args)
    {
        SocketTest();
    
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
    
    public static void SocketTest()
    {
        int port = 22345;
    
        var tcpListener = new TcpListener(IPAddress.Any, port);
    
        tcpListener.Start();
    
        // Listening thread
        new Thread(() =>
        {
    
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Waiting for connection to port");
    
            var socket = tcpListener.AcceptSocket();
    
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Connection accepted");
    
            var stream = new NetworkStream(socket);
            var reader = new BinaryReader(stream);
    
            try
            {
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Starting blocking read");
                var bytes = reader.ReadBytes(1);
                Console.WriteLine(Thread.CurrentThread.ManagedThreadId + " - Done blocking read, read {0} bytes", bytes.Length);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error reading " + ex);
            }
        }).Start();
    
        // connecting thread
        new Thread(() =>
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    
            Console.WriteLine("\t" + Thread.CurrentThread.ManagedThreadId + " - Connecting to local port");
    
            socket.Connect("127.0.0.1", port);
    
            Console.WriteLine("\t" + Thread.CurrentThread.ManagedThreadId + " - Connecting to local succeeded");
    
            Thread.Sleep(TimeSpan.FromSeconds(2));
    
            Console.WriteLine("\t" + Thread.CurrentThread.ManagedThreadId + " - Disposing of socket");
    
            socket.Dispose();
    
        }).Start();
    
        Thread.Sleep(TimeSpan.FromSeconds(5));
    }
    

    When you run this you get:

    3 - Waiting for connection to port
            4 - Connecting to local port
            4 - Connecting to local succeeded
    3 - Connection accepted
    3 - Starting blocking read
            4 - Disposing of socket
    3 - Done blocking read, read 0 bytes
    Press any key to exit
    

    You kind of answered the question yourself here though:

    My thought was to have the removeServerResponseStream.Read on another thread

    Which is exactly what you should do. When you know your app has closed close the socket from the active thread. This free’s the blocked thread and you can gracefully end.

    The pattern here is you usually spin up a thread for a specific socket and you maintain one active thread that is a kind of “controller” thread for requests.

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

Sidebar

Related Questions

I'm sending data from client sockets through a proxy server to an http server.
I notice that when http requests are made from clients through a proxy server,
I have to connect to internet through proxy server using php and java..... As
We have several cron jobs that ftp proxy logs to a centralized server. These
I have something like a proxy server (written in java) running between my clients
I have to write an application that is essentially a proxy server to handle
I have an ssl/tls server (nodejs) that acts as a proxy to postfix/sendmail to
I am writing a HTTP proxy server and I noticed that many clients use
I have a proxy class, that receives a request and send the request to
I need web access from Gradle through a proxy server to use the Gradle/Artifactory

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.