Socket Closing (C#)

Eran

Member
Joined
Apr 12, 2003
Messages
69
Do I have to close all the opened sockets when I close the app,
or the sockets are being closed automaticly when the app is closed?
 
The problem i have faced is similar to like this

I have a TCP Listner, and i am assigining a new thread to a new incoming connection, if there are some active connections then although application is closed its proces in back is still running and then i have to go to taskmanager and then kill the process
 
In any case, if I have a thread and in this thread I have
Code:
int ret = soc.Receive(receive, receive.Length, 0);

I will have to use the Suspend method before aborting the thread. No?
 
suspending the task before closing it, resolved me a problem.

When I used to close a thread which has the command
Code:
int ret = soc.Receive(receive, receive.Length, 0);
gave the following Exception:


System.Threading.ThreadAbortException: Thread was being aborted.
at System.ComponentModel.Win32Exception..ctor(Int32 error)
at System.Net.Sockets.SocketException..ctor()
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 size, SocketFlags socketFlags)
at MaleClient.FrmMain.RecieveData() in c:\documents and settings\erany\my documents\visual studio projects\matchmaker\maleclient\frmmain.cs:line 221
 
That is how threads work-- the only way to terminate them is to throw an exception, catch it at the application level and ignore it. Calling Thread.Suspend() is essentially doing nothing, since a subsequent call to Thread.Abort() will implicitly resume the thread and then abort it.
 
No, they wont.

Code:
Dim t As New Thread(AddressOf ThreadProc)
t.Start()
Thread.Sleep(2000)
t.Abort()

Code:
Private Sub ThreadProc()
	Try
		Do
			Trace.WriteLine("Executing...")
		Loop
	Catch e As ThreadAbortException
		MessageBox.Show("""Thread.Abort()"" has been called.", "")
	End Try
End Sub
 
Back
Top