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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Socket and It's Handle

Status
Not open for further replies.

Akusei

Programmer
Jul 15, 2003
90
US
I have a socket and it is connected with a client. I need to be able to pass the socket's handle as a command line argument to another .NET application and have it instantiate that socket to it can communicate with the remote computer.

For Example.
Code:
CODE FROM SERVER
private void NewConnection(Socket client)
{
   Process p = new Process();
   p.StartInfo.Filename = "whatever.exe";
   p.StartInfo.Arguments = client.Handle.ToInt64().ToString();
   p.Start();
   p.Close();
   p.Dispose();
}

On the other end when the process is started
Code:
CODE FROM WHATEVER.EXE

main(char[] args)
{
   IntPtr ptr = new IntPtr(Convert.ToInt64(args[1]));
   Socket client = HERE IS WERE I NEED HELP!!!
}

I need to somehow get the variable client to instantiate from the ptr pointer. Is this possible? Is there another way of doing this?

Thanks
Akusei
 
I come from a *nix socket programming background, and there you could just pass the actual socket file descriptor which is just an unsigned int. I don't think this will work in your case though. I would just create a new thread to handle the request.
 
I would love to just spawn a new thread, however the application can not work like that for a few reasons.

1. The process that is spawned will have multiple users and I do not want the users to be disconnected if the "telnet" server crashes

2. I need a seperate visible Console window for each spawned process

If I can accomplish both of these by creating multiple threads, then I would much rather do that, but I don't believe I am able to do that.
 
I also tried creating a GCHandle of type Pinned for the socket and passing the pinned address to the spawned process, then casting the IntPtr back to a GCHandle and then the Target back to a Socket; but this keeps giving me an error when I try to call GCHandle.AddrOfPinnedObject().

AHHHH I need help!
 
Ok, I found a sort of workaround for this and it has only one drawback that I can see so far.

I started the second process with the folling
Code:
   this.m_Proc = new Process();
   this.m_Proc.StartInfo.FileName = @"e:\something.exe";
   this.m_Proc.StartInfo.Arguments = node.ToString();
   this.m_Proc.StartInfo.UseShellExecute = false;
   this.m_Proc.StartInfo.RedirectStandardOutput = true;
   this.m_Proc.StartInfo.RedirectStandardInput = true;
   this.m_Proc.Start();

Then the "something.exe" operates as normal and all ouput from "something.exe" is redirected to the remote users screen. The only problem with this is that I want to be able to see the output on both the spawned process's console window and the remote users console window. With this method, all output is redirected to the remote user's console window and no output is shown on the spawned process's window... Is there any way to get both?

Thanks,
Akusei
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top