sock = listener.AcceptSocket();

V

vitagoni

I'm doing the next.

public void CloseListener()
{
listener.Stop();

listen.Abort();
listen.Join();

if (sock != null)
{
try { sock.Close(); }
catch (Exception) { }
}

return;
}

public bool Listen(int port)
{
listener = new TcpListener(port);

if (listener == null) return false;

listener.Start();

listen = new Thread(new ThreadStart);

if (listen != null)
{
listen.Start(mess);
return true;
}
return false;
}

public void Listener()
{

try
{
sock = listener.AcceptSocket();
}
catch (Exception ex)
{
throw(ex);
}

......
It is called from main form, which controls all that..

But when I close the main form and invoke CloseListener then application
hangs on at sock = listener.AcceptSocket();

Tell me how to stop listener from outside of the working thread..
 
V

vitagoni

I found the only one:

if (listener.Pending())
{
sock = listener.AcceptSocket();

and tra-ta-ta-tata

Is it good?
 
V

vitagoni

Hello Peter,

Thanks for your "politeness", sorry I'm Russian and it's quite different
culture here, at least I can write in English, hope it would be polite enough.

As for your example..
It's good in general, unfortunately, I don't want to paste the whole code
from my project.. but the general idea is : guess AcceptSocket() is working
just like unmanaged code, thus managed part is unable to stop it once it is
called.

I think that the good idea is to use listener.Pending() (boolean return),
and once it's true - just call AcceptSocket()...






Peter Duniho said:
[...]
But when I close the main form and invoke CloseListener then application
hangs on at sock = listener.AcceptSocket();

Tell me how to stop listener from outside of the working thread..

General point of manners: in the English language, it's considered polite
to precede an imperative statement like "tell me how..." with the word
"please". Otherwise, you sound like you are giving a command.

As far as the question itself goes...

Without a concise-but-complete code sample that reliably demonstrates what
you're doing and why it's not working for you, it's impossible to say.
There are at least a couple of things wrong with the code you posted
though: first, you abort the "listen" thread instead of just letting it
exit normally; second, you re-throw the exception in the "Listener()"
method.

It's also pointless to check the return value from "new TcpListener(port)"
for null; it will never be null.

But, all that said, the basic _idea_ of the code you posted is fine. So
if it's not working for you, there's something going on in code you
haven't shared.

See below for a concise-but-complete code sample that demonstrates the
general technique in action. If that's not enough to get you pointed back
in the right direction, you're going to have to post a better, more
complete question.

Pete


using System;
using System.Threading;
using System.Net.Sockets;
using System.Net;

namespace TestCloseTcpListener
{
class Program
{
static void Main(string[] args)
{
TcpListener listener = null;
AutoResetEvent are = new AutoResetEvent(false);

Thread thread = new Thread(delegate()
{
try
{
listener = new TcpListener(IPAddress.Any, 0);

listener.Start();
are.Set();
listener.AcceptSocket();
}
catch (Exception exc)
{
Console.WriteLine("listener exception: \"" +
exc.Message + "\"");
}
});

thread.Start();

// Don't bother stopping the listener until we know it's
started
are.WaitOne();

// just make sure the other thread's had enough time to get
// to the AcceptTcpClient() call. A full second should be
// _plenty_ of time.
Thread.Sleep(1000);

// Stop the TcpListener and wait for the thread to exit.
listener.Stop();
thread.Join();

Console.WriteLine("done");
Console.ReadLine();
}
}
}
 
V

vitagoni

As I can see the are.Set(); is the solution to set the thread ready to be
interrupted, right?
Without "are" it hangs on. Does it mean that AcceptSocket creates its own
thread which inherits AutoResetEvent property from calling thread?

Don't understand, sorry.
 
V

vitagoni

For example, in a GUI application, you might not even enable the part of
the UI (a button, for example) that allows the user to stop the
TcpListener until the TcpListener thread reports back to the UI (via an
event, for example) that it's called Start().

Seems like I have to place a button to stop the listener, otherwise on
Form_Closing event my application is hanging on, closes its window, but the
program still in the list of Task Man processes waiting for something I can't
even trace on.
 
V

vitagoni

here is my sample code:

public void ListenerA(object o)
{
Form1.MyShow mess = (Form1.MyShow)o;
byte[] buffer = new byte[5000];
are.Set();
string request = "";
try
{
listener.Start();
}
catch (Exception e) { mess.Invoke(ee.ToString()); }

while (true)
{
if (listener.Pending())
{
listenOn = false;
try
{
sock = listener.AcceptSocket();
listenOn = true;
}
catch (Exception e)
{
if (!closerequested)
mess.Invoke(e.ToString());
}
while (listenOn)
{
try
{
int numOfbytes = sock.Receive(buffer);
if (numOfbytes > 0)
{
for (int i = 0; i < numOfbytes; i++) {
request += (char)buffer; }
byte[] reply = mess.Invoke(result);
request = "";
sock.Send(reply);
}
}
catch (Exception e)
{
listenOn = false;
mess.Invoke(e.ToString());
}

}
sock.Close();
sock = null;
}
}
return;
}


and on Form_Closing I call the thread abortion.
and before listener.Stop() of course.

it comes to unhandled exception when at least once it's been connected

and the error happens on MyForm.Dispose()
 
V

vitagoni

Also, as I said, you should not be using TcpListener.Pending(). It's a
waste of CPU time, and adds nothing to your program.
Disagreed.

No doubt there are lots of other things wrong with the code, but without a
concise-but-complete code sample, there's no way to comment.

Once you said "there are lots of other things" - then be so "polite" to list
them.. otherwise that phrase is just a "soap bubble", sorry.
 
V

vitagoni

When I use Join() - it hangs on without any chances..((

I need to find out how to pass to main form exception messages.
I use delegate from main form, which is just display the message in textbox.
But what happens: when I close my Form the ClosingForm even is intercepted,
and an exception happens on Thread.Abort, and that listener thread calls
that delegate on main form, and I suppose here is my problem.
 
B

Ben Voigt [C++ MVP]

vitagoni said:
Disagreed.

Peter is right. You should use:
- A manual reset event to request shutdown
- BeginAccept to asynchronously perform the accept

WaitAny to let the program continue when either of the two above conditions
are met, and tell the program which. Then if there was a shutdown request
you can cancel the accept and end the thread.
Once you said "there are lots of other things" - then be so "polite"
to list them.. otherwise that phrase is just a "soap bubble", sorry.

The code doesn't compile, as written. It needs considerable context that
was missing from your post.
 
V

vitagoni

You right, I'm not experienced - that's why I'm here, isn't it looking
surprisingly?))

I need time to publish the code.. to simulate the error.. will do morrow.

In fact, I fixed the problem by getting rid of Invokes on exception.

but anyway, I will place the code...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top