Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Key To Heaven - New Launch
#8
Hmm, the most troubling for me so far has been:
* I was not aware that VB6 and VB.net had missmatching variable declarations. So for example a Long in VB6 is a Integer in VB.net, a Integer in VB6 is a Short in VB.net. So that caused issues for me when I was trying to read all my binary data files (map,items etc...). Because all those bytes need to match exactly. And for some files they still didnt match and that was because VB6 just reads it and doesnt care if it missmatches with the length of the file. While vb.net crashes, but that was basicly solved by try catching the get function for the data, cause the data was still read.
* Encodings was a issue, there is like ASCII, ETF8, UNIFORM etc when converting to and from bytes. It took me sometime to grasp the fact that that was the issue. But I basically use System.Text.Encoding.Default. for now. You mainly just search for System.Text.Encoding. in your vb.net converted server to see all the locations for it.
* Its also a little tricky with the indexes of arrays, since vb6 has arrays from value 1-10 for exmple, but thats forbidden in vb.net (it has to start from 0). So there was a -1 here and there to take care of that.

Here is the winsock part, pretty short and simple. All you do is add:

Code:
Public Shared Network As New Sockethandler
in the ServerWindow class. And add:
Code:
Network.Initialize()
in the ServerWindow form loaded sub.

And then you will have some errors with frmServer.Socket that you change to ServerWindow.Server...

Code:
Imports System.Collections.Generic
Imports System.Net.Sockets
Imports Winsock_Control

Public Class Sockethandler

    Public WithEvents Server As Winsock
    Public Sockets As New Dictionary(Of Integer, Winsock)
    Private m_SocketsToRemove As New List(Of Winsock)

    Public Sub Initialize()
        Server = New Winsock
        Server.LocalPort = 4000
        Server.RemotePort = 4000
        Server.RemoteIP = "127.0.0.1"
        Server.Listen()
    End Sub

    Public Function GetState(index As Integer) As WinsockStates
        If TryGetUser(index) Is Nothing Then
            Return WinsockStates.Closed
        End If

        Return TryGetUser(index).GetState
    End Function

    Public Function TryGetIndex(socket As Winsock) As Integer
        For Each item As KeyValuePair(Of Integer, Winsock) In Sockets
            If item.Value Is socket Then
                Return item.Key
            End If
        Next
        Return -1
    End Function

    Public Function TryGetUser(index As Integer) As Winsock
        If Sockets.ContainsKey(index) Then Return Sockets(index)

        Return Nothing
    End Function

    Private Sub Socket_ConnectionRequest(sender As Winsock, requestID As Socket) Handles Server.ConnectionRequest
        Try
            Dim i As Integer
            i = FindOpenPlayerSlot()

            Sockets.Add(i, sender)
            Dim ws As Winsock = CType(sender, Winsock)

            AddHandler ws.DataArrival, AddressOf wsk_DataArrival
            AddHandler ws.Disconnected, AddressOf wsk_Disconnected

            ws.Accept(requestID)

            Call AcceptConnection(ws, requestID, i)
        Catch ex As Exception
            Diagnostics.Debugger.Break()
        End Try
    End Sub

    Private Sub wsk_Disconnected(sender As Winsock)
        If m_SocketsToRemove.Contains(sender) Then Exit Sub
        m_SocketsToRemove.Add(sender)

        If m_SocketsToRemove.Count > 0 Then
            Try
                CloseSocket(TryGetIndex(sender))
                m_SocketsToRemove.Remove(sender)
            Catch ex As Exception
                Diagnostics.Debugger.Break()
            End Try
        End If
    End Sub

    Private Sub wsk_DataArrival(sender As Winsock, BytesTotal As Integer)
        If IsConnected(TryGetIndex(sender)) Then
            Call IncomingData(TryGetIndex(sender), BytesTotal)
        End If
    End Sub

End Class
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)