Results 1 to 5 of 5

Thread: Accepting on a specific port

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    2

    Accepting on a specific port

    I am listening on specific port for incoming connection requests. Because this port is known I am able to modify the "port forwarding" on the router to send requests to the correct computer.

    However, when the ACCEPT method is called for the WinSock control it randomly picks a port. How do I specify the port I want to use for the ACCEPT method?

    If I can manually select the port then I am able to open a specific block of ports on the router.

    Any help would be appreciated.

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Accepting on a specific port

    Quote Originally Posted by hiddenndangerus
    However, when the ACCEPT method is called for the WinSock control it randomly picks a port. How do I specify the port I want to use for the ACCEPT method?
    The connection(s) will be accepted on the port that you are listening on.

    Aside from that, I'm not 100% sure what you are trying to do.
    Last edited by ccoder; Apr 12th, 2005 at 05:20 PM.

  3. #3
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Accepting on a specific port

    Are you refering to the data socket on the server end?
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Posts
    2

    Re: Accepting on a specific port

    Normally when you accept an incoming connection requestion you use the LocalPort of ZERO (0). The newly created connection uses a random port number leaving the original port for listening.

    For example: Create a socket listening on port 4001. When a connection request comes in you accept it using a new socket. The newly created socket connection uses a random port number (say 1512).

    What I want to do is leave port 4001 for listening and have the connections use ports 4002, 4003, 4004...etc. That way I know which ports I have to forward on the router.

  5. #5
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177

    Re: Accepting on a specific port

    You can (and should) set the LocalPort to zero in the client so that the system will assign an unused port.

    When you set the LocalPort to zero in the server, the new control will still accept the connection on the port that the listener is using.

    Take a look at the attached bmp. It is a screen capture of a C/S demo that I did when I was teaching myself sockets programming in VB and working out the details of sending UTDs. You will see that all four of the server's Winsock controls are using port 5556.

    Here is the ConnectionRequest code:
    VB Code:
    1. Private Sub tcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    2.    Dim indx As Integer
    3.    
    4.    If Index = 0 Then
    5.       ' If we already have max connections, ignore this request
    6.       If ClientCount = MaxUsers Then
    7.          RequestLog.Text = RequestLog.Text & "Max connects - connection refused" & vbCrLf
    8.          Exit Sub
    9.       End If
    10.       ' look for an available socket
    11.       For indx = 1 To MaxUsers
    12.           If Not cntlLoaded(indx) Then
    13.              ' all existing sockets are busy - create a new one
    14.              cntlLoaded(indx) = True
    15.              Load tcpServer(indx)
    16.              tcpServer(indx).Close
    17.              ClientCount = ClientCount + 1
    18.           End If
    19.           If tcpServer(indx).State = sckClosed Then
    20.              ' Accept the request with the requestID parameter.
    21.              tcpServer(indx).LocalPort = 0
    22.              tcpServer(indx).Accept requestID
    23.              reqIds(indx) = requestID
    24.              cmdAccept(indx - 1).Enabled = True
    25.              cmdReject(indx - 1).Enabled = True
    26.              Exit For
    27.           End If
    28.       Next indx
    29.       RequestLog.Text = RequestLog.Text & "Connection accepted - ID #" & requestID & vbCrLf
    30.    End If
    31.    
    32. End Sub

    Oops, forgot to click Upload. And now I see the file is too large. PM or email me if you want to see the image.
    Last edited by ccoder; Apr 13th, 2005 at 10:03 AM. Reason: Attachment didn't upload

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width