Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Socket works once - cannot reproduce on step thru

Status
Not open for further replies.

Sypher2

Programmer
Oct 3, 2001
160
0
0
US
In a socket class I have a method which essentially does this:
[ul]
[li]Declares a new socket[/li]
[li]Does a BeginConnect to connect to server (waits for connection)[/li]
[li]Sends data to the server (waits for send to complete)[/li]
[li]Receives a response from the server (waits for response to complete)[/li]
[li]Shuts down the socket and closes it[/li]
[/ul]
This works the first time I call the method. However, subsequent calls do not work. I get an error when data is sent: "A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"

Sometimes the send will go through but I'll get the above error on the recieve part.

Now the strange thing is that if I step thru the code using F8, it works every time. When I run the code normally it will give me the error.

I don't know why the socket is disconnected when the connection part always works nor do I understand why I can step thru the code and it works but running normally does not.

Any ideas?
 
It sounds like your end connect isn't firing. This will leave the connection open. Then, when you attempt to open it again you will get errors. Post your code so we can see what you have.
 
I figured it out.

I have some ManualResetEvent variables. One for connecting, one for sending, and one for receiving. When each of these operations completed it would do a Set() to signal the main thread it was done with that part.

I wasn't re-initializing these ManualResetEvent variables. The second time a connect/send/receive was attempted the main thread would be immediately signaled and would move on before the operation actually finished.

Gotta love asynchronous stuff.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top