Link to home
Start Free TrialLog in
Avatar of Delng
Delng

asked on

MultiThreading Winsock Question

Hello!

I am working on a game. The server can accept 5 winsock connections say ports 9991-9995. However, the client and the server are actually the in the same .exe file meaning anyone can be the server. So if that person is the server he will execute the following

        With frmGame.Winsock1(1)
            .LocalPort = 9991
            .Listen
        End With
   
        With frmGame.Winsock1(2)
            .LocalPort = 9992
            .Listen
        End With

But if he is the client only, the will skip this and execute a connection to one of those ports.

       With Winsock1(1)          
             .RemoteHost = ServerIP
             .RemotePort = ServerPort
             .Connect
        End With

How do I implement it properly? So when a person joins he is connected to the an available port. When someone quits, I know which ports r free that can be assigned. How do i send a message to all of them at a time?

Do i need to use threading? If so how? I tried but it fails on the 'listen' of IMSWinsockControl part.
ASKER CERTIFIED SOLUTION
Avatar of SilentRage
SilentRage

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Delng
Delng

ASKER

You used the same port for all connections right?
As...i notice u used

For Each WS In frmMain.Winsock

Where did you define it (WS)? Can't find it...is there a missing file?
I never defined it, so it's variant.  That's an older project before I started using Option Explicit.

to be explicit:

Dim WS As Winsock
you listen on a port the host user specifies.  then everybody connects to that IP/port.
Avatar of Delng

ASKER

Need to ask you something again. The server uses winsock(0) while the clients use winsock(i) where i > 0 right?
yep.  when you're a client though, Winsock(0) is what makes the connection.  How the 0 index is put to use is the clue throughout my entire program on whether I'm acting as client or server.
Avatar of Delng

ASKER

Thanks!

I have a problem now. Lets say 3 clients have connected to the server. One of them quits. The SendClients now will have a problem. This is because one of the Winsock of that client that has quit no longer exists because it has been unloaded. How can I solve this problem?
Avatar of Delng

ASKER

Oh I know..haha it works with the For Each thingy...
Thanks Dude...anymore questions I post here yah? Thanks!
you can also post questions on my board if you like.