发布网友 发布时间:2022-04-22 08:31
共3个回答
热心网友 时间:2022-06-18 16:42
Dim s As Socket
Private Shared Function ConnectSocket(ByVal server As String, ByVal port As Integer) As Socket'创建连接
Dim s As Socket = Nothing
Dim hostEntry As IPHostEntry = Nothing
Dim address As IPAddress
address = System.Net.IPAddress.Parse(server)
Dim endPoint As New IPEndPoint(address, port)
Dim tempSocket As New Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
tempSocket.Connect(endPoint)
If tempSocket.Connected Then
s = tempSocket
End If
Return s
End Function
调用:s = ConnectSocket(IP地址, 端口)'端口号如8000,8080等
s.close'关闭连接
热心网友 时间:2022-06-18 16:42
长连接一般用于异步通讯处理,短连接一般用于同步通讯处理。要保持长连接,只要连接上以后,不调用Shutdown和Close就不会断,如果断了,检查你的代码。
热心网友 时间:2022-06-18 16:43
不会自己断的。
要么是你代码有问题,要么是对方主动给你断了的。