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.
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.
Re: Accepting on a specific port
Are you refering to the data socket on the server end?
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.
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:
Private Sub tcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim indx As Integer
If Index = 0 Then
' If we already have max connections, ignore this request
If ClientCount = MaxUsers Then
RequestLog.Text = RequestLog.Text & "Max connects - connection refused" & vbCrLf
Exit Sub
End If
' look for an available socket
For indx = 1 To MaxUsers
If Not cntlLoaded(indx) Then
' all existing sockets are busy - create a new one
cntlLoaded(indx) = True
Load tcpServer(indx)
tcpServer(indx).Close
ClientCount = ClientCount + 1
End If
If tcpServer(indx).State = sckClosed Then
' Accept the request with the requestID parameter.
tcpServer(indx).LocalPort = 0
tcpServer(indx).Accept requestID
reqIds(indx) = requestID
cmdAccept(indx - 1).Enabled = True
cmdReject(indx - 1).Enabled = True
Exit For
End If
Next indx
RequestLog.Text = RequestLog.Text & "Connection accepted - ID #" & requestID & vbCrLf
End If
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.