|
-
Apr 29th, 2008, 06:10 PM
#1
Thread Starter
Hyperactive Member
winsock again
Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
If Index = 0 Then
sRequestID = requestID
iSockets = iSockets + 1
Load sckServer(iSockets)
sckServer(iSockets).Accept requestID
End If
End Sub
Private Sub sckServer_Close(Index As Integer)
On Error Resume Next
imgPC(Index).Picture = imgDC
cmdPC(Index).ToolTipText = "Disconnected"
iSockets = iSockets - 1
Unload sckServer(Index)
End Sub
no problem on incoming connections
tha only problem is that:
1. cilent2 connects the index is 1
2. cilent1 connects the index is 2
how can i tell to the PC that even client2 1st connect the index is 2?
tnx
*****************
VB6,PHP,VS 2005
-
Apr 29th, 2008, 07:15 PM
#2
Re: winsock again
Don't unload the socket when a client disconnects. It's best to just keep it there, close it, and reuse it for the next incoming connection. Not only that, but there is a memory leak in the Winsock control that causes unloaded sockets to not be released from memory, so this will help with that issue. (There is an article on MSDN about this issue).
So, in the Close() event, remove the code that unloads the socket and replace it with sckServer(Index).Close
For an incoming connection, use something like this to find a socket to use:
Code:
Private Function FindAvailableSocket() As Integer
Dim i As Integer, intRet As Integer
If sckServer.UBound = 0 Then
'No other sockets loaded, just load #1.
Load sckServer(1)
FindAvailableSocket = 1
Else
'More than one socket, find one we can use.
'aka: one that is not in use already (.state = sckClosed).
For i = 1 To sckServer.UBound
If sckServer(i).State = sckClosed Then
intRet = i
Exit For
End If
Next i
If intRet > 0 Then
FindAvailableSocket = intRet
Else
'Didn't find one, load a new one.
intRet = sckServer.UBound + 1
Load sckServer(intRet)
FindAvailableSocket = intRet
End If
End If
End Function
Private Sub sckServer_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim intSocket As Integer
intSocket = FindAvailableSocket
If intSocket > 0 Then
sckServer(intSocket).Accept requestID
End If
End Sub
As for the question:
"how can i tell to the PC that even client2 1st connect the index is 2?"
I'm not sure exactly what you mean. Is "client2" a username or something? How are you determining it is client 2 and not client 1, 3, 4, etc.?
-
Apr 29th, 2008, 07:48 PM
#3
Thread Starter
Hyperactive Member
Re: winsock again
Code:
Private Sub sckServer_DataArrival(Index As Integer, ByVal bytesTotal As Long)
sckServer(Index).GetData strServerReceived
Select Case (strServerReceived)
Case "PC1"
Index = 1
idxPC = Index
cmdPC(Index).ToolTipText = "Connected"
If sMode = "start" Then
imgPC(Index).Picture = imgGreen
Else
imgPC(Index).Picture = imgStandby
End If
Case "PC2"
Index = 2
idxPC = Index
cmdPC(Index).ToolTipText = "Connected"
If sMode = "start" Then
imgPC(Index).Picture = imgGreen
Else
imgPC(Index).Picture = imgStandby
End If
i want to view disconnected image on the server
for example i closed client1, since the sckserver_close
will base on index, it will close the last index and
it will show the disconnected image on client2, it should be client1
TNX
*****************
VB6,PHP,VS 2005
-
Apr 30th, 2008, 01:33 AM
#4
Re: winsock again
It looks to me as if the client that is sending "PC1" isn't the one you are expecting! I'd look into the client-side code! Where do you determine that string "PC1"?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Apr 30th, 2008, 02:16 AM
#5
Thread Starter
Hyperactive Member
Re: winsock again
i have an .INI file in the client, just set what client_name you would want,
meanwhile i have temporary solution, every event i just call below to refresh the connection.
"but i think there are still easiest way to solve this"
Code:
Sub REFRESH_SOCKET()
Dim ctr1 As Integer,ctr2 As Integer
On Error Resume Next
For ctr1 = 1 To a
cmdPC(ctr1).ToolTipText = "Disconnected"
sckServer(ctr1).Close
Next
iSockets = 0
'For ctr2 = 1 To a
' If cmdPC(ctr2).ToolTipText = "Disconnected" Then
'imgPC(ctr2).Picture = imgDC
' End If
'Next
call NO_OF_CONNECTIONS
*****************
VB6,PHP,VS 2005
-
Apr 30th, 2008, 03:21 AM
#6
Re: winsock again
Based on your statements and code, you have no real connection between the Index of the socket and the Number or Name of the Client. And I do believe you don't need that connection. Instead of hardcoding the connection of Client1 to the Socket Index 1 you should store which client is on which socket on the server side when the connection is made!
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
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
|