Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SOX instead of Winsock
#1
Server Side:

-Delete the mswinsock control from the project

-Delete it from the control list too

-Add the sox.ocx

-Name it Socket



frmServer.frm:



delete or comment out the code you have for the old socket,

Socket_Accept

Socket_DataArrival

Socket_Close



Now make a new Socket_State sub(go to the topleft combo box thing in the code window and select socket)



The state sub is called everytime the state of the socket changes

0 = close

1 = listening ‘server thing

2 = Connecting ‘Client thing

3 = Accepted ‘Server thing, Sox automatically accepts connections

4 = Connected ‘Client thing

Code:
Private Sub Socket_State(ByVal Sox As Long, ByVal State As Long)

    If Sox  0 Then 'not the main listening socket

        Select Case State

             Case 3

                 Call AcceptConnection(Sox)

             Case 0

                 Call CloseSocket(Sox)

        End Select

    End If    

End Sub

That should make sense now.

Now make a new DataArrival procedure.



Private Sub Socket_DataArrival(ByVal Sox As Long, Data As Variant)



End Sub



Inside add

If IsConnected(Sox) Then

‘sox is the internal array index, no more control arrays, but you should know that if you read the readme Tongue.



Next add



Call IncomingData(Sox, data)

End If



You will change the incomingdata sub later



It should end up looking like this..

Code:
Private Sub Socket_DataArrival(ByVal Sox As Long, Data As Variant)

    If IsConnected(Sox) Then

        Call IncomingData(Sox, Data)

    End If

End Sub



Private Sub Socket_Error(Sox As Long, Proc As String, Area As String, Code As Long, Abbr As String, Desc As String, DescEx As String)

    'MsgBox "*** Error ***" & vbCrLf & vbTab & "Sox: " & Sox & vbCrLf & vbTab & "Proc: " & Proc & vbCrLf & vbTab & "Area: " & Area & vbCrLf & vbTab & "Code: " & Code & vbCrLf & vbTab & "Abbr: " & Abbr & vbCrLf & vbTab & "Desc: " & Desc & vbCrLf & vbTab & "DescEx: " & DescEx & vbCrLf & vbCrLf

End Sub

If you want you can add that, don’t need to, I commented it out on mine.



Next: Incoming Data Sub

Change the header to look like this

Sub IncomingData(ByVal Index As Long, ByVal Data As Variant)



Comment out or delete this:

'frmServer.Socket(Index).GetData Buffer, vbString, DataLength



Add this, it converts the data to a string

Buffer = StrConv(Data, vbUnicode)



Scroll down in that sub and fine this:

' Check if elapsed time has passed

Change the line below it to this:

Code:
Player(Index).DataBytes = Player(Index).DataBytes + UBound(Data)




Next go to the AcceptConnection sub

Code:
Sub AcceptConnection(ByVal Index As Long)

Dim i As Long



    If (Index = 0) Then

        i = FindOpenPlayerSlot

        

        If i  0 Then

             ' Whoho, we can connect them

             Call SocketConnected(Index)



        Else

             frmServer.Socket.Close (Index)

        End If

    End If

End Sub

Make it like that



In Sub CloseSocket

Comment out where it closes the socket, kind of stupid making it close the socket since its getting called once the socket has closed.



Change Function GetPlayerIP to

Code:
Function GetPlayerIP(ByVal Index As Long) As String

    GetPlayerIP = frmServer.Socket.RemoteHostIP(Index)

End Function

Find ' Get the listening socket ready to go

Comment out both of the 2 lines under it which set the remotehost/port



Comment/delete were it loads/unloads the sockets, you don’t need to do that anymore, its done internally.



Find frmServer.Socket.Listen

-Change it to

frmServer.Socket.Listen frmServer.Socket.RemoteHostIP(0), Game_port (might need to change it to 127.0.0.1, I’m not sure)

Go to Sub ServerLogic

Get rid of that forloop which checks to see if it should close the socket



Go to Sub UpdateCaption

Change it how you want, figure it out.

Go to Function IsConnected

Add

Code:
If Index >= frmServer.Socket.Count Then

        IsConnected = False

        Exit Function

    End If

This is to avoid an error if it checks a socket which isn’t there.

Code:
If frmServer.Socket.State(Index) = 3 Then

        IsConnected = True

    Else

        IsConnected = False

    End If

Now change the other if statement to this…



Find .SendData (data here)

Change it to frmserver.socket.sendstring i/index(depending where it is), data

If you want to change this senddatatoall sub you can, you don’t have to though

Go to sub senddatatoall

Change it to

frmServer.Socket.broadcast data
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)