Hi
I never done threading or worked with sockets in anyway. I have read tons of ducument on this now and tried a million samples. I trying to keep this bit of code as simple as possible.
1) I was wondering how i can close this tread without getting any exceptions.
2) where could i make improvement on it without confusing a newbie.
i think this code was orginaly by a guy called johnh.. read alot of his threads on this forum "very good". i made some changes to make it simple for me.
this code is on the server app that listen for "messages" that i am trying to fix.
It does work fine now but i get an exception when closing app.
note that you can copy and paste this code into empty app and add controll and run it. it work. when you close the app it gives exception
all i am doing here send simple text from client to server as test.
I never done threading or worked with sockets in anyway. I have read tons of ducument on this now and tried a million samples. I trying to keep this bit of code as simple as possible.
1) I was wondering how i can close this tread without getting any exceptions.
2) where could i make improvement on it without confusing a newbie.
i think this code was orginaly by a guy called johnh.. read alot of his threads on this forum "very good". i made some changes to make it simple for me.
this code is on the server app that listen for "messages" that i am trying to fix.
It does work fine now but i get an exception when closing app.
note that you can copy and paste this code into empty app and add controll and run it. it work. when you close the app it gives exception
VB.NET:
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listen.Abort()
End Sub
VB.NET:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
listen = New Threading.Thread(AddressOf listener)
listen.Start()
End Sub
VB.NET:
Private listen As Threading.Thread
' Public listen As Threading.Thread
Dim mystop As Boolean
Sub listener(ByVal mystop As Boolean)
'CheckForIllegalCrossThreadCalls = False
'Dim l As New TcpListener(Net.IPAddress.Parse("127.0.0.1"), 8765)
Try
Dim l As New TcpListener(Net.IPAddress.Any, 3252)
l.Start()
ToolStripStatusLabel1.Text = "Listening"
Do
If l.Pending Then l.BeginAcceptTcpClient(AddressOf accepting, l)
Threading.Thread.Sleep(1000)
Loop
Catch ex As System.Threading.ThreadAbortException
Return
End Try
End Sub
all i am doing here send simple text from client to server as test.
VB.NET:
Sub accepting(ByVal ar As IAsyncResult)
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
Dim networkStream As NetworkStream = clientSocket.GetStream()
Dim bytes(clientSocket.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, CInt(clientSocket.ReceiveBufferSize))
Dim clientdata As String = Encoding.ASCII.GetString(bytes)
MessageBox.Show(clientdata)
networkStream.Flush()
clientSocket.Close()
End Sub
Last edited: