470,632 Members | 2,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

Sockets: BeginReceiveFrom callback not working (VB.NET)

1
Just looking for some insight as to why the callback "BeginReceiveFromCallback" is not being called in my "Receive" Subroutine below (when I call BeginReceiveFrom). I am trying to read data asynchronously using UDP. I have implemented Send and Connect subroutines in a similar way, and they both seem to work. When I put a breakpoint in the callbacks for Send and Connect, I hit them, and I can see the results I want. However, when I put a breakpoint in the BeginReceiveFromCallback, I never hit it. The compiler just seems to skip over it. Thanks!

I initialize the UDP connection in the main form...


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Dim CLIENT_SOCKET As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
  4.  
  5.  Dim int_PORT_NUM As Integer
  6.  Dim IP_ADDRESS As System.Net.IPAddress
  7.  Dim remoteEP As System.Net.IPEndPoint
  8.  Dim UDP_CONNECTION As AsynchronousClient
  9.  
  10. 'initialize socket:
  11.  
  12. 'Establish local endpoint for the socket:
  13. ipHostInfo = Dns.Resolve(Dns.GetHostName)
  14. ipAddress = ipHostInfo.AddressList(0)
  15. localEndPoint = New IPEndPoint(ipAddress, 0)
  16.  
  17. CLIENT_SOCKET.SetSocketOption(Sockets.SocketOptionLevel.Socket, Sockets.SocketOptionName.ReceiveBuffer, 65536)
  18.  
  19. bind the socket to the local endpoint:
  20. CLIENT_SOCKET.Bind(localEndPoint)
  21.  
  22.        'Get Remote Host:
  23.  
  24.         str_IP_ADDRESS = "135.202.8.146"
  25.         IP_ADDRESS = IP_ADDRESS.Parse(str_IP_ADDRESS)
  26.  
  27.         'get port number:
  28.         int_PORT_NUM = 0
  29.  
  30.         'assign remote endpoint:
  31.         remoteEP = New System.Net.IPEndPoint(IP_ADDRESS, int_PORT_NUM)
  32.  
  33.         'create new ethernet connection:
  34.         UDP_CONNECTION = New AsynchronousClient(remoteEP)
  35.  
  36.         'connect socket to RemoteEP:
  37.         UDP_CONNECTION.Connect()
  38.  
  39.         'begin receive:
  40.         UDP_CONNECTION.Receive()
  41.  
where the AsynchronousClient class is...


Expand|Select|Wrap|Line Numbers
  1. Imports System 
  2. Imports System.Net 
  3. Imports System.Net.Sockets 
  4. Imports System.Threading 
  5. Imports System.Text 
  6.  
  7. Public Class AsynchronousClient 
  8.  
  9.  
  10.     'CLASS VARIABLES: 
  11.     '=============== 
  12.  
  13.  
  14.     'OBJECTS: 
  15.  
  16.     Private REMOTE_ENDPOINT As IPEndPoint 
  17.     Private CLIENT_SOCKET As Socket 
  18.  
  19.  
  20.     Public Sub New(ByVal A_REMOTE_ENDPOINT As IPEndPoint) 
  21.  
  22.  
  23.         Dim ipHostInfo As IPHostEntry 
  24.         Dim ipAddress As IPAddress 
  25.         Dim localEndPoint As IPEndPoint 
  26.  
  27.         'Create new UDP socket: 
  28.         CLIENT_SOCKET = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) 
  29.  
  30.         'Establish local endpoint for the socket: 
  31.         ipHostInfo = Dns.Resolve(Dns.GetHostName) 
  32.         ipAddress = ipHostInfo.AddressList(0) 
  33.         localEndPoint = New IPEndPoint(ipAddress, 0) 
  34.  
  35.         'bind the socket to the local endpoint: 
  36.         CLIENT_SOCKET.Bind(localEndPoint) 
  37.  
  38.         'Assign remote server information: 
  39.         REMOTE_ENDPOINT = A_REMOTE_ENDPOINT 
  40.  
  41.  
  42.     End Sub 
  43.  
  44.  
  45. #Region "Receive" 
  46.  
  47.     Public Sub Receive() 
  48.  
  49.  
  50.         Try 
  51.  
  52.  
  53.             ' Create the BRFStateObject: 
  54.             Dim BRFStateObject As New BRFStateObject 
  55.  
  56.             'assign client socket: 
  57.             BRFStateObject.PASS_CLIENT_SOCKET = Me.CLIENT_SOCKET 
  58.  
  59.             Dim tempRemote_EP As EndPoint 
  60.             tempRemote_EP = CType(REMOTE_ENDPOINT, EndPoint) 
  61.  
  62.             ' Begin asynchronously receiving the data from the remote device. 
  63.             Me.CLIENT_SOCKET.BeginReceiveFrom(   BRFStateObject.byte_BUFFER, 0, _ 
  64.                             BRFStateObject.int_BUFFER_SIZE, 0, tempRemote_EP, _ 
  65.                             AddressOf BeginReceiveFromCallback, BRFStateObject) 
  66.  
  67.         Catch e As Exception 
  68.             Console.WriteLine(e.ToString()) 
  69.         End Try 
  70.  
  71.     End Sub 'Receive 
  72.  
  73. #End Region 
  74.  
  75. #Region "BeginReceiveFromCallback" 
  76.  
  77.     Private Sub BeginReceiveFromCallback(ByVal ar As IAsyncResult) 
  78.  
  79.  
  80.         'VARIABLES: 
  81.         '========== 
  82.  
  83.         Dim int_NUM_BYTES As Integer 
  84.  
  85.  
  86.         'SUB: 
  87.         '==== 
  88.  
  89.  
  90.         Try 
  91.  
  92.  
  93.             ' Close BeginReceiveFrom and get bytes 
  94.             int_NUM_BYTES = CLIENT_SOCKET.EndReceiveFrom(ar, REMOTE_ENDPOINT) 
  95.  
  96.             'convert ar to state object: 
  97.             byte_BUFFER = ar.AsyncState 
  98.  
  99.             If int_NUM_BYTES > 0 Then 
  100.  
  101.                 byte_BUFFER = ar.AsyncState 
  102.                 System.Console.WriteLine(byte_BUFFER(0)) 
  103.  
  104.                 Exit Sub 
  105.  
  106.             End If 
  107.         Catch e As Exception 
  108.             Console.WriteLine(e.ToString()) 
  109.         End Try 
  110.     End Sub 'BeginReceiveFromCallback 
  111.  
  112. #End Region 
  113.  
  114.  
  115.     Public Sub Connect() 
  116.  
  117.         Try 
  118.             Me.CLIENT_SOCKET.BeginConnect(REMOTE_ENDPOINT, AddressOf BeginConnectCallback, Me.CLIENT_SOCKET) 
  119.         Catch e As Exception 
  120.             Console.WriteLine(e.ToString()) 
  121.         End Try 
  122.  
  123.  
  124.     End Sub 
  125.  
  126.  
  127.     Private Shared Sub BeginConnectCallback(ByVal ar As IAsyncResult) 
  128.         Try 
  129.             ' Retrieve the socket from the state object. 
  130.             Dim client As Socket = CType(ar.AsyncState, Socket) 
  131.  
  132.             ' Complete the connection. 
  133.             client.EndConnect(ar) 
  134.  
  135.             Console.WriteLine("Socket connected to {0}", _ 
  136.                 client.RemoteEndPoint.ToString()) 
  137.  
  138.         Catch e As Exception 
  139.             Console.WriteLine(e.ToString()) 
  140.         End Try 
  141.     End Sub 'BeginConnectCallback 
  142.  
  143.     Public Sub Send(ByVal str_A_STRING As String) 
  144.  
  145.         Dim byteData As Byte() 
  146.  
  147.         ' Convert the string data to byte data using ASCII encoding. 
  148.         byteData = Encoding.ASCII.GetBytes(str_A_STRING) 
  149.  
  150.         ' Begin sending the data to the remote device. 
  151.         Me.CLIENT_SOCKET.BeginSendTo(byteData, 0, byteData.Length, 0, Me.REMOTE_ENDPOINT, _ 
  152.             AddressOf SendCallback, Me.CLIENT_SOCKET) 
  153.  
  154.     End Sub 'Send 
  155.  
  156.  
  157.     Private Shared Sub SendCallback(ByVal ar As IAsyncResult) 
  158.         Try 
  159.             ' Retrieve the socket from the state object. 
  160.             Dim client As Socket = CType(ar.AsyncState, Socket) 
  161.  
  162.             ' Complete sending the data to the remote device. 
  163.             Dim bytesSent As Integer = client.EndSend(ar) 
  164.             Console.WriteLine("Sent {0} bytes to server.", bytesSent) 
  165.  
  166.             ' Signal that all bytes have been sent. 
  167.             sendDone.Set() 
  168.         Catch e As Exception 
  169.             Console.WriteLine(e.ToString()) 
  170.         End Try 
  171.     End Sub 'SendCallback 
  172.  
  173.  
  174.  
  175. Public Class BRFStateObject 
  176.  
  177.     ' Client socket: 
  178.     Public PASS_CLIENT_SOCKET As Socket = Nothing 
  179.  
  180.     'Buffer Size: 
  181.     Public int_BUFFER_SIZE As Integer = 4096 
  182.  
  183.     ' Buffer: 
  184.     Public byte_BUFFER(4096 - 1) As Byte 
  185.  
  186.     'Recieved data string: 
  187.     Public sb As New StringBuilder 
  188.  
  189.  
  190. End Class 'BRFStateObject
Nov 1 '06 #1
0 3876

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Allen St.Clair | last post: by
10 posts views Thread by altaf.sunesara | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.