System.Net.Sockets.Socket.DuplicateAndClose C# (CSharp) Method

DuplicateAndClose() public method

public DuplicateAndClose ( int targetProcessId ) : SocketInformation
targetProcessId int
return SocketInformation
        public SocketInformation DuplicateAndClose(int targetProcessId)
        {
            //
            // On Windows, we cannot duplicate a socket that is bound to an IOCP.  In this implementation, we *only*
            // support IOCPs, so this will not work.  
            //
            // On Unix, duplication of a socket into an arbitrary process is not supported at all.
            //
            throw new PlatformNotSupportedException(SR.net_sockets_duplicateandclose_notsupported);
        }

Usage Example

示例#1
0
        static void Main(string[] args)
        {
            byte[] bytes = new byte[1024];
            // Connect to a remote device.
            try {
            // Establish the remote endpoint for the socket.
            // This example uses port 11000 on the local computer.
            string serverIP = args[0]; //"192.168.56.1";
            string client2IP = args[1];//192.168.56.1";
            IPAddress ipAddress = IPAddress.Parse(serverIP);
            IPEndPoint remoteEP = new IPEndPoint(ipAddress,1010);
            Console.WriteLine("Client1: I will create a socket object to talk to the server");
            // Create a TCP/IP  socket.
            Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
            // Connect the socket to the remote endpoint. Catch any errors.
            try {
                sender.Connect(remoteEP);
                Console.WriteLine("Client1: Got connected to {0}",sender.RemoteEndPoint.ToString());

                byte[] msg = Encoding.ASCII.GetBytes("This is Client1 saying hello!\n ");
                // Send the data through the socket.[
                Console.WriteLine("Client1: Sending message to server");

                int bytesSent = sender.Send(msg);

                Console.WriteLine("Client1: Message sent successfully! ");
                System.Net.IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(client2IP);
                Process clientTwoProcess = Process.GetProcessesByName("Client2VM", ipHostEntry.HostName)[0];

                SocketInformation sinfo = sender.DuplicateAndClose(clientTwoProcess.Id);

                IPAddress client2IPAddress = IPAddress.Parse(client2IP);
                remoteEP = new IPEndPoint(client2IPAddress, 2010);

                // Create a TCP/IP  socket.
                Socket sender1 = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);
                sender1.Connect(remoteEP);
                Console.WriteLine("Client1: Let me send the socket object to Client2");
                bytesSent = sender1.Send(sinfo.ProtocolInformation);
                sender1.Close();
                Console.WriteLine("Client1: GoodBye!");
                Console.ReadLine();

            } catch (ArgumentNullException ane) {
                Console.WriteLine("ArgumentNullException : {0}",ane.ToString());
            } catch (SocketException se) {
                Console.WriteLine("SocketException : {0}",se.ToString());
            } catch (Exception e) {
                Console.WriteLine("Unexpected exception : {0}", e.ToString());
            }

            } catch (Exception e) {
            Console.WriteLine( e.ToString());
            }
        }
All Usage Examples Of System.Net.Sockets.Socket::DuplicateAndClose