Jump to content
  • Advertisement
Sign in to follow this  
Shashwat Rohilla

Socket Programming

This topic is 2612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using VS2008 C# .Net

I have created a socket temp to accept any incoming connection request.

I have used
Socket p = temp.Accept()

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


Now I want to close the thread using the Abort() method of the Thread 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
p = temp.Accept()
it removes the thread and does not show any instance in the Task Manager.

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

Share this post


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

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


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

You could wrap a try { } catch block around the code in your GO() method.

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


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.


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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!