Skip to content

Instantly share code, notes, and snippets.

@jasonsirota
Created June 1, 2011 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jasonsirota/1001730 to your computer and use it in GitHub Desktop.
Save jasonsirota/1001730 to your computer and use it in GitHub Desktop.
BeginSend Socket Test Mon
/*
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");
}
}
}
@panesofglass
Copy link

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.

@jasonsirota
Copy link
Author

jasonsirota commented Jun 1, 2011 via email

@jasonsirota
Copy link
Author

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 :)

@bvanderveen
Copy link

Strange that the hang/exit behavior is different. Could imply something about differences in the underlying threading model.

Forking…

@jasonsirota
Copy link
Author

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