473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 4225

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

Similar topics

1
by: VincentWong | last post by:
hi all, We're getting a security exception (An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll) on a asychronous...
3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
1
by: Claire | last post by:
Hi Im writing an application using the above controls in blocking mode. Ive not used them before and I'm more used to asynchronous socket programming utilizing socket events. As there are no...
1
by: Sougato Das | last post by:
Does anyone know any good places for information on sockets programming using VB.NET. I know VB.NET fairly well, but I know very little about sockets. Thanks.
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
0
by: Allen St.Clair | last post by:
I'm tried lots of times to make it working but failed. VB 2005: Sub Main() Dim osck As Net.Sockets.Socket osck = New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork,...
10
by: altaf.sunesara | last post by:
Hello ! I have some devices which communicate trough LAN to the server the old form i was using was FTP as a communication between Server and Client i have decided to use personal TCP sockets...
7
by: luvwknd | last post by:
Hi all, I am attempting to utilize MS's article I.D. #828993 found at: http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B828993 In an effort to create a GUI Ping utility that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.