Sign in to follow this  
Shashwat Rohilla

Socket Programming

Recommended Posts

I'm using VS2008 C# .Net

I have created a socket [i]temp [/i]to accept any incoming connection request.

I have used
[i]Socket p = temp.Accept()

[/i]It is in a thread as
[i]void GO()
{
.
.
.
temp.Bind(ip);
temp.Listen(5);
while(1)
{
p = temp.Accept();
.
.
.
}
}[/i]

Now I want to close the thread using the [i]Abort()[/i] method of the [i]Thread[/i] class.

But it does not closes the thread because I can still see an instance of my program in the Task Manager.
When I remove the line
[i] p = temp.Accept()[/i]
it removes the thread and does not show any instance in the Task Manager.

My Question is: [b]how to shut this Accept function?
[/b]I tried [i]temp.Close() [/i]but it gives an Exception.
I tried [i]temp.Disconnect()[/i], but it says that a socket should be connected to some EndPoint.
There is a function [i]EndAccept[/i] but it takes some argument of type [i]interface[/i] which I didn't get.
Please help!

Share this post


Link to post
Share on other sites
btower    1661
You could wrap a try { } catch block around the code in your GO() method.

[code]try {
// code
} catch (ThreadException ex) {
}[/code]

Also, you don't need to manually kill the thread when the application quits. If you want the thread to die down when the process end, make the thread a background thread. You can do this by setting the Thread.IsBackground property to True.

Share this post


Link to post
Share on other sites
[quote name='btower' timestamp='1306306193' post='4815472']
You could wrap a try { } catch block around the code in your GO() method.

[code]try {
// code
} catch (ThreadException ex) {
}[/code]

Also, you don't need to manually kill the thread when the application quits. If you want the thread to die down when the process end, make the thread a background thread. You can do this by setting the Thread.IsBackground property to True.
[/quote]

Its not giving exception in the GO() method.
Its giving exception where I write the code to close the socket (not thread; thread is aborted without exception).
I tried including a try-catch there. Its not giving Exception. But still the problem is not solved. Its still in the task manager.

Share this post


Link to post
Share on other sites
btower    1661
Did you try the 2nd suggestion I wrote? Background threads are killed automatically once the process stops. By default, threads you create in C# are not background threads (iirc). You can set the IsBackground property of the thread you're running (before you start it of course).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this