Click here to Skip to main content
15,890,282 members
Home / Discussions / C#
   

C#

 
GeneralRe: better way to make function returns Pin
netJP12L20-Nov-09 8:34
netJP12L20-Nov-09 8:34 
AnswerRe: better way to make function returns Pin
dojohansen23-Nov-09 10:59
dojohansen23-Nov-09 10:59 
QuestionStream closes while reading a socket Pin
joana.simoes20-Nov-09 5:11
joana.simoes20-Nov-09 5:11 
AnswerRe: Stream closes while reading a socket Pin
Paulo Zemek20-Nov-09 5:57
mvaPaulo Zemek20-Nov-09 5:57 
GeneralRe: Stream closes while reading a socket Pin
joana.simoes20-Nov-09 6:19
joana.simoes20-Nov-09 6:19 
GeneralRe: Stream closes while reading a socket Pin
Paulo Zemek22-Nov-09 7:44
mvaPaulo Zemek22-Nov-09 7:44 
AnswerRe: Stream closes while reading a socket Pin
Luc Pattyn20-Nov-09 6:27
sitebuilderLuc Pattyn20-Nov-09 6:27 
GeneralRe: Stream closes while reading a socket Pin
joana.simoes23-Nov-09 0:13
joana.simoes23-Nov-09 0:13 
Thanks a lot for the reply!
I refactored the code in order to wrapp my listener inside a class, which I now call insde a thread. My entry point for the proxy class, now looks like this:
m_listener.Start();
System.Diagnostics.Debug.WriteLine("Listening socket on port " + m_port);

ThreadedListener tl=new ThreadedListener(m_listener, m_service);
Thread newThread = new Thread(new ThreadStart(tl.Process));
newThread.Start();

Console.WriteLine("(0): Creathed Thread (ID {1})",
    Thread.CurrentThread.ManagedThreadId, newThread.ManagedThreadId);


And this is the ThreadListener class; the method activated by the thread (process) is introducing an asynchronous callback to process the client request;

internal class ThreadedListener
{
    private HttpListener m_listener;
    private SisService m_service;
    const int BUFFER_SIZE = 1024;

    public ThreadedListener(HttpListener listener, SisService service) { m_listener = listener; m_service = service; }

    public void Process()
    {
        HttpListenerContext request=m_listener.GetContext();
        IAsyncResult result = m_listener.BeginGetContext(new AsyncCallback(RequestHandler), m_listener);
    }

    private void RequestHandler(IAsyncResult ar)
    {
        HttpListenerContext httpContext = null;
        try
        {
            HttpListener listener = (HttpListener)ar.AsyncState;
            httpContext = listener.EndGetContext(ar);

            // start listening for next request
            listener.BeginGetContext(RequestHandler, listener);

            // invoke response handler
            ResponseHandler(httpContext);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            if (httpContext != null)
            {
                httpContext.Response.Close();
            }
        }
    }

    private void ResponseHandler(HttpListenerContext context)
    {
         //do Stuff;
    }
 }


And guess what? I still get the same (annoying) exception at the same circunstances:

A first chance exception of type 'System.Net.HttpListenerException' occurred in System.dll
An operation was attempted on a nonexistent network connection


I did also a small change, to take the proxy class outside the thread; It was previously like this in my main method:

System.Threading.Thread proxythread = new System.Threading.Thread(new ThreadStart(m_prx.Start));
proxythread.Start();


And is now like this:
m_prx = new ProxyServer(this);
m_prx.Start();

(I thought we dont need another thread here; but Im not sure this has any influence; probably not)
Any ideas?
Thanks again for helping me with this,
Jo
GeneralRe: Stream closes while reading a socket Pin
joana.simoes23-Nov-09 0:39
joana.simoes23-Nov-09 0:39 
GeneralRe: Stream closes while reading a socket Pin
Luc Pattyn23-Nov-09 0:43
sitebuilderLuc Pattyn23-Nov-09 0:43 
GeneralRe: Stream closes while reading a socket Pin
joana.simoes23-Nov-09 3:56
joana.simoes23-Nov-09 3:56 
NewsRe: Stream closes while reading a socket Pin
joana.simoes23-Nov-09 6:43
joana.simoes23-Nov-09 6:43 
AnswerRe: Stream closes while reading a socket Pin
dojohansen23-Nov-09 11:15
dojohansen23-Nov-09 11:15 
NewsRe: Stream closes while reading a socket Pin
joana.simoes24-Nov-09 1:37
joana.simoes24-Nov-09 1:37 
QuestionExcel Formatting. Pin
FEMDEV20-Nov-09 4:47
FEMDEV20-Nov-09 4:47 
AnswerRe: Excel Formatting. Pin
toby3120-Nov-09 8:16
toby3120-Nov-09 8:16 
GeneralRe: Excel Formatting. Pin
FEMDEV22-Nov-09 22:44
FEMDEV22-Nov-09 22:44 
QuestionSQLite Pin
jashimu20-Nov-09 3:26
jashimu20-Nov-09 3:26 
AnswerRe: SQLite Pin
MeLight20-Nov-09 3:35
MeLight20-Nov-09 3:35 
GeneralRe: SQLite Pin
jashimu20-Nov-09 4:44
jashimu20-Nov-09 4:44 
GeneralRe: SQLite Pin
Pete O'Hanlon20-Nov-09 5:04
mvePete O'Hanlon20-Nov-09 5:04 
GeneralRe: SQLite Pin
PIEBALDconsult20-Nov-09 8:04
mvePIEBALDconsult20-Nov-09 8:04 
GeneralRe: SQLite Pin
jashimu20-Nov-09 8:09
jashimu20-Nov-09 8:09 
AnswerRe: SQLite Pin
PIEBALDconsult20-Nov-09 3:35
mvePIEBALDconsult20-Nov-09 3:35 
AnswerRe: SQLite Pin
Md. Marufuzzaman20-Nov-09 4:34
professionalMd. Marufuzzaman20-Nov-09 4:34 

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.