Hi, the bellow code doesn't send data to server, the server doesn't receive it. please give me a solution.
Private Sub Form_Unload(Cancel As Integer)
sockMain.SendData "Closing"
End Sub
Printable View
Hi, the bellow code doesn't send data to server, the server doesn't receive it. please give me a solution.
Private Sub Form_Unload(Cancel As Integer)
sockMain.SendData "Closing"
End Sub
Ideally you would probably write a handler for the Query_Unload event and have a Form-level Boolean like SentClosing.
In it you would check unloadmode, and if it is vbFormControlMenu and SentClosing is False, then do something like:
Then in your SendComplete handler you'd want something like:Code:If unloadmode = vbFormControlMenu And Not SentClosing Then
If sckMain.State = sckOpen Then
sckMain.SendData "Closing"
sckMain.Close
End If
SentClosing = True
cancel = True
End If
You'd probably also need to manage SentClosing a bit more, such as making sure it is always True until you open your connection (where you'd set it to False).Code:If SentClosing Then
Unload Me
Else
... other existing logic...
End If
What do you mean multiple winsocks? Do you have one client with multiple winsocks or multiple clients with one winsock each?
If it a single client with one socket connected to server then dilettante's method will work except I don't think you should close the socket immediately following the
sckMain.SendData "Closing"
'NO--->sckMain.Close
If you have one client with multiple sockets then isn't each socket indexed? If so, then it's the same code except you use the index to send the closing message and then in the SendComplete you use that index again to close that socket then check if that index is the last socket to send a closing message and if so then unload your Form.
No unfortunately my winsocks are not indexed. Now what?
It would be basically the same thing except each winsock will have it's own SendComplete event. Only in the last socket to send a closing message SendComplete event will you check for SentClosing.
Code:'
Dim SentClosing As Boolean
'
'
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If unloadmode = vbFormControlMenu And Not SentClosing Then
If sckAdam.State = sckOpen Or sckAdam.State = sckConnected Then
sckAdam.SendData "Closing"
End If
'
' etc, etc for all sockets
'
If sckZebra.State = sckOpen Or sckZebra.State = sckConnected Then
sckZebra.SendData "Closing"
End If
SentClosing = True
cancel = True
End If
End Sub
'
'
'
' First socket to close
'
Private Sub sckAdam_SendComplete()
If SentClosing Then
sckAdam.Close
Else
'... other existing logic...
End If
End Sub
'
' etc, etc
'
'
' Last socket to close
'
Private Sub sckZebra_SendComplete()
If SentClosing Then
sckZebra.Close
Unload Me
Else
'... other existing logic...
End If
End Sub
Yes, the details just need to be thought through. You manage the tracking of whether Query_Unload needs to send-and-close and when to finally do the final Form Unload that actually ends the program.
You can defer the Winsock Close until after the "closing" SendComplete. However calling the Close method doesn't shut a TCP connection down, it just terminates sending for your end of the two-way connection. The other end can still receive the in-transit data and even send you more data.
The real question is "Why bother?"
The program(s) at the other end(s) of your connection(s) get notified anyway. If the other program(s) is(are) VB6 using Winsock controls they'll get Close events.
There shouldn't be any reason for such a "Closing" message, which is why people seldom need to do all of this.
Very nice example thank you very much. But are you sure that if i send data through multiple sockets, the vb send them in order like not messing the order?
Like if i do....
Winsock1.send data
winsock2.send data
winsock3.send data
so here the winsock3 is the last socket which will complete the sending process?