469,573 Members | 1,630 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

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....

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
  10.     'Pause
  11.     For i = 1 To 50
  12.         DoEvents
  13.     Next i
  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
  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
  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
  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
  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
  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
0 1721

Post your reply

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

Similar topics

4 posts views Thread by Garam | last post: by
3 posts views Thread by Robert A. van Ginkel | last post: by
9 posts views Thread by Gita George | last post: by
reply views Thread by Björn Pedersen | last post: by
2 posts views Thread by b.m. | last post: by
4 posts views Thread by John Pote | last post: by
2 posts views Thread by Fernando | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.