VB socket non-blocking error

smotbd
smotbd used Ask the Experts™
on
I have a simple Visual Basic program that opens a file and then copies it over an UDP socket to a destination port. BUT i get  an error saying:
   Run-time error '10035'
   Socket is non-blocking and the specified operation will block.

How can I solve this problem. Can I set the socket to be blocking instead of non-blocking maybe??
Please help asap!! Thank you.

The code is shown below:


Private Sub Form_Load()
file.Text = "C:\b.txt"
remote_ip.Text = "193.63.128.54"
remote_port.Text = "5000"
local_port.Text = "5000"
End Sub

Private Sub sck_open_cmd_Click()
UDPsock1.Protocol = sckUDPProtocol
UDPsock1.RemoteHost = remote_ip.Text '"193.63.131.67"    'remote_ip.Text 'ip to send to
UDPsock1.RemotePort = remote_port.Text '"5000" '"6000"    'port to talk to????
UDPsock1.Bind local_port.Text '"7000"                                   'local port
End Sub

Private Sub sck_close_cmd_Click()
UDPsock1.Close
End Sub

Private Sub send_cmd_Click()
Dim data As String
Open "C:\b.txt" For Input As #1   'file.Text '"E:\test.txt" 'type e.g. E:\test.txt into file TextBox
    Do Until EOF(1)
    Line Input #1, data
         UDPsock1.SendData data
    Loop
Close #1
End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Hi,

This might be of help (Blocking vs. Non-Blocking Sockets):
http://www.developerfusion.co.uk/show/28/8/

Regards,

Author

Commented:
I have read that before and yes, it helps understand what the problem is.
I think, in my case, I am reading a chunk of a file and sending it via the udp socket and this should be done repeatadly until the whole file is transferred. However, after a few read and send, the error (described in 1st message) pops up, thus suggesting that the buffer is to full I guess. I.e the code reading the data is to quick incomparison with the socket handling the data transfer.
....but how can I check if the socket buffer is full if that is the buffer I need to look at?

thanks
Commented:
The winsock object is asynchronous by nature, which means you can't change it to a blocking socket.  You can use the API to do this, but it may be simpler to structure your program for asynchronous communications.

UDP is connectionless and best for sending small amounts of data.  You are correct that this error can mean that the data is queued in the "send" buffer, and you cannot add any more at this time.  The thing to do would be to trap the error and try again after a short pause.

This may not seem ideal, but it's hard to make UDP act in a reliable manner since it isn't designed for communications that have to be reliable.

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub send_cmd_Click()
    Dim data As String
    Open "C:\b.txt" For Input As #1   'file.Text '"E:\test.txt" 'type e.g. E:\test.txt into file TextBox
    Do Until EOF(1)
        Line Input #1, data
        On Error Resume Next
        If Len(data) Then
            Sleep 100  'Just a little pause to help things along
            Debug.Print data
            UdpSock1.SendData data
            Do While Err.Number = 10035
                Sleep 100
                Err.Clear
                UdpSock1.SendData data
            Loop
        End If
    Loop
    Close #1
End Sub  





Forced accept.

Computer101
EE Admin

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial