.NET VB How does a server listen on multiple ports?

I am writing a very small and simple server that I want to listen on several ports.

I use below, but connections to port 8081 are not accepted - any suggestions, what am I doing wrong?

(I really want to listen to several ports, I do not confuse several connections)

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 
+4
source share
1 answer

The synchronous Sockets(i).Accept will block until it receives a connection. The accept method will not be called on your second socket until you connect to the first.

I think you want to implement asynchronous sockets, there is a sample at http://msdn.microsoft.com/en-us/library/fx6588te.aspx

Your arrays are also one of the elements for too long.

 Dim EndPoints(ports.Length - 1) As IPEndPoint 
+3
source

Source: https://habr.com/ru/post/1315934/


All Articles