2010-07-16 2 views
4

여러 개의 포트에서 수신 대기하는 매우 작고 간단한 서버를 작성하고 있습니다..NET VB 여러 포트에서 서버 수신하는 방법?

아래 내용을 사용하고 있지만 포트 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 

답변

3

동기 방법 Sockets(i).Accept을 (사실은 내가 다중 연결을 혼동하고 있지 않다, 여러 포트에서 듣고 싶은). 첫 번째 소켓에 연결할 때까지 accept 메소드가 두 번째 소켓에서 호출되지 않습니다. 난 당신이 비동기 소켓을 구현하려는 생각

는 귀하의 배열이 너무 오래도 하나 개의 요소이다 http://msdn.microsoft.com/en-us/library/fx6588te.aspx

에서 샘플이 있습니다.

Dim EndPoints(ports.Length - 1) As IPEndPoint 
관련 문제