Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Creating a Network Class…


Pbcrazy
 Share

Recommended Posts

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 clsNetwork

CSocket.StartNetwork(netMode.Client, IP, Port)
CSocket.SendData(Data)
CSocket.StopNetwork()
```
or

```
Public SSocket as new clsNetwork

CSocket.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
    Server
End Enum
>! Private WithEvents Socket As Winsock
>! Public MAX_CONNECTIONS As Long
Public NETWORK_MODE As Byte
>! Private Sub Class_Initialize()
    Set Socket = New Winsock
End Sub
>! Private Sub Class_Terminate()
    Set Socket = Nothing
End Sub
>! Public Function StartNetwork(ByVal mode As Byte, ByVal ip As String, ByVal port As Long, Optional ByVal maxconnections As Long) As Boolean
Dim 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 = False
End Select
End Function
>! Public Function StopNetwork() As Boolean
Dim 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).Close

End Select
>! StopNetwork = True

End 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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...