Sending a string using sockets (not that easy!)

I am using Nektra Deviare to call Winsock method calls. My idea is to learn how to also send messages through the same socket than the original application. So what I do, when I encounter the first call to send a message, I store the socket id (first argument to the send function) so I can use it later.

So here's my code:

    uint socket = 0;

[DllImport("Ws2_32.dll")]
private static extern int send(uint socket, string buf, int len, int flags);

void _proxy_OnFunctionCalled(DeviareTools.IProcess proc, DeviareParams.ICallInfo callInfo, Deviare.IRemoteCall rCall)
{
   socket = (uint)callInfo.Params.get_Item(0).Value;
}

      

and later

    int ret = send(socket, "ABC", 3, 0);
    MessageBox.Show(ret.ToString());

      

This last message always appears -1. Why should it be?

thank

edit: calling WSAGetLastError () returns 2. Which I don't know what that means as it doesn't look like msdn.

+2


source to share


3 answers


Try to do

MessageBox.Show(socket.ToString());

      



and make sure your socket variable is set.

I believe error 2 is "file not found" which will tell me that you have an invalid socket id, but I could be wrong about that.

+1


source


WinSock must be initialized, a socket must be created, a connection must be established (unless your socket is SOCK_DGRAM), so you can send () whatever you want. In addition, API functions are designed to work with data from pointers (char *), ensuring that this happens. Perhaps "ABC" is declared as a WIDECHAR array, String object, or something like a buffer (char *).



There is a complete example of a basic socket connection on MSDN here: http://msdn.microsoft.com/en-us/library/ms737625(VS.85).aspx

0


source


It might be a little late, but the problem is that you are trying to use a socket created in another process. Using Deviare you get the socket id from one process and you try to use it in your process. This doesn't work because socket ids are only valid in the process in which they were created.

0


source







All Articles