hello!
anybody can help me? i want to send my data to all clients and test if its match then close the client but i cannot figure out why it is no sending.
'==== server side ====
Code:
Private Sub cmdPC_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
cmdPC(Index).SetFocus
sClientName = cmdPC(Index).Caption
PopupMenu mnu
End If
End Sub
'**** this is my problem
Private Sub mnuClose_Click()
Dim i As Integer
For i = 1 To iSockets
If Socket(i).State = sckConnected Then
Socket(i).SendData sClientName
cmdPC(i).Enabled = False
Else
MsgBox "client is not connected"
End If
Next
End Sub
Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
On Error GoTo err
sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).RemoteHostIP
'If Index = 0 Then
List1.AddItem (sServerMsg)
sRequestID = requestID
iSockets = iSockets + 1
lblConnections.Caption = iSockets
Load Socket(iSockets)
'Load Socket(Socket.UBound + 1)
Socket(iSockets).LocalPort = 1007
Socket(iSockets).Accept requestID
Label1.Caption = ""
Exit Sub
err:
iSockets = iSockets - 1
Label1.Caption = err.Description + "-" + sClientName
'==== client side ====
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
Winsock1.GetData sData, vbString
'Label1.Caption = sData
txtPrice.Text = sData
Label3.Caption = "Received Data"
shpGo.Visible = True
shpWait.Visible = False
shpError.Visible = False
If sData = txtName.Text Then
End
ElseIf sData = "Disconnected" Then
Winsock1.Close
Label4.Caption = "Disconnected"
End If
End Sub
sometimes server sends but in the wrong client (las client connected)
like i will right click btn(2) to send data to client2 but then it will display
on cilent1.
thank you doog... sorry its my 1st time in winsock.
Private Sub mnuClose_Click()
Dim i As Integer
For i = 1 To iSockets
If Socket(i).State = sckConnected Then
Socket(i).SendData sClientName
cmdPC(i).Enabled = False
Else
MsgBox "client is not connected"
End If
Next
End Sub
yes you're right after i install sp6, it is now able to send message to where client
i want...
i copy this code and successfully exit client2 but when i will close client3 array2 doesn't exist (because i already closed)
sorry to disturbed you...
Private Sub mnuClose_Click()
Dim i As Long
For i = 1 To iSockets
If Socket(i).State = sckConnected Then
Socket(i).SendData sClientName
cmdPC(idx).Enabled = False
Else
MsgBox "client is not connected"
End If
Next
End Sub
You could check cmdPC(i) first and skip if it's already False (ie the Socket is already closed)
Code:
Private Sub mnuClose_Click()
Dim i As Long
For i = 1 To iSockets
If cmdPC(i).Enabled = True Then
If Socket(i).State = sckConnected Then
Socket(i).SendData sClientName
cmdPC(i).Enabled = False
Else
MsgBox "client is not connected"
End If
End If
Next
End Sub
Dim i As Long
'For i = 1 To iSockets
If cmdPC(idxPC).Enabled = True Then
If Socket(idxSocket).State = sckConnected Then
Socket(idxPC).SendData sClientName
cmdPC(idxPC).Enabled = False
Else
MsgBox "client is not connected"
End If
End If
'Next
i removed the for loop to make it work to close the exact client
but when i open again the client (that i closed) it says "obejct already loaded"
huhuhu... pls. help again.
Private Sub mnuClose_Click()
Dim i As Long
For i = 1 To iSockets
If cmdPC(i).Enabled = True Then
If Socket(i).State = sckConnected Then
Socket(i).SendData sClientName
cmdPC(i).Enabled = False
Else
MsgBox "client is not connected"
End If
End If
Next
End Sub
hello again!
about the code: i have 3 clients open and if i clickd btn3 to close client3
the data sends only to clien1 and disabled btn1 which is wrong.
(how to send to all clients?)
Private Sub mnuClose_Click()
Dim i As Long
For i = 1 To iSockets
If Socket(i).State = sckConnected Then
If cmdPC(i).Enabled = True Then
Socket(i).SendData sClientName
cmdPC(i).Enabled = False
Else
MsgBox "client is not connected"
End If
End If
Next i
End Sub
Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Socket(Index).GetData strReceived
strSplit = Split(strReceived, "|")
sClientName = strSplit(0)
strIP = strSplit(1)
strHostName = strSplit(2)
txtClientName.Text = sClientName
txtHostName.Text = strHostName
txtIP.Text = strIP
Select Case (sClientName)
Case "PC1"
Index = 1
cmdPC(Index).Enabled = True
Case "PC2"
Index = 2
cmdPC(Index).Enabled = True
Case "PC3"
Index = 3
cmdPC(Index).Enabled = True
End Select
Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
On Error GoTo err
sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).RemoteHostIP
If Index = 0 Then
List1.AddItem (sServerMsg)
sRequestID = requestID
iSockets = iSockets + 1
lblConnections.Caption = iSockets
Load Socket(iSockets)
'Load Socket(Socket.UBound + 1)
Socket(iSockets).LocalPort = 1007
Socket(iSockets).Accept requestID
Label1.Caption = ""
End If
Exit Sub
err:
Socket(0).Close
Socket(0).LocalPort = 0
iSockets = iSockets - 1
Label1.Caption = err.Description + "-" + sClientName
End Sub
Private Sub cmdPC_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = vbRightButton Then
cmdPC(Index).SetFocus
sClientName = cmdPC(Index).Caption
idxPC = Index
'idxSocket = Index
PopupMenu mnu
End If
End Sub
Private Sub mnuClose_Click()
Dim i As Long
For i = 1 To iSockets
If Socket(i).State = sckConnected Then
If cmdPC(i).Enabled = True Then
Socket(i).SendData sClientName
cmdPC(i).Enabled = False
Else
MsgBox "client is not connected"
End If
End If
Next
End Sub
client side
Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
Winsock1.GetData sData, vbString
'Label1.Caption = sData
txtPrice.Text = sData
Label3.Caption = "Received Data"
shpGo.Visible = True
shpWait.Visible = False
shpError.Visible = False
If sData = txtName.Text Then
End
ElseIf sData = "Disconnected" Then
Winsock1.Close
Label4.Caption = "Disconnected"
End If
End Sub
if i will clicked btn(3) the data will send to client1 (it should be to all clients)
the index is wrong?
pls.... anybody who can debug? i just need help
the problem:
1. 3 clients are opened
2. close client1
3. open again client1
4. close again client1
5. the message will recieved on client3 (it should be client1)
6. i can't do for loop because if client1 is closed then open again
in the for loop my x=1 remember client1 is already closed and reopen
You need to get your code straightened out. I'm not going to go through all of your code re-doing your stuff but Here is one problem you are doing.
In the below sub you are using a user defined variable idxSocket to establish which client disconnected, and by the way, the value in that variable is 0 (zero) when it should be 1 (one). It is better to use the already supplied variable Index insted of your variable. The Index is there for that purpose. A good rule of socket array programming is to rely on the Index value as you enter each of these events instead of your own. If you feel you must use your own indexing then you need to do a better job of keeping track of the variable.
Code:
Private Sub socket_Close(Index As Integer)
sServerMsg = "Connection closed: " & Socket(idxSocket).RemoteHostIP
List1.AddItem (sServerMsg)
Socket(idxSocket).Close
Unload Socket(idxSocket)
cmdPC(idxPC).Enabled = False
iSockets = iSockets - 1
lblConnections.Caption = iSockets
End Sub
Last edited by jmsrickland; Feb 27th, 2008 at 12:54 AM.
ok, i will re-code again into simplier first
how about the request id? how did the server knew that this is the request ID for client1 or client2 (server generates the reqeuest id?)
and why is it dynamic?
or....
if you can send me sample program server and 3 clients send data to all
and exit client2 the open again cilent2, exit client2 again
lasltly... client1 and client3 are open, now close client3 (can't send to all clients because client2 is closed so, for loop 1 to 3 will have error)
ok, i will re-code again into simplier first
how about the request id? how did the server knew that this is the request ID for client1 or client2 (server generates the reqeuest id?)
and why is it dynamic?
LIke I said, all you have to do is to use the Index when clients are communicating with the server and that also means when they close. But when they re-connect it becomes a new client
For example:
Clinet 1 connects - Index = 1 - Server loads socket index 1
Client 2 connects - Index = 2 - Server loads socket index 2
Client 3 connects - Index = 3 - Server loads socket index 3
Client 1 closes connection - Sever unloads socket index 1
Now only sockets 2 and 3 are active
Client 1 re-connects - Server has no way to know it is client 1 re-connecting so it loads a new socket with index of 4 and now old client 1 becomes new client 4. You can't depend on other methods for keeping track of who is doing what so you just allow clients to connect, disconnect, and reconnect giving each client a new index whenever they re-connect. When clients communicate with the server always use the Index value that is pre-loaded for you by the socket system. You can always depend on the Index. Just think if you had to write a client/server application that had hundreds of clients.
hello! thank you for your advise,
and starting from scratch i've ended like this: added try and catch error
also in for loop just to make sure that data from server will send to all open sockets(clients)
***i can now proceed to the next stage of my project.
tnx again!
Code:
Private Sub Socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
On Error GoTo err
sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).RemoteHostIP
If Index = 0 Then
'List1.AddItem (sServerMsg)
sRequestID = requestID
iSockets = iSockets + 1
lblConnections.Caption = iSockets
Load Socket(iSockets)
'Load Socket(Socket.UBound + 1)
Socket(iSockets).LocalPort = 1007
Socket(iSockets).Accept requestID
Label1.Caption = ""
End If
Exit Sub
err:
'If err.Number = 360 Then
Unload Socket(iSockets)
iSockets = iSockets - 1
'End If
' Label1.Caption = CStr(err.Number) + " " + err.Description + "-" + sClientName
End Sub
OK. My advise to you is that you should use iSockets only in the ConnectionRequest Event. All other events you should use the Index from the argument list of the Procedure.
Also Socket(iSockets).LocalPort = 1007 is not necessary.
Also, I think your error trap is wrong. If you get an error it means you didn't load a socket so you shouldn't unload a socket cause you will get another error while you are in your error trap.
Code:
err:
'If err.Number = 360 Then
Unload Socket(iSockets) <---------- NO
iSockets = iSockets - 1 <----------- YES
'End If
' Label1.Caption = CStr(err.Number) + " " + err.Description + "-" + sClientName
End Sub
Also Socket(iSockets).LocalPort = 1007 is not necessary.
>> why it is not necessary?
>> and in my close_socket i didn't use the word index instead isockets
Private Sub Socket_Close(Index As Integer)
sServerMsg = "Connection closed: " & Socket(iSockets).RemoteHostIP
List1.AddItem (sServerMsg)
Socket(iSockets).Close
Unload Socket(iSockets)
cmdPC(iSockets).Enabled = False
iSockets = iSockets - 1
lblConnections.Caption = iSockets
cmdRefresh_Click
End Sub
i have an error but my aim of closing the target client is successfully close
ok i will study further what do you mean.
Think about it. It's already there and besides what purpose will it serve? It won't do any harm it just isn't necessary.
>> and in my close_socket i didn't use the word index instead isockets
In your Close Event you are assuming that the client that disconnect has the Index val equal to iSockets and that may not always be the case. I told you in an earlier reply it is considered bad programming and it is unreliable as well. The Index is your only guarantee which clinect has disconnected. You may get away with it once or twice but it will bite you in the &*# someday. How do you know that iSockets will have a value of, say 3, when client 3 disconnects or it will have a value of 2 when client 2 disconnects?
Client 1 disconnects - in your Close Event you use iSockets, which should have a value of 3 in it and assume it is the one for Client 1 who disconneced so you in essence closed the socket for Client 3 instead of Client 1.
Last edited by jmsrickland; Feb 28th, 2008 at 12:29 AM.
>>i removed below can't close clients
Unload Socket(iSockets) <---------- NO
***anyways in previous reply i already meet my target client to close
and sending data from the server to all clients (even if theres an error its ok for now)