Re: [Winsock UDP] Why .Close to avoid Err 40020?
- From: Gilles Ganault <nospam@xxxxxxxxxx>
- Date: Mon, 09 Jul 2007 19:13:35 +0200
On Fri, 6 Jul 2007 15:52:21 -0600, "Lance Wynn"
<LanceWynn@xxxxxxxxxxxxxxxx> wrote:
You cannot set the Protocol after the connection is open (Which in the case
of UDP probably happens when you send your first data).
Thanks guys. It helped :-)
The next step, is having...
1 the server wait on UDP 5555
2. the client broadcast a string on UDP 5555 with remote IP
255.255.255.255 asking for the server's IP
3. the server answer this with its local IP
The goal is obviously to locate a server without having to set its IP
address on each client. I'll figure out later how to handle the case
where the server can't use the default UDP port because it's already
in use.
With the following code, it works the first time, but if I click on
the client's Command1 or even close/restart the client, the server
does receive the broadcast but doesn't send its IP address:
====== SERVER ============
Option Explicit
Private Sub Form_Load()
ServerSock.Protocol = sckUDPProtocol
ServerSock.LocalPort = 5555
ServerSock.Bind
End Sub
Private Sub ServerSock_DataArrival(ByVal bytesTotal As Long)
Dim strDataIn As String
ServerSock.GetData strDataIn
Label1.Caption = strDataIn + vbCrLf + ServerSock.RemoteHostIP +
vbCrLf + _
Str(ServerSock.RemotePort) + vbCrLf + Time$
'After first, successful transmission, server stops TXing!
ServerSock.SendData ServerSock.LocalIP
'Required, or server will not answer subsequent requests with
'Err 10054 "The connection is reset by remote side"
ServerSock.Close
'Makes no difference
'ServerSock.Protocol = sckUDPProtocol
'ServerSock.LocalPort = 5555
ServerSock.Bind
End Sub
====== CLIENT ============
Option Explicit
Private Sub ClientSock_DataArrival(ByVal bytesTotal As Long)
Dim sStuff As String
ClientSock.GetData sStuff
'Needed? DoEvents
Label1.Caption = "Remote address " & sStuff & vbCrLf & Time$
End Sub
Private Sub Command1_Click()
'Required to avoid error 40020: "Invalid operation at current
state"
If ClientSock.State <> sckClosed Then
ClientSock.Close
End If
ClientSock.Protocol = sckUDPProtocol
ClientSock.RemotePort = 5555
ClientSock.RemoteHost = "255.255.255.255"
ClientSock.SendData "SERVER_IP_ADDRESS_PLEASE"
End Sub
Private Sub Form_Load()
ClientSock.Close
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
ClientSock.Close
End Sub
====== END ============
Does the server need to be reset with something else besides what I
commented out because it made no difference?
I remember there was a lot of useful infos on www.vbip.com, but the
site is gone and his author Oleg Gdalevich's whois e-mail doesn't work
:-(
Thanks for any help.
.
- Follow-Ups:
- Re: [Winsock UDP] Why .Close to avoid Err 40020?
- From: Alan McGinlay
- Re: [Winsock UDP] Why .Close to avoid Err 40020?
- From: Lance Wynn
- Re: [Winsock UDP] Why .Close to avoid Err 40020?
- References:
- [Winsock UDP] Why .Close to avoid Err 40020?
- From: Gilles Ganault
- Re: [Winsock UDP] Why .Close to avoid Err 40020?
- From: Lance Wynn
- [Winsock UDP] Why .Close to avoid Err 40020?
- Prev by Date: Opening PDF with F1
- Next by Date: Re: portable apps with VB6
- Previous by thread: Re: [Winsock UDP] Why .Close to avoid Err 40020?
- Next by thread: Re: [Winsock UDP] Why .Close to avoid Err 40020?
- Index(es):
Relevant Pages
|