473,394 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
  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
0 1849

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

Similar topics

4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
4
by: Garam | last post by:
I need to write a substantial amount of network code in C. The thing is, it has to be able to run on both Unix and Windows. Rather than writing separate code for each, I was looking for something...
3
by: Robert A. van Ginkel | last post by:
In news:OZ0W9RsdDHA.2432@TK2MSFTNGP10.phx.gbl... I ask the question how I can see if all the data is on the other side of the connection. I got as answer that I should use the blocking property. I...
2
by: Nadav | last post by:
Hi, I am about to write a performance critical application and I wonder what is the best way to implement it... Should I use the standart Win32 winsock DLL or should I rather use the new managed...
9
by: Gita George | last post by:
I'm trying to write a program (a pop3 mail checker) and I'm having a problem. I'm using the socket control and I've noticed that I do not have any events !? How do I use the socket to receive...
0
by: Björn Pedersen | last post by:
I'm having a tough time getting System.Net.Sockets to work with a server that's based around Winsock 6.0. For many reasons I can't write a new server because the server handles a number of client...
2
by: b.m. | last post by:
Hi, I'm a beginner to VB.NET ... I ve been trying to learn .net System.Net.Sockets but i cant seem to figure out a few things... Being that I am an ex-VB6 developer, I am pretty used to...
4
by: John Pote | last post by:
Hi all, I want to use python on a server to access incoming TCP port accesses. So I need to use the socket interface which is new to me. To quote the Py Library Reference "7.2 socket --...
2
by: Fernando | last post by:
Is there anything or anyway to do something simmilar to WSAAsyncSelect which is asyncronous but the notifications comes in the same thread( throgh messages ) , which for simple things facilitates...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.