以文本方式查看主题 - Foxtable(狐表) (http://foxtable.com/bbs/index.asp) -- 专家坐堂 (http://foxtable.com/bbs/list.asp?boardid=2) ---- [求助]Socket 服务停止后再启动就退出 (http://foxtable.com/bbs/dispbbs.asp?boardid=2&id=150178) |
|||||
-- 作者:zcl -- 发布时间:2020/5/22 19:32:00 -- [求助]Socket 服务停止后再启动就退出 请高手给看看: 我做了个简单 socket 服务程序,怎么停止了以后,再运行,就自动闪退呀?
|
|||||
-- 作者:zcl -- 发布时间:2020/5/22 19:49:00 -- 代码也很简单: 全局代码: Public server As myServer Public Class myServer Private Dim isRunning As Boolean Public Dim Encoding As Encoding Private Dim iPort As Integer = 8000 Public Dim thThreadRead As System.Threading.Thread Public Dim tlTcpListen As System.Net.Sockets.TcpListener Public Dim tcClient As System.Net.Sockets.TcpClient Public Sub New(ByVal ip As String, ByVal p As Integer ) Encoding = Encoding.GetEncoding("unicode") iPort = p thThreadRead = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Listen)) thThreadRead.Start() End Sub Private Sub Listen() Try tlTcpListen = New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, iPort) tlTcpListen.Start() tcClient = tlTcpListen.AcceptTcpClient() MessageBox.Show("已经建立TCP连接!") Catch ex As System.Security.SecurityException MessageBox.Show("侦听失败!", "错误") End Try End Sub Public Sub disConnet() Try thThreadRead.Abort() \'\'\'\'中止线程 tlTcpListen.Stop() \'\'\'\'关闭侦听 tcClient.Close() MessageBox.Show("停止") Catch End Try isRunning = False End Sub End Class 按钮 server = new myserver("127.0.0.1",8000) If e.sender.Text ="运行" Then \' server.mStart("127.0.0.1",6852) e.sender.Text ="停止" Else \'server.disconnet server.thThreadRead.Abort() e.sender.Text ="运行" End If 不知道哪里的问题。 |
|||||
-- 作者:有点蓝 -- 发布时间:2020/5/23 9:02:00 -- 参考:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=100826&skin=0 |
|||||
-- 作者:zcl -- 发布时间:2020/5/23 9:38:00 -- 有点蓝老师, 你那个TCP服务器端 倒是不闪退,但是死机呀
[此贴子已经被作者于2020/5/23 9:39:52编辑过]
|
|||||
-- 作者:zcl -- 发布时间:2020/5/23 11:08:00 -- 没人知道怎么回事吗? |
|||||
-- 作者:有点蓝 -- 发布时间:2020/5/23 11:10:00 -- 这个例子我测试没有问题
|