Pbcrazy Posted February 29, 2012 Author Share Posted February 29, 2012 Hey folks, I've been playing with Eclipse recently, and I've been wanting to try to do a little something that will hopefully make my other projects easier, and possibly yours as well.In the current system, we all have a winsock control stuck onto a form both on the client and server right? You can tell it to do stuff by saying…```frmMain.Socket.$$$$```or if you're on the server…```frmServer.Socket(Index).$$$$```Well, what I've been attempting to do is write a new network code (well, pretty much the same code, not really anything to change there), but put it all into a class. That way you can create a network object the same way you create a DirectX object. Say in modGeneral or something like…```Public CSocket as new clsNetworkCSocket.StartNetwork(netMode.Client, IP, Port)CSocket.SendData(Data)CSocket.StopNetwork()```or```Public SSocket as new clsNetworkCSocket.StartNetwork(netMode.Server, IP, Port)CSocket.SendDataTo(Index, Data)CSocket.StopNetwork()```That's the basic concept anyway, to create a network class that'll make my life easier when programming networks. Just simple commands that will fit practically any game.However, I've been running into a few issues with the server socket array. In the class, I only declare one socket, and then depending on how the class is initialized, i either keep a single socket as the client, or redim a socket array for the server.The code "works" thus far fine, except till you try to close down the program. Regardless of being set to either Client or Server, it'll error out on the Server close for loop.Here's the code for the entire class so far:>! ```Option Explicit>! Public Enum netMode Client = 1 ServerEnd Enum>! Private WithEvents Socket As Winsock>! Public MAX_CONNECTIONS As LongPublic NETWORK_MODE As Byte>! Private Sub Class_Initialize() Set Socket = New WinsockEnd Sub>! Private Sub Class_Terminate() Set Socket = NothingEnd Sub>! Public Function StartNetwork(ByVal mode As Byte, ByVal ip As String, ByVal port As Long, Optional ByVal maxconnections As Long) As BooleanDim i As Long>! NETWORK_MODE = mode>! Select Case NETWORK_MODE Case netMode.Client Socket.Close Socket.RemoteHost = ip Socket.RemotePort = port Socket.Connect If Socket.State = sckConnected Then StartNetwork = True Else StartNetwork = False End If Case netMode.Server MAX_CONNECTIONS = maxconnections ReDim Socket(0 To MAX_CONNECTIONS) For i = 0 To MAX_CONNECTIONS Set Socket(i) = New Winsock Next i Socket(0).Close Socket(0).RemoteHost = ip Socket(0).LocalPort = port Socket(0).Listen>! StartNetwork = True Case Else MsgBox ("Error: Invalid network mode selected!") StartNetwork = FalseEnd SelectEnd Function>! Public Function StopNetwork() As BooleanDim i As Long>! Select Case NETWORK_MODE Case netMode.Client If Socket.State = sckConnected Then Socket.Close End If Case netMode.Server For i = 1 To MAX_CONNECTIONS If Socket(i).State = sckConnected Then Socket(i).Close End If Next i Socket(0).CloseEnd Select>! StopNetwork = TrueEnd Function```And here is where it errors out with the error "wrong number of arguments or invalid property assign" (something like that):``` If Socket(i).State = sckConnected Then Socket(i).Close```If anyone has any idea what's wrong, it'd be greatly appreciated. Or if you even have advice on how to do this better, that'd be great.Thanks,-Pb Link to comment Share on other sites More sharing options...
Robin Posted February 29, 2012 Share Posted February 29, 2012 Go look around the various Winsock API classes online. Link to comment Share on other sites More sharing options...
Pbcrazy Posted February 29, 2012 Author Share Posted February 29, 2012 That's what I have been doing, hence why I've gotten this far.I'm not much of a programmer though, so I was hoping to get a bit more of an explanation as to why or how from some one here.I'll keep looking though… Link to comment Share on other sites More sharing options...
Robin Posted March 2, 2012 Share Posted March 2, 2012 From what I can see you're just wrapping a control. You're better off using the API directly. Link to comment Share on other sites More sharing options...
Pbcrazy Posted March 3, 2012 Author Share Posted March 3, 2012 Please do explain? What's the difference between wrapping a control and using the API directly? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now