各位大虾,关于网络服务器监听的问题socket的用法 ( 积分: 48 )

  • 主题发起人 主题发起人 slb302
  • 开始时间 开始时间
S

slb302

Unregistered / Unconfirmed
GUEST, unregistred user!
我在vb.net中的webform窗体里面写服务器监听程序的时候遇到这样的问题:
'侦听
Sub EstablishSocketServer(ByVal state As Object)
Try
While run
Dim listener As Socket
listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
endPoint = New IPEndPoint(Dns.Resolve("192.168.10.211").AddressList(0), 1121)
listener.Blocking = True
listener.Bind(endPoint)
SetStatus(Status.侦听)
listener.Listen(0)
socket(socketCount) = listener.Accept
listener.Close()
socketCount = socketCount + 1
If Not (socket Is Nothing) then
SetStatus(Status.已连接)
Session.Item(yclj) = "True"
RaiseEvent Notifications(Notification.Initialized, Me, 0)
else
RaiseEvent Notifications(Notification.ErrorNotify, "建立Socket失败,检查端口设置是否正确", socketCount)
End If
End While
Catch e As SocketException
RaiseEvent Notifications(Notification.ErrorNotify, "初始化Socket时发生错误:" &
ControlChars.CrLf &
e.ToString(), socketCount)
'触发事件
Catch e As IOException
Dim sockExcept As SocketException = CType(e.InnerException, SocketException)
If Not (sockExcept Is Nothing) And 10054 = sockExcept.ErrorCode then
RaiseEvent Notifications(Notification.EndNotify, "远程连接已经断开", socketCount)
else
RaiseEvent Notifications(Notification.ErrorNotify, "Socket 错误" &
ControlChars.CrLf &
e.Message, socketCount)
End If
Catch e As Exception
RaiseEvent Notifications(Notification.ErrorNotify, "Socket 错误" &
ControlChars.CrLf &
e.Message, socketCount)
End Try
当我关闭网页的时候,listener.Close()这句话不执行,也就是说对象不会释放,那么端口就会被占用。
请问大虾,怎么样在网页关闭的时候让listener.Close()执行,释放端口呢?
急啊 ~~~~~~~~~~~
 
我在vb.net中的webform窗体里面写服务器监听程序的时候遇到这样的问题:
'侦听
Sub EstablishSocketServer(ByVal state As Object)
Try
While run
Dim listener As Socket
listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
endPoint = New IPEndPoint(Dns.Resolve("192.168.10.211").AddressList(0), 1121)
listener.Blocking = True
listener.Bind(endPoint)
SetStatus(Status.侦听)
listener.Listen(0)
socket(socketCount) = listener.Accept
listener.Close()
socketCount = socketCount + 1
If Not (socket Is Nothing) then
SetStatus(Status.已连接)
Session.Item(yclj) = "True"
RaiseEvent Notifications(Notification.Initialized, Me, 0)
else
RaiseEvent Notifications(Notification.ErrorNotify, "建立Socket失败,检查端口设置是否正确", socketCount)
End If
End While
Catch e As SocketException
RaiseEvent Notifications(Notification.ErrorNotify, "初始化Socket时发生错误:" &
ControlChars.CrLf &
e.ToString(), socketCount)
'触发事件
Catch e As IOException
Dim sockExcept As SocketException = CType(e.InnerException, SocketException)
If Not (sockExcept Is Nothing) And 10054 = sockExcept.ErrorCode then
RaiseEvent Notifications(Notification.EndNotify, "远程连接已经断开", socketCount)
else
RaiseEvent Notifications(Notification.ErrorNotify, "Socket 错误" &
ControlChars.CrLf &
e.Message, socketCount)
End If
Catch e As Exception
RaiseEvent Notifications(Notification.ErrorNotify, "Socket 错误" &
ControlChars.CrLf &
e.Message, socketCount)
End Try
当我关闭网页的时候,listener.Close()这句话不执行,也就是说对象不会释放,那么端口就会被占用。
请问大虾,怎么样在网页关闭的时候让listener.Close()执行,释放端口呢?
急啊 ~~~~~~~~~~~
 
后退
顶部