|
-
Jan 22nd, 2006, 11:17 AM
#1
Thread Starter
Member
Number of connected clients displayed is incorrect
I am running a server, multiple client network. Pretty much all is working correctly.
Part of the program displays the active number of client computers. As new workstations join the network, the server adds to the displayed number of connected clients, and as each client closes the client software, the server correctly changes the displayed number of connected computers.
BUT
If a client is forcibly shut down (e.g. holding the power button) while the client program is running, the server still displays that computer's connection state as connected(7).
Suggestions?
Connection code:
VB Code:
Private Sub Form_Load()
Socket(0).LocalPort = 9301
Socket(0).Listen
End Sub
Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Load Socket(Socket.UBound + 1)
Socket(Socket.UBound).Accept requestID
List1.AddItem requestID & " from " & Socket(Index).RemoteHostIP & " - " & Index
End Sub
display number of connections code (timer interval is 5000 msec)
VB Code:
Public Sub TChkCnctState_Timer()
sckCount = 0
For i = Socket.LBound To Socket.UBound
If Socket(i).State = sckConnected Then sckCount = sckCount + 1
Next i
lblConnections.Caption = sckCount
End Sub
Last edited by dlern; Jan 23rd, 2006 at 05:12 AM.
-
Feb 18th, 2006, 01:12 AM
#2
Member
Re: Number of connected clients displayed is incorrect
Hi i just join this forum. I'm doing on a project similar to the one u mention can you give me the coding to display computer which just on and how to display the number of connected computer. The code i have can only display the computers which are connected to the same network as mine if they off their computer then it won't display again. I also need to display the computer which are off coz i have to do Wake on LAN on them to wake them up. Please help me....
-
Feb 18th, 2006, 07:24 AM
#3
Re: Number of connected clients displayed is incorrect
Can you show me the Winsock_Close and Winsock_Error sub please? As im sure the problem lies there. If not, you could get around this by pinging your clients in that for loop, if you get the ping back obviously its connected, if no reply in say a few seconds then close the socket as they gone, this would give you an accurate online users message
Last edited by the182guy; Feb 18th, 2006 at 07:28 AM.
Chris
-
Feb 20th, 2006, 04:08 AM
#4
Thread Starter
Member
Re: Number of connected clients displayed is incorrect
 Originally Posted by the182guy
Can you show me the Winsock_Close and Winsock_Error sub please? As im sure the problem lies there. If not, you could get around this by pinging your clients in that for loop, if you get the ping back obviously its connected, if no reply in say a few seconds then close the socket as they gone, this would give you an accurate online users message
Actually, I used a debug.print statement with a timer to show the connection status of each connected computer every few seconds, and it shows the connection status as "7" for up to 5 miuntes after the client computer has been forcibly shut down. Therefore, I would think it has nothing to do with the winsock close or error subs.
By pinging, do you mean to use a "Wsk(index).senddata" event fro all the connected computers and then see if an appropriate "Wsk(index).GetData" event is returned, or did you have something else in mind?
Last edited by dlern; Feb 20th, 2006 at 04:38 AM.
-
Feb 20th, 2006, 04:44 AM
#5
Thread Starter
Member
Re: Number of connected clients displayed is incorrect
 Originally Posted by tauhu82
can you give me the coding to display computer which just on and how to display the number of connected computer. Please help me....
VB Code:
private sub timer1_timer()
For i = 1 To Socket.UBound
If Socket(i).State = 7 Then sckCount = sckCount + 1
Next i
lblConnections.Caption = sckCount
end sub
-
Feb 20th, 2006, 08:50 AM
#6
Re: Number of connected clients displayed is incorrect
Sometimes if the client is forced to shutdown or the socket errors, the socket will remain in its connected state if you dont put in your Winsock1_Close and _Error subs
-
Feb 20th, 2006, 09:43 AM
#7
Thread Starter
Member
Re: Number of connected clients displayed is incorrect
 Originally Posted by the182guy
Sometimes if the client is forced to shutdown or the socket errors, the socket will remain in its connected state if you dont put in your Winsock1_Close and _Error subs
Yes, but how do you know to close the socket if the socket state is reading "connected" even if there is no client that it is connected to. See what I mean?
VB Code:
Private Sub socket_Close(Index As Integer)
Socket(Index).Close
Unload Socket(Index)
End Sub
-
Feb 20th, 2006, 12:33 PM
#8
Re: Number of connected clients displayed is incorrect
 Originally Posted by dlern
Yes, but how do you know to close the socket if the socket state is reading "connected" even if there is no client that it is connected to. See what I mean?
VB Code:
Private Sub socket_Close(Index As Integer)
Socket(Index).Close
Unload Socket(Index)
End Sub
The socket close event is fired when the remote side disconnects for any reason, so the above code is fine, except if your using Unload to unload the socket when its disconnected if you used the For loop to get the number of online users, it would error. It will error because your trying to find out the state of something that doesnt exist, so if you used:
VB Code:
For i = 1 to Socket(UBound)
If Socket(i).State = sckConnected Then
Online=Online+1
End If
Next i
Lets say for example... you have 10 clients online, and client 4 decides to disconnect (by choice or forced from shutdown), your socket close event will fire, it closes client 4 socket and unloads it, then your timer runs the above loop.
When the i counter gets to 4 its going to raise an error because socket(4) doesnt exist - you unloaded earlier.
The other problem with using Unload on sockets is say you left the server app running 24/7 on a server computer, clients come and go, and everytime one comes, you are loading a new socket, and when it goes, its unloaded, so eventually you would be using a bad range such as
Socket(0) for accepting connections
Socket(1 to 500) have been used an unloaded
Socket(501 to 520) in use
So by using Unload on sockets its inefficient and you cant use a For loop to do something on it such as broadcast a message to all clients, or find out the users online, it can be done but its harder.
The solution to using Unload is by not unloading the socket when it disconnects, just leave it, and the next time a client requests connection, search the sockets to find one that is already loaded but not in use, and accept the connection on that instead of loading a new socket everytime. If theres no sockets already available for use, then just load a new one.
So it would run like....
1st Client connects....Search sockets for a free one, there is no free sockets because hes the first client, so load a new socket and accept.
2nd Client connects...Search sockets, there is none free, load up a new one and accept.....
Client 1 disconnects, closes socket
3rd client connects, search sockets, Oh? theres a free one, lets use it instead of loading a new one up, accept it on socket(1)
See what I mean? using Unload causes problems.
To code your connections so that it searches for a free socket, use something like:
VB Code:
Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim i As Integer
Dim FoundOne As Boolean
FoundOne = False
For i = 1 To Socket.UBound
If Socket(i).State = sckNotConnected Then
'We found a free socket, use it
Socket(i).Close
Socket(i).Accept requestID
FoundOne = True
End If
Next i
If FoundOne = False Then
'Theres no free sockets, so load one and accept
Load Socket(Socket.UBound + 1)
Socket(Socket.UBound).Close
Socket(Socket.UBound).Accept requestID
End If
End Sub
Dont forget to remove any Unload Socket lines from anywhere.
Then you can use for loops to loop through your sockets withotu error for doing things like broadcasting data or searching for online users.#
As for the online users thing, using a PING would be the most accurate option
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|