|
-
Sep 24th, 2002, 02:14 PM
#1
Thread Starter
New Member
Disconnecting a TCPlistener
I am using the following code from the msdn library to establish TCP connections as a server. Would someone be so kind as to throw me a clue on how to modify it to keep track of the listener connections and make any individual connection close(kick off) (for example on button click of textbox value )?
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Public Delegate Sub StatusInvoker(ByVal t As String)
Public Class Form1
Inherits System.Windows.Forms.Form
Private mobjThread As Thread
Private mobjListener As TcpListener
Private mcolClients As New Hashtable()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mobjThread = New Thread(AddressOf DoListen)
mobjThread.Start()
UpdateStatus("Listener started")
End Sub
Private Sub DoListen()
Try
mobjListener = New TcpListener(5000)
mobjListener.Start()
Do
'Dim x As New Client(mobjListener.AcceptSocket)
Dim x As New Client(mobjListener.AcceptTcpClient)
AddHandler x.Connected, AddressOf OnConnected
AddHandler x.Disconnected, AddressOf OnDisconnected
'AddHandler x.CharsReceived, AddressOf OnCharsReceived
AddHandler x.LineReceived, AddressOf OnLineReceived
mcolClients.Add(x.ID, x)
Dim params() As Object = {"New connection"}
Me.Invoke(New StatusInvoker(AddressOf Me.UpdateStatus), params)
Loop Until False
Catch
End Try
End Sub
Private Sub OnConnected(ByVal sender As Client)
UpdateStatus("Connected")
End Sub
Private Sub OnDisconnected(ByVal sender As Client)
UpdateStatus("Disconnected")
mcolClients.Remove(sender.ID)
End Sub
'Private Sub OnCharsReceived(ByVal sender As Client, ByVal Data As String)
' UpdateStatus("Chars:" & Data)
'End Sub
Private Sub OnLineReceived(ByVal sender As Client, ByVal Data As String)
UpdateStatus("Line:" & Data)
'Dim objClient As Client
'Dim d As DictionaryEntry
'For Each d In mcolClients
' objClient = d.Value
'objClient.Send(Data & vbCrLf)
'Next
End Sub
Private Sub UpdateStatus(ByVal t As String)
lstStatus.Items.Add(t)
lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
mobjListener.Stop()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim tempstring As String
Dim filenum As Integer
filenum = FreeFile()
FileOpen(filenum, "C:\halcyon\system2.txt", OpenMode.Input)
While Not EOF(filenum)
tempstring = LineInput(filenum)
End While
FileClose(filenum)
TextBox1.Text = tempstring
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
lstStatus.Items.Add(TextBox1.Text)
lstStatus.SetSelected(lstStatus.Items.Count - 1, True)
Dim d As DictionaryEntry
Dim objClient As Client
Dim tempstring As String
tempstring = TextBox1.Text & vbCrLf
For Each d In mcolClients
objClient = d.Value
objClient.Send(tempstring)
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|