建立了TCP连接之后,怎么释放端口?
客户端程序只要和服务端程序连接了,不关闭程序就没办法再次连接。代码如下,请高手指点
Try
tlTcpListen = New Net.Sockets.TcpListener(iPort)
tlTcpListen.Start()
ToolStripStatusLabel1.Text = "正在监听..."
tcClient = tlTcpListen.AcceptTcpClient()
nsStream = tcClient.GetStream()
srRead = New IO.StreamReader(nsStream)
ToolStripStatusLabel1.Text = "已经建立TCP连接!"
While blistener
Dim sMessage As String = srRead.ReadLine()
If (sMessage = "STOP") Then
tlTcpListen.Stop()
nsStream.Close()
srRead.Close()
ToolStripStatusLabel1.Text = "无连接!"
thThreadRead.Abort()
Return
Else
Dim sTime As String = DateTime.Now.ToShortTimeString()
ListBox1.Items.Add(sTime + " " + sMessage)
End If
End While
Catch ex As System.Security.SecurityException
MessageBox.Show("侦听失败!", "错误")
End Try