473,657 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need Help with this Async code

I'm trying to use Async Socket calls to create a tcp server. It goes into
the listening correctly, but when I connect, it crashes with the following
exception:

An unhandled exception of type 'System.Invalid OperationExcept ion' occurred
in system.dll
Additional information: AcceptCallback
There is no source code available for the current location.

Here's the code. Hopefully someone can help me out.
-----------------------------------------------------------
Private sck As Socket 'Class level

Private Sub btnStart_Click( ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles btnStart.Click
Dim addr As IPAddress = IPAddress.Parse ("0.0.0.0")
Dim ep As New IPEndPoint(addr , CInt(txtPort.Te xt))
Dim sck As New Socket(AddressF amily.InterNetw ork, _
SocketType.Stre am, ProtocolType.Tc p)
sck.Bind(ep)
sck.Listen(2)
sck.BeginAccept (AddressOf AcceptCallback, Nothing)
lblStatus.Text = "Listening"
End Sub

Private Sub AcceptCallback( ByVal ar As IAsyncResult)
sck.EndAccept(a r) 'I believe this is my problem
Dim bytes(4095) As Byte
sck.BeginReceiv e(bytes, 0, bytes.Length, SocketFlags.Non e, _
AddressOf ReceiveCallback , bytes)
End Sub

Private Sub ReceiveCallback (ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncS tate, Byte())
Dim numbytes As Int32 = sck.EndReceive( ar)
If numbytes = 0 Then
Else
End If
End Sub
Nov 21 '05 #1
1 1291
On 2005-01-30, Terry Olsen <to******@hotma il.com> wrote:
I'm trying to use Async Socket calls to create a tcp server. It goes into
the listening correctly, but when I connect, it crashes with the following
exception:

An unhandled exception of type 'System.Invalid OperationExcept ion' occurred
in system.dll
Additional information: AcceptCallback
There is no source code available for the current location.

Here's the code. Hopefully someone can help me out.
-----------------------------------------------------------
Private sck As Socket 'Class level

Private Sub btnStart_Click( ByVal sender As System.Object, _
ByVal e As System.EventArg s) Handles btnStart.Click
Dim addr As IPAddress = IPAddress.Parse ("0.0.0.0")
Dim ep As New IPEndPoint(addr , CInt(txtPort.Te xt))
Dim sck As New Socket(AddressF amily.InterNetw ork, _
SocketType.Stre am, ProtocolType.Tc p)
sck.Bind(ep)
sck.Listen(2)
sck.BeginAccept (AddressOf AcceptCallback, Nothing)
lblStatus.Text = "Listening"
End Sub

Private Sub AcceptCallback( ByVal ar As IAsyncResult)
sck.EndAccept(a r) 'I believe this is my problem
Dim bytes(4095) As Byte
sck.BeginReceiv e(bytes, 0, bytes.Length, SocketFlags.Non e, _
AddressOf ReceiveCallback , bytes)
End Sub

Private Sub ReceiveCallback (ByVal ar As IAsyncResult)
Dim bytes() As Byte = CType(ar.AsyncS tate, Byte())
Dim numbytes As Int32 = sck.EndReceive( ar)
If numbytes = 0 Then
Else
End If
End Sub


Terry...

Your trying to read off your server socket. When a socket connection is
accepted, the client is actually connected on a new socket so that the
server socket goes back to listening.

You should really be doing something like:

Private Sub AcceptCallback (ByVal ar As IAsyncResult)
Dim clt As Socket = sck.EndAccept(a r)
...
clt.BeginReciev e (....)
sck.BeginAccept (...)
End Sub

You might try looking through the async socket examples here:
http://msdn.microsoft.com/library/de...consockets.asp

HTH
--
Tom Shelton [MVP]
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1829
by: Passynkov, Vadim | last post by:
I am using Asynchronous Query Processing interface from libpq library. And I got some strange results on Solaris My test select query is 'SELECT * from pg_user;' and I use select system synchronous I/O multiplexer in 'C' The first test sends 10000 select queries using 10 nonblocking connections to database ( PQsendQuery ). The second test sends the same 10000 select queries using 1 connection ( PQexec ).
11
1758
by: Glen Wolinsky | last post by:
This is my first attempt as asynchronous processing. I have created a small test app as proof of concept, but I am having one proglem. In the example (code listed below), my callback routine has two problems: 1. It runs TWICE; and 2. While it seems to update the web page controls, the results never show up on the page. I am using delegates per a couple of examples I found on MSDN and elsewhere.
20
2866
by: Jason Dravet | last post by:
Some time ago I wrote an application that controls the projectors in our classrooms(turn on, turn off, and volume control). This application is written in VB.net 2003. It was working perfectly until XP SP2 was installed on the PCs. Now when the power on or power off button is pressed the following message is displayed: An unhandled exception has occurred in your application. If you click Continue, the application will ignore this...
2
1247
by: ITALstudio s.r.l. | last post by:
Hy, I make an async call, using delegate and BeginInvoke. When the async call is completed, the callback procedure is executed two times. I debugged the code and I'm sure: I make one async call, but the callback procedure is executed two times. I use the Framework 1.1 SP1.
2
1157
by: Lespaul36 | last post by:
I have tried many things. I still have not found anything that seems to work. Here is the portions of my code that deal with the listening socket maybe you have a better idea? The form still seems to hang. I got it to not hang if I used a loop with a application.doevents, but it was not CPU friendly. I had a suggestiong using channels, but haven't found much to help me or to find the advantage of it. It is just for chat on a LAN. I...
0
2147
by: dnphamus13 | last post by:
I'm new to this and drowning right now. I would like to put my database online for viewing. I managed to do the filtering but i need to do PAGING as the XML doc get bigger. From what i understand this is what I need to do; transform the current XML doc with the filterring xsl, then somehow add the paging xsl. That's the part i'm having trouble with. I'm using JavaScript to apply the filterring XSL to XML doc. Filterring is based on what...
3
1835
by: christopher.davidson | last post by:
Hello, I am working with XML files and utilizing Array functions to take the XML data and combined it with some html code to display a particular page. The process currently works like so: 1.) Request an Async request of the XML 2.) Once complete, parse the appropriate XML data to parse
1
20608
by: Ryan Liu | last post by:
Hi, I have a 100 clients/ one server application, use ugly one thread pre client approach. And both side user sync I/O. I frequently see the error on server side(client side code is same, but I don't see the error): "System.IO.IOException: Unable to read data from the transport connection:A blocking operation was interrupted by a call to WSACancelBlockingCall"
13
1764
by: Alexander Gnauck | last post by:
Hello, while using async sockets I ran into a strange problem which occurs only on some machines. I wrote a small demo application which can be used to reproduce the problem. You can download it from here: http://alex.ag-software.de/SocketTest.zip If you press the "Connect in new Thread" button from the test application you may be able to cause the System.IO.IOException: Unable to write data to the transport connection. While the...
0
8413
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.