Created
June 1, 2011 03:16
-
-
Save jasonsirota/1001730 to your computer and use it in GitHub Desktop.
BeginSend Socket Test Mon
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
I have a piece of .NET code that uses Asynchronous sockets that I want to cross | |
compile and run on Mono, however, when I attempt to run it, the AsyncCallback for | |
the BeginSend never gets called. I've boiled down the problem to a very simple | |
example. The example does not ever hit the callback, I have tried the following | |
configurations: | |
OS: Ubuntu 10.10 (Maverick Meerkat) | |
Mono 2.8.2 using Nathan Bridgewater's install shell script | |
Mono 2.10.2 using Nathan Bridgewater's updated shell script | |
Both using MonoDevelop F5 and compiling in MonoDevelop but running in the console | |
The behavior of the mono versions is slightly different: | |
- Using 2.8: The console hangs, never invokes the callback and never exits | |
- Using 2.10: The console exits but does not invoke the callback | |
In both cases, the Socket error code reports Success. | |
Note: This is a client, not a server. | |
*/ | |
using System; | |
using System.Net.Sockets; | |
namespace BeginSendSocketTest | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
var packet = new byte[4]; | |
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); | |
socket.Connect("localhost",11211); | |
SocketError errorCode; | |
socket.BeginSend(packet,0,packet.Length,SocketFlags.None,out errorCode,OnDataSent,socket); | |
Console.WriteLine("status: " + errorCode.ToString()); | |
} | |
public static void OnDataSent(IAsyncResult result) | |
{ | |
Console.WriteLine("Callback has been invoked."); | |
var socket = (Socket)result.AsyncState; | |
var sent = socket.EndSend(result); | |
Console.WriteLine(sent + "bytes sent"); | |
} | |
} | |
} |
Client, and yes, I'll edit the gist, thanks.
It doesn't matter though as that is never being called :D
Jason
On Tue, May 31, 2011 at 8:34 PM, panesofglass < ***@***.***>wrote:
Are you writing a client or server? `socket.Connect` should be used when
you are writing a client. You'll want `socket.Bind` and `socket.Listen` for
server code.
You also called `socket.EndReceive` in your `OnDataSent` callback. I think
you meant to call `socket.EndSend`.
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1001730
I fixed the EndReceive back to EndSend but as the issue is the callback is never invoked and that line is never called, it doesn't matter :)
Strange that the hang/exit behavior is different. Could imply something about differences in the underlying threading model.
Forking…
Thanks, this indeed did turn out to be the issue, main thread has to stay active until worker threads complete.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Are you writing a client or server?
socket.Connect
should be used when you are writing a client. You'll wantsocket.Bind
andsocket.Listen
for server code.You also called
socket.EndReceive
in yourOnDataSent
callback. I think you meant to callsocket.EndSend
.