Unreliable VB.NET Sockets

B

Buc

I have a small program that accepts a DGRAM UDP text packet (+-250 bytes
port 9001) and puts it in a list box. The network receive part acts BUGGY.
It acts the same way on more than one machine. It acted random.. SO..I used
the debugger, the callback fires and the first line of code breaks (the
receivefrom) and then the sub exits without executing the remaining lines of
code ?? The next packet may do the same thing OR the sub may finish the
remaining lines of code correctly and actually stick the text packet in the
listbox. I send one packet at a time for test and verify with a sniffer. I
added the try / catch later to see if there was some MYSTERY error but it
didn't catch anything either. (An Exception or a SocketException) Here's the
code...
Dim rxSocket As Socket = Nothing
Dim RecvBytes(1000) As Byte
Dim RemoteEP As New IPEndPoint(0, 0)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
rxSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)
RemoteEP = New IPEndPoint(IPAddress.Any, 9001)
rxSocket.Bind(RemoteEP)
rxSocket.BeginReceiveFrom(RecvBytes, 0, RecvBytes.Length,
SocketFlags.None, RemoteEP, New AsyncCallback(AddressOf OnReceive), Nothing)
End Sub
Public Sub OnReceive(ByVal ar As IAsyncResult)
Dim iBytes As Integer
Try
iBytes = rxSocket.ReceiveFrom(RecvBytes, 0, RecvBytes.Length,
SocketFlags.None, RemoteEP) <<BREAKPOINT HERE and then it exits sub
SOMETIMES
Catch e As SocketException <<no Socket exception or other
exception occurs
MsgBox(e.Message)
Exit Sub
End Try
ListBox1.Items.Add(Encoding.UTF8.GetString(RecvBytes))
iBytes = rxSocket.EndReceiveFrom(ar, RemoteEP)
rxSocket.BeginReceiveFrom(RecvBytes, 0, RecvBytes.Length,
SocketFlags.None, RemoteEP, New AsyncCallback(AddressOf OnReceive), Nothing)
End Sub

Please Help, I don't know what else to except go back and use the MS Winsock
control with dot net somehow if I can't get dotnet SOCKET stuff to work
reliably..
Buc
 
K

Ken Tucker [MVP]

Hi,

Try using a variable instead of using new in
rxSocket.beginreceievefrom.

Dim myCallback as New AsyncCallback(AddressOf OnReceive)
rxSocket.BeginReceiveFrom(RecvBytes, 0, RecvBytes.Length,
SocketFlags.None, RemoteEP, myCallback, Nothing)

Check out indy an open source dotnet sockets class.
http://www.indyproject.org/Indy.html

Ken
 
B

Buc

Never Mind, I took the RECEIVEFROM line out and it started getting every
packet, Its not needed I guess, any the data is already in Recvbytes buffer
when callback OnReceive fires..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top