By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,086 Members | 1,919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,086 IT Pros & Developers. It's quick & easy.

Help with sockets/winsock programming

P: 3
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....

Expand|Select|Wrap|Line Numbers
  1. 'Enable listening on port 25001
  2. Private Sub Form_Load()
  3.     Dim i As Integer
  4.     packetDelimiter = Chr$(0)
  5.     Set dbdata = OpenDatabase(App.Path & "/data/data.mdb") ' opens database file
  6.     RoomLimitNumber = 0
  7.     ItemLimitNumber = 0
  8.     Call LoadRooms
  9.  
  10.     'Pause
  11.     For i = 1 To 50
  12.         DoEvents
  13.     Next i
  14.  
  15.     Call LoadItems
  16.     'Load all the sockets and timers
  17.     WStcpServer(0).LocalPort = 25001
  18.     WStcpServer(0).Listen
  19.     num = 0 'User count
  20. End Sub
  21.  
  22. 'When a connection is recieved, goto verifying stages
  23. Private Sub WStcpServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
  24.     num = num + 1
  25.     Load WStcpServer(num)
  26.     Load Timer3(num)
  27.     Load tmrrefresh(num)
  28.     WStcpServer(num).Accept requestID
  29.     'Add a user to the arrays. Prevents making a 'limit' to the amount of users you can have.
  30.     MsgSend = "¿caut¶" & "Verify" 'Ask for verification
  31.     blnRetVal = WinsockSend(WStcpServer(num), MsgSend)
  32. End Sub
  33.  
  34. 'Preform this when a client closes or disconnects
  35. Private Sub WStcpServer_Close(Index As Integer)
  36.     Dim i As Integer
  37.         If Not users(Index) = "" Then 'If there was no connection or bad login, then don't do this, otherwise continue
  38.             txtMain.Text = txtMain.Text & "Disconnected: " & WStcpServer(Index).RemoteHostIP & ": " & users(Index) & vbCrLf
  39.             For i = WStcpServer.LBound + 1 To WStcpServer.UBound
  40.                 If userroom(i) = userroom(Index) Then
  41.                     If users(i) = users(Index) Then
  42.                     Else
  43.                         'Leave the game message
  44.                         MsgSend = "~g" & users(Index) & " has just left the game!"
  45.                         blnRetVal = WinsockSend(WStcpServer(i), MsgSend)
  46.                     End If
  47.                 End If
  48.             Next
  49.             users(Index) = "" 'Clear the username from the users. Frees up a socket.
  50.         End If
  51.         WStcpServer(Index).Close
  52.         Unload WStcpServer(Index)
  53.         Unload Timer3(Index)
  54.         Unload tmrrefresh(Index)
  55.         num = num - 1
  56.  
  57. End Sub

and basicly something id use would be..
Expand|Select|Wrap|Line Numbers
  1. 'Chat Sub
  2. Private Sub PublicChat(ByVal Index As Integer, Comarray() As String, ComI As Integer)
  3.     Dim i As Integer
  4.  
  5.     'Send to all users in room
  6.     For i = WStcpServer.LBound + 1 To WStcpServer.UBound
  7.         If userroom(i) = userroom(Index) Then 'In the room?
  8.             MsgSend = "~yFrom " & "~w" & users(Index) & "~y: " & Comarray(ComI)
  9.             blnRetVal = WinsockSend(WStcpServer(i), MsgSend)
  10.         Else
  11.         End If
  12.     Next
  13.  
  14. 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?
Mar 16 '06 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.