Я пишу очень маленький и простой сервер, который я хочу слушать на нескольких портах.
Я использую ниже, но подключения к порту 8081 не принимаются - какие-либо предложения, что я делаю неправильно?
(я на самом деле хочу прослушивать несколько портов, я не путаю множественные соединения)
Public Sub StartServer()
Dim ports() As Integer = {8080, 8081}
Dim portList As String = ""
Dim address As IPAddress = IPAddress.Any
' map the end point with IP Address and Port
'Create Endpoints
Dim EndPoints(ports.Length) As IPEndPoint
'Create sockets
Dim Sockets(ports.Length) As Socket
Dim i As Integer = 0
For i = 0 To ports.Length - 1
portList = portList & ports(i) & " : "
EndPoints(i) = New IPEndPoint(address, ports(i))
Sockets(i) = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Sockets(i).Bind(EndPoints(i))
Sockets(i).Listen(20)
Log("Listening on: All ips & Port: " & ports(i))
Next
Log("Listening on: All ips & Ports: " & portList)
Do While Not Me.DoStop
' Wait for an incoming connections
For i = 0 To ports.Length - 1
Dim sock As Socket = Sockets(i).Accept()
' Connection accepted
' Initialise the Server class
Dim ServerRun As New Server(sock)
' Create a new thread to handle the connection
Dim t As Thread = New Thread(AddressOf ServerRun.HandleConnection)
t.IsBackground = True
t.Priority = ThreadPriority.BelowNormal
t.Start()
Next
' Loop and wait for more connections
Loop
End Sub