PDA

Click to See Complete Forum and Search --> : Winsock!!!


eegor
Mar 22nd, 2000, 04:06 AM
OK. I have been working on this problem for at least 2 weeks and it is probably a damn easy problem to solve but PLEASE HELK!!! Ready, here we go... I simply want my program to accept a max of 10 connections to WinSock. Therefore, what I did was create a Winsock control and called it WinsockMain. Then an array of 10 Winsock which are the real sockets. WinsockMain listens and when it receives a connection request, it passes the requestID to an open Winsock, and then closes and listens again. Look at the code:

Private Sub WinsockMain_ConnectionRequest(ByVal requestID As Long)

For i = 1 To 10
If Winsock(i).State = 0 Then GoTo WinsockAvailable
Next i

WinsockMain.Close: DoEvents
WinsockMain.Listen: DoEvents
Exit Sub

WinsockAvailable:
Winsock(i).Accept requestID: DoEvents
Winsock(i).SendData "Username: ": DoEvents
MainState(i) = 1
WinsockMain.Close: DoEvents
WinsockMain.RemoteHost = ""
WinsockMain.RemotePort = 0
WinsockMain.Listen: DoEvents

End Sub

The MainState is just something in my program, so don't worry about it. Now, here's the problem. Everything works alright, and the user connects and is passed to an open Winsock. However, randomly (at least I think), when a user is connected, a second user cannot enter, it just doesn't find the open port. I checked the WinsockMain control and the state was 2 (listening) and the port was correct and everything. But it just doesn't find the opening! Then, when I log out of the 1 user connection, it magically finds the open port. Also, this problem sometimes occurs when the logged on user logs out. It's very strange. Any ideas?

privoli
Mar 24th, 2000, 09:08 AM
Try...

WinsockAvailable:

' that forces the socket to use a random inbound port
' thus free-ing up the listening port, you must use
' this method when doing multiple sockets.

Winsock(i).LocalPort = 0
Winsock(i).Accept requestID: DoEvents
Winsock(i).SendData "Username: ":