Click here to Skip to main content
15,889,462 members
Home / Discussions / C#
   

C#

 
GeneralRe: Setting DirectShow Filter properties for non DMO filter in code (no GUI). Pin
RobstaHendricks15-Nov-09 22:45
RobstaHendricks15-Nov-09 22:45 
QuestionHow to get painted image from picturebox [modified] Pin
Vishwanath Patil12-Nov-09 0:41
Vishwanath Patil12-Nov-09 0:41 
AnswerMessage Closed Pin
12-Nov-09 2:25
stancrm12-Nov-09 2:25 
GeneralRe: How to get painted image from picturebox Pin
Vishwanath Patil12-Nov-09 2:37
Vishwanath Patil12-Nov-09 2:37 
AnswerRe: How to get painted image from picturebox Pin
Dave Kreskowiak12-Nov-09 4:23
mveDave Kreskowiak12-Nov-09 4:23 
GeneralRe: How to get painted image from picturebox Pin
Vishwanath Patil12-Nov-09 18:04
Vishwanath Patil12-Nov-09 18:04 
GeneralRe: How to get painted image from picturebox Pin
Dave Kreskowiak13-Nov-09 15:15
mveDave Kreskowiak13-Nov-09 15:15 
QuestionThreaded Proxy? Pin
joana.simoes12-Nov-09 0:40
joana.simoes12-Nov-09 0:40 
Hi,
Thanks for reading this!
For the people who never read any of my posts I am trying to write a proxy server in c#.
I am trying to "keep it simple" and stay away from asynchronous requests, but is not possible to totally avoid threading and I think I am encountering some multi threading problems.

The idea is to have a listener socket, binded to a certain prefix. Whatever requests I get there, I append them the correct credentials and send them straight to the (atuthenticated) server on the other side; then I grabb the bytes of the response and show them in the browser. For this I am using the HttpListenerContext class.

public void Start()
{
    try
    {
        m_listener.Start();
        while (true)
        {
            try
            {
                HttpListenerContext request = m_listener.GetContext();
                ThreadPool.QueueUserWorkItem(ProcessRequest, request);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
        }
    }
    catch (Exception e)
    {
        System.Diagnostics.Debug.WriteLine(e.ToString());
    }
    System.Diagnostics.Debug.WriteLine("Listening socket on port " + m_port);
}


The function ProcessRequest does the actual job that I wanted. (note that it does it in an asynchronous way!)

private void ProcessRequest(object listenerContext)
{
    try
    {
        var context = (HttpListenerContext)listenerContext;

        try
        {
            // READ ADDRESS HERE...
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

            Stream instream = context.Request.InputStream;
            Stream outstream = response.GetResponseStream();

            //TRANSFER DATA FROM REQUEST STREAM TO RESPONSE STREAM
            // Close streams
            instream.Close();
            outstream.Close();
            context.Response.OutputStream.Close();
        }
        catch (Exception ex)
        {

        }
    }
    catch
    {
        Restart();
    }
}


Normally, everything goes ok; however, whenever I have a client that does a lot of requests very quickly (almost parallely) there is a problem in line writing the output stream (with the bytes that I just grabbed from the server) and an error is thrown:

An operation was attempted on a nonexistent network connection

This is misterious; is the network connection referring to the incoming stream (the server that I am comunicating with)? if its there is no apparent problem, because I checked and the connection is still on... or is this connection referring to the output stream? if it is, there is also no apparent problem cause I checked it with the functon [i]CanWrite[/i] and it returns true, so it is looks in good health;
If anybody has any "light" to to bring into this question I would really appreciate it, cause I googled it to exhaustion and did not come to any conclusions Confused | :confused:

Anyway, moving on: I caught the error and figure out that I need to somehow close the streams that are opened; I did not find a clean easy way to do it (cause I cannot access the streams anymore), so the "brute-force" approach was to close the actual listener and start it again (so that I can again listen for connections);


private void Restart()
{
    Stop();
    Start();
}


Most of the times, this is ok, but sometimes, when I enter the [i]Start()[/i] function, there is a problem with this line:


HttpListenerContext request = m_listener.GetContext();



and an exception is thrown:

The I/O operation has been aborted because of either a thread exit or an application request


and then I am in a bit of a mess.
Apart from trying to solve this particular errors that cause my application to fail requests once in a while, I am looking at the bigger picture and wondering if the issues I am encountering are from not approaching properly the situation; am I having simultaneous access to the code and no handling properly the multithreading (I actually tried a lock() of the critical section without any improvement...) ? do I need multithreading here? or is it something else?
As I said, any help here would be much appreciated! Thanks in advance Thumbs Up | :thumbsup:
AnswerRe: Threaded Proxy? Pin
Covean12-Nov-09 2:08
Covean12-Nov-09 2:08 
GeneralRe: Threaded Proxy? Pin
Luc Pattyn12-Nov-09 2:23
sitebuilderLuc Pattyn12-Nov-09 2:23 
GeneralRe: Threaded Proxy? Pin
Covean12-Nov-09 2:47
Covean12-Nov-09 2:47 
AnswerRe: Threaded Proxy? Pin
Luc Pattyn12-Nov-09 2:22
sitebuilderLuc Pattyn12-Nov-09 2:22 
GeneralRe: Threaded Proxy? Pin
joana.simoes13-Nov-09 5:22
joana.simoes13-Nov-09 5:22 
QuestionPDF conversion Pin
Santhosh Sebastian Mattathil11-Nov-09 23:34
Santhosh Sebastian Mattathil11-Nov-09 23:34 
QuestionShowing non 96dpi jpegs in PictureBox Pin
Chesnokov Yuriy11-Nov-09 23:00
professionalChesnokov Yuriy11-Nov-09 23:00 
AnswerRe: Showing non 96dpi jpegs in PictureBox Pin
Eduard Keilholz11-Nov-09 23:03
Eduard Keilholz11-Nov-09 23:03 
QuestionLoading app.config from a remote location fails to load section handler. Pin
Larantz11-Nov-09 22:57
Larantz11-Nov-09 22:57 
AnswerRe: Loading app.config from a remote location fails to load section handler. Pin
Shameel11-Nov-09 23:38
professionalShameel11-Nov-09 23:38 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Larantz11-Nov-09 23:40
Larantz11-Nov-09 23:40 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Larantz11-Nov-09 23:50
Larantz11-Nov-09 23:50 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Shameel12-Nov-09 0:00
professionalShameel12-Nov-09 0:00 
GeneralRe: Loading app.config from a remote location fails to load section handler. Pin
Larantz12-Nov-09 0:11
Larantz12-Nov-09 0:11 
Questionadd row to data grid view problem Pin
Rinad11-Nov-09 22:51
Rinad11-Nov-09 22:51 
AnswerMessage Closed Pin
11-Nov-09 22:55
stancrm11-Nov-09 22:55 
GeneralRe: add row to data grid view problem Pin
Rinad11-Nov-09 23:31
Rinad11-Nov-09 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.