Help with sockets/winsock programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Valleriani
    New Member
    • Mar 2006
    • 3

    Help with sockets/winsock programming

    Right Now I've been working on a text based game for fun. It was working fine but they I relized that you need to shut/close the ports. Before I was just opening ports and not shutting them, no problems because it will just go to the next port. But this way is not correct. I started shutting the ports but now I get the error of the 'user' not being in the correct socket when its checking etc. For example: User 1 logs in, user 2 logs in as well. User one logs off, User 2 tries to talk or such but cannot because the server thinks theres only 1 socket being used.. (Basicly thinks hes in socket one since you minus a socket on logoff) I personally don't know how to fix it. I tried a few things with ports but they didn't work correctly, I am stumped to how to fix it. Personally I think it should move or detect theres a missing port and go to the next one since its closed or something like that. I'm not really sure.. Welp, here is some coding....

    Code:
    'Enable listening on port 25001
    Private Sub Form_Load()
        Dim i As Integer
        packetDelimiter = Chr$(0)
        Set dbdata = OpenDatabase(App.Path & "/data/data.mdb") ' opens database file
        RoomLimitNumber = 0
        ItemLimitNumber = 0
        Call LoadRooms
        
        'Pause
        For i = 1 To 50
            DoEvents
        Next i
        
        Call LoadItems
        'Load all the sockets and timers
        WStcpServer(0).LocalPort = 25001
        WStcpServer(0).Listen
        num = 0 'User count
    End Sub
    
    'When a connection is recieved, goto verifying stages
    Private Sub WStcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
        num = num + 1
        Load WStcpServer(num)
        Load Timer3(num)
        Load tmrrefresh(num)
        WStcpServer(num).Accept requestID
        'Add a user to the arrays. Prevents making a 'limit' to the amount of users you can have.
        MsgSend = "¿caut¶" & "Verify" 'Ask for verification
        blnRetVal = WinsockSend(WStcpServer(num), MsgSend)
    End Sub
    
    'Preform this when a client closes or disconnects
    Private Sub WStcpServer_Close(Index As Integer)
        Dim i As Integer
            If Not users(Index) = "" Then 'If there was no connection or bad login, then don't do this, otherwise continue
                txtMain.Text = txtMain.Text & "Disconnected: " & WStcpServer(Index).RemoteHostIP & ": " & users(Index) & vbCrLf
                For i = WStcpServer.LBound + 1 To WStcpServer.UBound
                    If userroom(i) = userroom(Index) Then
                        If users(i) = users(Index) Then
                        Else
                            'Leave the game message
                            MsgSend = "~g" & users(Index) & " has just left the game!"
                            blnRetVal = WinsockSend(WStcpServer(i), MsgSend)
                        End If
                    End If
                Next
                users(Index) = "" 'Clear the username from the users. Frees up a socket.
            End If
            WStcpServer(Index).Close
            Unload WStcpServer(Index)
            Unload Timer3(Index)
            Unload tmrrefresh(Index)
            num = num - 1
       
    End Sub

    and basicly something id use would be..
    Code:
    'Chat Sub
    Private Sub PublicChat(ByVal Index As Integer, Comarray() As String, ComI As Integer)
        Dim i As Integer
        
        'Send to all users in room
        For i = WStcpServer.LBound + 1 To WStcpServer.UBound
            If userroom(i) = userroom(Index) Then 'In the room?
                MsgSend = "~yFrom " & "~w" & users(Index) & "~y: " & Comarray(ComI)
                blnRetVal = WinsockSend(WStcpServer(i), MsgSend)
            Else
            End If
        Next
        
    End Sub
    Everything is FINE until someone disconnects because it doesn't know how to handle exiting properly i think.
    Personally the getdata is fine, but its the sockets. I do not know how to move or skip sockets that aren't in use. Another example would be 2 people on, one disconnects, the first person disconnects.. so person on socket 2 other person cannot talk because its trying to send the data to a invalid socket (thinking its socket 1 I believe) Something like that.. Any help? Need more info?
    Last edited by Valleriani; Mar 16 '06, 02:06 PM.
Working...