[Mono-list] Use existing socket descriptor

Rick Parrish mono at rickparrish.ca
Mon Aug 26 18:04:30 UTC 2013


I'm having troubles getting my application to use an existing socket
handle.  The application I'm trying to work with passes an integer (ie 27)
to my application via the command-line.  So I populate a SocketInformation
structure with that integer as the socket handle, then use that to create a
new Socket.  For example:

SocketInformation SI = new SocketInformation();
SI.Options = SocketInformationOptions.Connected;
SI.ProtocolInformation = new byte[24];
// Bunch of code to populate SI.ProtocolInformation
Socket S = new Socket(SI);
S.Send(new byte[] { 65, 66, 67, 68 });

When I call S.Send(), an exception is thrown with "The descriptor is not a
socket" as the message.

So I thought maybe I was populating the SocketInformation wrong, but when I
inspect the properties of the created Socket all looks good to me, and if I
replace the call to S.Send() with this it works fine:

send(S.Handle, new byte[] { 65, 66, 67, 68 }, 4, SocketFlags.None);

Where send() is defined as:

[DllImport("libc")]
private extern static int send(IntPtr sock, byte[] buf, int count,
SocketFlags flags);

Digging into things it looks like S.Send() will call Send_internal(), which
will in turn call _wapi_send(). This is where the call to send() is
ultimately made, but before that this happens:

if (_wapi_handle_type (handle) != WAPI_HANDLE_SOCKET) {
    WSASetLastError (WSAENOTSOCK);
    return(SOCKET_ERROR);
}

Is this possibly where my problem is coming from?  If so, any way around it
(aside from giving up the managed Socket class and pinvoking all the socket
i/o)?

Thanks,
Rick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20130826/87fe35be/attachment.html>


More information about the Mono-list mailing list